Android:列表多选

 <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() + " 人");
        });

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值