RecycleView的全选和反选

效果实现:点击全选,chechBox里的全都选上,点击反选,全都呈现未选中状态。

1.activity_xml中代码:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/all"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="全选" />

    <Button
        android:id="@+id/fan"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="反选" />

</LinearLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

2.item中的代码

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/text_item"
        android:layout_width="300dp"
        android:layout_height="wrap_content"

        />

    <CheckBox
        android:id="@+id/mCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
<View
    android:id="@+id/v"
    android:layout_width="match_parent"
    android:layout_height="3dp"
    android:background="#C0C0C0" />

3.Mainactivity中的代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button all;
    private Button fan;
    private RecyclerView rv;
    private ArrayList<Bean> arr;
    private LinearLayoutManager linearLayoutManager;
    private MyRecycleVAdapter myadapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        initData();

    }

    private void initData() {

        arr = new ArrayList<>();

        for (int i = 0; i < 100; i++) {

            Bean bean = new Bean(i + "", false);

            arr.add(bean);

        }

        linearLayoutManager = new LinearLayoutManager(this);

        rv.setLayoutManager(linearLayoutManager);
//        rv.addItemDecoration(new drawline());

        //设置数据
        if (myadapter == null) {
            myadapter = new MyRecycleVAdapter(arr, MainActivity.this);
            rv.setAdapter(myadapter);
        } else {
            myadapter.notifyDataSetChanged();
        }

        this.myadapter.setData(arr);


    }

    private void initView() {
        all = (Button) findViewById(R.id.all);
        fan = (Button) findViewById(R.id.fan);
        rv = (RecyclerView) findViewById(R.id.rv);

        all.setOnClickListener(this);
        fan.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.all:

                myadapter.setELL();


                break;

            //反选
            case R.id.fan:

                myadapter.setfalse();

                break;


        }
    }

4.Myadapter中的代码:

public class MyRecycleVAdapter extends RecyclerView.Adapter<MyRecycleVAdapter.MyViewHoder> {

    private ArrayList<Bean> list = new ArrayList<>();
    private Context context;



    public MyRecycleVAdapter(ArrayList<Bean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public MyViewHoder onCreateViewHolder(ViewGroup parent, int viewType) {

        View inflate = View.inflate(context, R.layout.item, null);

        MyViewHoder myViewHoder = new MyViewHoder(inflate);


        return myViewHoder;
    }

    @Override
    public void onBindViewHolder(final MyViewHoder holder, final int position) {

        holder.text_item.setText(list.get(position).getStr());
        holder.mCheckBox.setChecked(list.get(position).istrue());

        holder.text_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "" + list.get(position).toString(), Toast.LENGTH_SHORT).show();
            }
        });

        holder.text_item.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                AlertDialog.Builder builder = new AlertDialog.Builder(context);

                builder.setMessage("长按" + position);

                builder.show();


                return false;
            }
        });


    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    public void setData(ArrayList<Bean> arr) {
        if (arr != null) {
            list.addAll(arr);
        }
    }

    public void setELL() {

        for (int i = 0; i < list.size(); i++) {

            Bean bean = list.get(i);
            bean.setIstrue(true);

        }
        notifyDataSetChanged();

    }


    public void setfalse() {

        for (int i = 0; i < list.size(); i++) {

            Bean bean = list.get(i);
            bean.setIstrue(false);

        }
        notifyDataSetChanged();

    }


    class MyViewHoder extends RecyclerView.ViewHolder {


        private final TextView text_item;
        private final CheckBox mCheckBox;
        private final View viewById;

        public MyViewHoder(View itemView) {
            super(itemView);

            text_item = (TextView) itemView.findViewById(R.id.text_item);
            mCheckBox = (CheckBox) itemView.findViewById(R.id.mCheckBox);
            viewById = itemView.findViewById(R.id.v);

        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值