Android RecyclerView+CheckBox点击条目选中,全选,全不选,选中删除,全部删除的操作.

效果视频地址:   https://pan.baidu.com/s/1Vcoq5F4LKkR_wvAWccaK3A

视频:

Android RecyclerView+CheckBox点击条目选中,全选,全不选,选中删除,全部删除的操作._第1张图片

1.MyRecyclerViewAdapter

import android.content.Context;
import android.support.v7.widget.RecyclerView;
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 com.xunmatong.mydemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * Created by Administrator on 2018/10/27.
 */

public class MyRecyclerViewAdapter  extends RecyclerView.Adapter {
    private Context context;
    private List list;
    private List booleanlist=new ArrayList<>();

    public MyRecyclerViewAdapter(Context context) {
        this.context=context;
        list=new ArrayList<>();
    }

    public MyRecyclerViewAdapter(Context context,List list) {
        this.context=context;
        this.list=list;
        for (int i = 0; i < list.size(); i++) {
            //设置默认的显示
            booleanlist.add(false);
        }
    }

    public void addData(List strings){

        list.addAll(strings);
        for (int i = 0; i < strings.size(); i++) {
            booleanlist.add(false);
        }
        notifyDataSetChanged();
    }

    //更改集合内部存储的状态
    public void initCheck(boolean flag) {

        for (int i = 0; i < list.size(); i++) {
            //更改指定位置的数据
            booleanlist.set(i,flag);
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                context).inflate(R.layout.list_item, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.tv.setText(list.get(position));

        holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //用集合保存当前的状态
                booleanlist.set(position,isChecked);
            }
        });
        holder.tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "dianji", Toast.LENGTH_SHORT).show();
                booleanlist.set(position,holder.cb.isChecked()?false:true);
                holder.cb.setChecked(booleanlist.get(position));
            }
        });

        holder.cb.setChecked(booleanlist.get(position));
    }

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

    //清空所有数据
    public void deleteAllData() {

        list.clear();
        booleanlist.clear();
        notifyDataSetChanged();

    }

    //删除选中的数据
    public void deletingData() {

        int y=0;
        for (int i = 0; i < list.size(); i++) {
            if(booleanlist.get(i)!=null && booleanlist.get(i) ) {
                list.remove(i);
                booleanlist.remove(i);
                y++;
                i--;
            }
        }
        notifyDataSetChanged();

        if(y==0){
            Toast.makeText(context, "没有选中的要删除的数据", Toast.LENGTH_SHORT).show();
        }
    }

    public void selectAll(){
        initCheck(true);
        notifyDataSetChanged();
    }
    public void unSelectAll(){
        initCheck(false);
        notifyDataSetChanged();
    }


    /**
     * ViewHolder的类,用于缓存控件
     */
    class MyViewHolder extends RecyclerView.ViewHolder {

        CheckBox cb;
        TextView tv;
        public MyViewHolder(View view) {
            super(view);
            cb = (CheckBox) view.findViewById(R.id.list_item_cb);
            tv = (TextView) view.findViewById(R.id.list_item_tv);

        }

    }

}

2.Activity

 

public class Activity16 extends AppCompatActivity {

    private RecyclerView recyclerView;
    private List list = new ArrayList<>();
    private MyRecyclerViewAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity16);
        recyclerView = findViewById(R.id.recyclerView);
        TextView nochexbox = findViewById(R.id.nochexbox);
        TextView allchexbox = findViewById(R.id.allchexbox);
        TextView delete = findViewById(R.id.delete);
        TextView alldelete = findViewById(R.id.alldelete);
        for (int c = 0; c < 20; c++) {
            list.add("条目" + c);
        }
        //给RecyclerView设置布局管理器
        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        //初始化适配器
        adapter = new MyRecyclerViewAdapter(getApplicationContext(), list);
        //给recyclerView添加适配器
        recyclerView.setAdapter(adapter);
        //设置分隔线
        recyclerView.addItemDecoration(new DividerItemDecoration(this,1));
        //设置增加或删除条目的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        nochexbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.unSelectAll();
            }
        });
        allchexbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter. selectAll();
            }
        });
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.deletingData();
            }
        });
        alldelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.deleteAllData();
            }
        });
    }
}

3.activity16

 




    

    
        
        
        
        
    

4.flag_02  这个是文字背景(添加背景之后在代码中添加   

android:elevation="10dp"
android:translationZ="10dp"

)会有背景阴影的效果 .

代码:




    

    

    


总结:我们只是代码的搬运工,adapter是在网上搜到的方法,只是添加了一个条目点击的效果.

你可能感兴趣的:(Android RecyclerView+CheckBox点击条目选中,全选,全不选,选中删除,全部删除的操作.)