使用RecyclerView展示checkBox全选反选
来源:互联网 发布:手机闪光灯软件下载 编辑:程序博客网 时间:2024/06/03 19:09
使用时一定要在builde.gradle文件里添加依赖:compile 'com.android.support:recyclerview-v7:22.0.+'
======================================================
public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private Button fanxuan; private Button quanxuan; private LinearLayoutManager layoutManager; private myAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView) findViewById(R.id.recycleview); quanxuan = (Button) findViewById(R.id.allcheck); fanxuan = (Button) findViewById(R.id.fanxuan); //在加载数据之前配置 layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); //适配器 adapter = new myAdapter(); recyclerView.setAdapter(adapter); quanxuan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { adapter.all(); } }); fanxuan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { adapter.Fanxuan(); } }); }}
====================================================================
public class myViewHolder extends RecyclerView.ViewHolder { public View view; public TextView textView; public CheckBox checkBox; public myViewHolder(View view ) { super(view); this.view = view; textView = (TextView) view.findViewById(R.id.textaaa); checkBox = (CheckBox) view.findViewById(R.id.checkbox); }}====================================================================public class myAdapter extends RecyclerView.Adapter<myViewHolder> { //这个是checkbox的Hashmap集合 private final HashMap<Integer, Boolean> map; //这个是数据集合 private final ArrayList<String> list; public myAdapter() { map = new HashMap<>(); list = new ArrayList<>(); for (int i = 0; i < 30; i++) { list.add("Stronger Then I Was _" + i); map.put(i, false); } } //全选 public void all() { Set<Map.Entry<Integer, Boolean>> entries = map.entrySet(); boolean shouldAll = false; for (Map.Entry<Integer, Boolean> entry : entries) { boolean value = entry.getValue(); if (!value) { shouldAll = true; break; } } for (Map.Entry<Integer, Boolean> entry : entries) { entry.setValue(shouldAll); } notifyDataSetChanged(); } //反选 public void Fanxuan() { Set<Map.Entry<Integer, Boolean>> entrie = map.entrySet(); for (Map.Entry<Integer, Boolean> entry : entrie) { entry.setValue(!entry.getValue()); } notifyDataSetChanged(); } //单选 public void singleCheck(int position) { Set<Map.Entry<Integer, Boolean>> entrie = map.entrySet(); for (Map.Entry<Integer, Boolean> entry : entrie) { entry.setValue(false); } map.put(position, true); notifyDataSetChanged(); } @Override public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { RecyclerView.LayoutManager manager = ((RecyclerView) parent).getLayoutManager(); //初始化布局文件 View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.check, parent, false); return new myViewHolder(inflate); } @Override public void onBindViewHolder(myViewHolder holder, final int position) { holder.textView.setText(list.get(position)); holder.checkBox.setChecked(map.get(position)); holder.checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { map.put(position, !map.get(position)); notifyDataSetChanged(); //单选 singleCheck(position); } }); } @Override public int getItemCount() { return list.size(); }}=====================================================================<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.gss.jrtt.recyclerviewdemo.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/allcheck" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="全选" /> <Button android:id="@+id/fanxuan" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="反选" /> </LinearLayout> <android.support.v7.widget.RecyclerView android:id="@+id/recycleview" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView></LinearLayout>==========================================================================<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"><LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/textaaa" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stronger Then I Was"/></LinearLayout></LinearLayout>============================================================================
阅读全文
0 0
- 使用RecyclerView展示checkBox全选反选
- RecyclerView使用,item实现全选,反选,单选,计算checkbox选中的个数
- RecyclerView全选反选
- checkbox全选反选
- checkbox 全选 反选
- checkbox全选反选
- checkbox,全选,反选
- checkbox全选反选
- checkbox全选和反选
- jquery checkbox 全选 反选
- checkbox全选/反选 动画版
- checkbox 全选 与 反选
- checkbox全选与反选
- checkbox全选与反选
- checkbox全选和反选
- checkbox全选和反选
- checkbox全选和反选
- jQuery CheckBox全选、反选
- C
- DenseNet 网络
- mysql_简单查询
- 第三周项目三求集合并集
- Java架构师必须要懂的知识(一)
- 使用RecyclerView展示checkBox全选反选
- C++数据共享与保护
- 2017亚洲区域赛-西安赛区
- 英语日常短语(2)
- TLS encryption and mutual authentication using syslog-ng Open Source Edition
- 哈哈日语 巧辨初级语法「~にくい」和「~がたい」
- An annotated path to start with Machine Learning
- 网易乐得 线下笔试 求第N个丑数 Java实现
- HDU1012