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

xiaoxiao2022-06-03  4

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

视频:

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<MyRecyclerViewAdapter.MyViewHolder> { private Context context; private List<String> list; private List<Boolean> booleanlist=new ArrayList<>(); public MyRecyclerViewAdapter(Context context) { this.context=context; list=new ArrayList<>(); } public MyRecyclerViewAdapter(Context context,List<String> list) { this.context=context; this.list=list; for (int i = 0; i < list.size(); i++) { //设置默认的显示 booleanlist.add(false); } } public void addData(List<String> 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<String> 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

 

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:layout_weight="1" android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_marginRight="10dp" android:background="@drawable/flag_02" android:elevation="10dp" android:translationZ="10dp" android:gravity="center" android:layout_weight="1" android:padding="10dp" android:id="@+id/nochexbox" android:layout_width="0dp" android:layout_height="wrap_content" android:text="全不选"/> <TextView android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_marginRight="10dp" android:background="@drawable/flag_02" android:elevation="10dp" android:translationZ="10dp" android:gravity="center" android:layout_weight="1" android:padding="10dp" android:id="@+id/allchexbox" android:layout_width="0dp" android:layout_height="wrap_content" android:text="全选"/> <TextView android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_marginRight="10dp" android:background="@drawable/flag_02" android:elevation="10dp" android:translationZ="10dp" android:gravity="center" android:layout_weight="1" android:padding="10dp" android:id="@+id/delete" android:layout_width="0dp" android:layout_height="wrap_content" android:text="删除"/> <TextView android:layout_marginRight="10dp" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:background="@drawable/flag_02" android:elevation="10dp" android:translationZ="10dp" android:gravity="center" android:layout_weight="1" android:padding="10dp" android:id="@+id/alldelete" android:layout_width="0dp" android:layout_height="wrap_content" android:text="全部删除"/> </LinearLayout> </LinearLayout>

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

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

)会有背景阴影的效果 .

代码:

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <gradient android:angle="180" android:endColor="#ffffff" android:centerColor="#ffffff" android:startColor="#ffffff" /> <corners android:radius="10dp" /> <padding android:bottom="2dp" android:left="10dp" android:right="10dp" android:top="2dp" /> </shape>

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

转载请注明原文地址: https://www.6miu.com/read-4915053.html

最新回复(0)