使用时一定要在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>============================================================================
使用RecyclerView展示checkBox全选反选
最新推荐文章于 2024-04-09 03:27:12 发布