Android进阶之路 - RecyclerView记录CheckBox选取数据

目前没有把全选,反选,全部取消的功能集成到此Demo中,只实现了基本的选取记录与记录功能。

存在问题与解决方案:

问题:RecyclerView\checkBox的复用问题,导致checkBox出现选取0角标的box时候角标10同样被选取
解决:给予CheckBox的状态记录

开发中心思维:

使用map集合来存储对应角标的对应状态,一般为Map”Intenger,Boolean”,在使用中如某一条信息被选取,那么就是记录当前的角标位为true,即被选取的状态,之后循环我们的Lists总数据,同时内部循环map记录的角标位,判断为true的时候,获取数据存储到新的List中!

build :

compile 'com.android.support:recyclerview-v7:24.2.1'

Effect :

Android进阶之路 - RecyclerView记录CheckBox选取数据_第1张图片

MainActivity :

package com.yl.checkbox.checkboxdata;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private RecyclerView mRvView;
    private TextView mAll;
    private TextView mCancel;
    private TextView mInvert;
    private CheckAdapter checkAdapter;
    private List lists;
    private ListlistDatas=new ArrayList<>();
    private TextView mGetData;

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

        lists = new ArrayList<>();
        for(int i=0;i<20;i++){
            lists.add("没有什么能够阻挡,天马星空的梦想"+i);
        }

        initView();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRvView.setLayoutManager(linearLayoutManager);
        checkAdapter = new CheckAdapter(this, lists);
        mRvView.setAdapter(checkAdapter);

        //子条目监听
        checkAdapter.setItemClickListener(new RecyclerViewOnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {
                Toast.makeText(MainActivity.this,"If you are happy - "+ position,Toast.LENGTH_SHORT).show();
                //设置选中的项
                checkAdapter.setSelectItem(position);
            }
        });
    }

    private void initView() {
        mRvView = (RecyclerView) findViewById(R.id.rv_view);
        mAll = (TextView) findViewById(R.id.tv_all);
        mCancel = (TextView) findViewById(R.id.tv_cancel);
        mInvert = (TextView) findViewById(R.id.tv_invert);
        mGetData = (TextView) findViewById(R.id.tv_getData);
        mAll.setOnClickListener(this);
        mCancel.setOnClickListener(this);
        mInvert.setOnClickListener(this);
        mGetData.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_getData:
                String content ="";
                listDatas.clear();

                Toast.makeText(MainActivity.this,"获取我们选取的数据",Toast.LENGTH_SHORT).show();
                Log.e("TAG",mGetData.getText().toString());

                Map map = checkAdapter.getMap();
                for(int i =0;iif(map.get(i)){
                        listDatas.add(lists.get(i));
                    }
                }

                //这里是为了测试我们的结果 ,可忽略!
                for (int j=0;j"TAG",listDatas.get(j));
                    content+=listDatas.get(j);
                }
                mGetData.setText(content);
                Log.e("TAG",content);
                break;

            //以下三个功能后续扩展
            case R.id.tv_all:
                Toast.makeText(MainActivity.this,"全选",Toast.LENGTH_SHORT).show();
                break;

            case R.id.tv_cancel:
                Toast.makeText(MainActivity.this,"取消全选",Toast.LENGTH_SHORT).show();
                break;

            case R.id.tv_invert:
                Toast.makeText(MainActivity.this,"反选",Toast.LENGTH_SHORT).show();
                break;

        }
    }

    //Adapter数据填充
    class CheckAdapter extends RecyclerView.Adapter{
        private Context mContext;
        private List lists;
        private HashMapMaps=new HashMap();
        private HashMapAllMaps=new HashMap();
        public RecyclerViewOnItemClickListener onItemClickListener;

        //成员方法,初始化checkBox的状态,默认全部不选中
        public CheckAdapter(Context context, List lists){
            this.mContext=context;
            this.lists=lists;
            initMap();
        }

        //初始化map内的数据状态,全部重置为false,即为选取状态
        private void initMap() {
            for (int i = 0; i < lists.size(); i++) {
                Maps.put(i, false);
            }
        }


        //获取最终的map存储数据
        public MapgetMap(){
            return Maps;
        }

        //后续扩展 - 获取最终的map存储数据
        public MapgetAllMap(){
            return AllMaps;
        }

        //点击item选中CheckBox
        public void setSelectItem(int position) {
            //对当前状态取反
            if (Maps.get(position)) {
                Maps.put(position, false);
            } else {
                Maps.put(position, true);
            }
            notifyItemChanged(position);
        }
        @Override
        public CheckAdapter.CheckViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            CheckViewHolder checkViewHolder = new CheckViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.item_layout, parent,false),onItemClickListener);
            return checkViewHolder;
        }

        @Override
        public void onBindViewHolder(CheckAdapter.CheckViewHolder holder, final int position) {


            holder.mName.setText(lists.get(position));

            holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Maps.put(position,isChecked);
                }
            });

            if(Maps.get(position)==null){
                Maps.put(position,false);
            }
            //没有设置tag之前会有item重复选框出现,设置tag之后,此问题解决
            holder.mCheckBox.setChecked(Maps.get(position));


            //之后扩展使用
            AllMaps.put(position,true);
        }


        @Override
        public int getItemCount() {
            return lists ==null?0:lists.size();
        }

        public void setItemClickListener(RecyclerViewOnItemClickListener onItemClickListener) {
            this.onItemClickListener = onItemClickListener;
        }

        class CheckViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            private RecyclerViewOnItemClickListener mListener;
            private  TextView mName;
            private  CheckBox mCheckBox;
            public CheckViewHolder(View itemView,RecyclerViewOnItemClickListener onItemClickListener) {
                super(itemView);
                this.mListener = onItemClickListener;
                itemView.setOnClickListener(this);
                mName = (TextView) itemView.findViewById(R.id.item_name);
                mCheckBox = (CheckBox) itemView.findViewById(R.id.item_cb);
            }

            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClickListener(v, getPosition());
                }
            }
        }

    }

    //接口回调设置点击事件
    public interface RecyclerViewOnItemClickListener {
        //点击事件
        void onItemClickListener(View view, int position);
    }

}

MainActivity Xml :


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="com.yl.checkbox.checkboxdata.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:visibility="gone"
        >
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="25dp"
            android:gravity="center"
            android:id="@+id/tv_all"
            android:text="全选" />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="25dp"
            android:gravity="center"
            android:id="@+id/tv_cancel"
            android:text="取消全选" />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/tv_invert"
            android:layout_height="25dp"
            android:gravity="center"
            android:text="反选" />
    LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#21aae3"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取选取数据"
        android:gravity="center"
        android:padding="5dp"
        android:id="@+id/tv_getData"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#21aae3"
        />

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

    android.support.v7.widget.RecyclerView>

LinearLayout>

你可能感兴趣的:(#,项目开发知识点归纳)