<LinearLayout
android:id="@+id/ll_select"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:visibility="gone">
<com.deve.by.andy.guojin.common.view.MyTextView
android:id="@+id/tv_select_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:drawableStart="@drawable/selector_item_selected"
android:drawablePadding="@dimen/dp_10"
android:text="@string/select_all" />
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="@+id/tv_select_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已选 0 人"
android:textColor="@color/color_999"
android:textSize="14sp" />
</LinearLayout>
1、Adapter
public class SelectPersonAdapter extends BaseQuickAdapter<MyStudentResult.AppendDataBean, BaseViewHolder> {
public SelectPersonAdapter(@Nullable List<MyStudentResult.AppendDataBean> data) {
super(R.layout.item_skill_select_person, data);
}
@Override
protected void convert(BaseViewHolder helper, MyStudentResult.AppendDataBean b) {
NiceImageView img = helper.getView(R.id.iv);
GlideUtils.Companion.showWithPath(b.getFactUrl(), img);
helper.setText(R.id.tv_name, b.getUserName());
helper.setText(R.id.tv_class, b.getDepartmentName());
}
public String getCheckPersons() {
if (getData().size() == 0) {
return "";
}
StringBuilder personIds = new StringBuilder();
for (MyStudentResult.AppendDataBean bean : getData()) {
if (bean.isChecked()) {
personIds.append(bean.getStudentID()).append(",");
}
}
personIds.deleteCharAt(personIds.length() - 1);
return personIds.toString();
}
public List<MyStudentResult.AppendDataBean> getCheckPersonList() {
List<MyStudentResult.AppendDataBean> list = new ArrayList<>();
for (MyStudentResult.AppendDataBean bean : getData()) {
if (bean.isChecked()) {
list.add(bean);
}
}
return list;
}
}
2、用法
/**
* 判断是否全选
*
* @return
*/
private boolean isAllCheck() {
for (MyStudentResult.AppendDataBean bean : mPersonList) {
if (!bean.isChecked()) {
return false;
}
}
return true;
}
case R.id.tv_select_all://全选
mTvSelectAll.setSelected(!mTvSelectAll.isSelected());
for (MyStudentResult.AppendDataBean bean : mPersonList) {
bean.setChecked(mTvSelectAll.isSelected());
mSelectPersonAdapter.notifyDataSetChanged();
}
mTvSelectNum.setText("已选 " + mSelectPersonAdapter.getCheckPersonList().size() + " 人");
break;
mSelectPersonAdapter.setOnItemClickListener((adapter, view, position) -> {
MyStudentResult.AppendDataBean item = mSelectPersonAdapter.getItem(position);
item.setChecked(!item.isChecked());
mSelectPersonAdapter.notifyItemChanged(position);
mTvSelectAll.setSelected(isAllCheck());
mTvSelectNum.setText("已选 " + mSelectPersonAdapter.getCheckPersonList().size() + " 人");
});