public class SelectLeaderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<String> mList;
private boolean mIsSelectable;
private SparseBooleanArray mSelectedPositions;
public SelectLeaderAdapter(Context context, List<String> list) {
this.context = context;
this.mList = list;
}
//更新adpter的数据和选择状态
public void updateDataSet(ArrayList<String> list) {
this.mList = list;
mSelectedPositions = new SparseBooleanArray();
}
//获得选中条目的结果
public ArrayList<String> getSelectedItem() {
ArrayList<String> selectList = new ArrayList<>();
for (int i = 0; i < mList.size(); i++) {
if (isItemChecked(i)) {
selectList.add(mList.get(i));
}
}
return selectList;
}
//设置给定位置条目的选择状态
private void setItemChecked(int position, boolean isChecked) {
if (mSelectedPositions == null) {
mSelectedPositions = new SparseBooleanArray();
}
mSelectedPositions.put(position, isChecked);
}
//根据位置判断条目是否选中
private boolean isItemChecked(int position) {
if (mSelectedPositions != null) {
return mSelectedPositions.get(position);
}
return false;
}
//根据位置判断条目是否可选
private boolean isSelectable() {
return mIsSelectable;
}
//设置给定位置条目的可选与否的状态
private void setSelectable(boolean selectable) {
mIsSelectable = selectable;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_selectleaderauther_lv, viewGroup, false);
return new ListItemViewHolder(itemView);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
//设置条目状态
((ListItemViewHolder) holder).name.setText(mList.get(position));
//判断是否选中
boolean itemChecked = isItemChecked(position);
if (itemChecked) {
((ListItemViewHolder) holder).name.setTextColor(context.getResources().getColor(R.color.bg_White));
((ListItemViewHolder) holder).name.setBackgroundColor(context.getResources().getColor(R.color.themeColor));
} else {
((ListItemViewHolder) holder).name.setBackgroundColor(context.getResources().getColor(R.color.green3));
((ListItemViewHolder) holder).name.setTextColor(context.getResources().getColor(R.color.green1));
}
((ListItemViewHolder) holder).checkBox.setChecked(itemChecked);
//checkBox的监听
((ListItemViewHolder) holder).checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isItemChecked(position)) {
setItemChecked(position, false);
} else {
setItemChecked(position, true);
}
}
});
//条目view的监听
((ListItemViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isItemChecked(position)) {
setItemChecked(position, false);
} else {
setItemChecked(position, true);
}
notifyItemChanged(position);
}
});
}
@Override
public int getItemCount() {
return mList == null ? 0 : mList.size();
}
public class ListItemViewHolder extends RecyclerView.ViewHolder {
CheckBox checkBox;
TextView name;
ListItemViewHolder(View view) {
super(view);
this.name = (TextView) view.findViewById(R.id.selectfriends_username);
this.checkBox = (CheckBox) view.findViewById(R.id.cb_selectfriends);
}
}
}
网友评论