RecyclerView出现已经很久了,使用面也是非常广的,现在就来用Recyclerview实现多选模式
基本思路是为Adapter增加一个Boolean集合
- SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
用来为Adapter 里的数据item设置标记,默认每个条目为false,选中的话就设置为true
- private void setItemChecked(int position, boolean isChecked) {
- mSelectedPositions.put(position, isChecked);
- }
通过条目位置得到该条目的Boolean值,就可以知道条目有没有选中这时可以在onbindview里设置chekbox的状态了,同时设置chekbox的监听
- //根据位置判断条目是否选中
- private boolean isItemChecked(int position) {
- return mSelectedPositions.get(position);
- }
- //绑定界面,设置监听
- @Override
- public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int i) {
- //设置条目状态
- ((ListItemViewHolder) holder).mainTitle.setText(mList.get(i));
- ((ListItemViewHolder) holder).checkBox.setChecked(isItemChecked(i));
- //checkBox的监听
- ((ListItemViewHolder) holder).checkBox.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (isItemChecked(i)) {
- setItemChecked(i, false);
- } else {
- setItemChecked(i, true);
- }
- ab.setTitle("已选择" + getSelectedItem().size() + "项");
- }
- });
- //条目view的监听
- ((ListItemViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (isItemChecked(i)) {
- setItemChecked(i, false);
- } else {
- setItemChecked(i, true);
- }
- notifyItemChanged(i);
- ab.setTitle("已选择" + getSelectedItem().size() + "项");
- }
- });
- }
最后返回选中的条目数据
- //获得选中条目的结果
- public ArrayList<String> getSelectedItem() {
- ArrayList<String> selectList = new ArrayList<>();
- for (int i = 0; i < mList.size(); i++) {
- if (isItemChecked(i)) {
- selectList.add(mList.get(i));
- }
- }
- return se
完整代码如下
MainActivity
- </pre><pre name="code" class="java"><span style="font-size:14px;">package com.enrique.recyclerview;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.ActionBar;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.support.v7.widget.Toolbar;
- import android.util.SparseBooleanArray;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.TextView;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- private ArrayList<String> mList = new ArrayList<>();
- RecyclerView recyclerView;
- LinearLayoutManager layoutManager;
- SelectAdapter mAdapter;
- ActionBar ab;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- ab = getSupportActionBar();
- for(int i = 0; i < 10 ; i++){
- mList.add("条目" + i );
- }
- recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
- layoutManager = new LinearLayoutManager(this);
- recyclerView.setLayoutManager(layoutManager);
- mAdapter = new SelectAdapter(mList);
- recyclerView.setAdapter(mAdapter);
- setItemDecoration();
- Button fab = (Button) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- mAdapter.updateDataSet(mAdapter.getSelectedItem());
- mAdapter.notifyDataSetChanged();
- }
- });
- }
- //设置分割线
- private void setItemDecoration() {
- RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
- recyclerView.addItemDecoration(itemDecoration);
- }
- public class SelectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
- private ArrayList<String> mList = new ArrayList<>();
- private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
- private boolean mIsSelectable = false;
- public SelectAdapter(ArrayList<String> list) {
- if (list == null) {
- throw new IllegalArgumentException("model Data must not be null");
- }
- mList = list;
- }
- //更新adpter的数据和选择状态
- public void updateDataSet(ArrayList<String> list) {
- this.mList = list;
- mSelectedPositions = new SparseBooleanArray();
- ab.setTitle("已选择" + 0 + "项");
- }
- //获得选中条目的结果
- public ArrayList<String> getSelectedItem() {
- ArrayList<String> selectList = new ArrayList<>();
- for (int i = 0; i < mList.size(); i++) {
- if (isItemChecked(i)) {
- selectList.add(mList.get(i));
- }
- }
- return selectList;
- }
- @Override
- public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
- View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_item, viewGroup, false);
- return new ListItemViewHolder(itemView);
- }
- //设置给定位置条目的选择状态
- private void setItemChecked(int position, boolean isChecked) {
- mSelectedPositions.put(position, isChecked);
- }
- //根据位置判断条目是否选中
- private boolean isItemChecked(int position) {
- return mSelectedPositions.get(position);
- }
- //根据位置判断条目是否可选
- private boolean isSelectable() {
- return mIsSelectable;
- }
- //设置给定位置条目的可选与否的状态
- private void setSelectable(boolean selectable) {
- mIsSelectable = selectable;
- }
- //绑定界面,设置监听
- @Override
- public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int i) {
- //设置条目状态
- ((ListItemViewHolder) holder).mainTitle.setText(mList.get(i));
- ((ListItemViewHolder) holder).checkBox.setChecked(isItemChecked(i));
- //checkBox的监听
- ((ListItemViewHolder) holder).checkBox.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (isItemChecked(i)) {
- setItemChecked(i, false);
- } else {
- setItemChecked(i, true);
- }
- ab.setTitle("已选择" + getSelectedItem().size() + "项");
- }
- });
- //条目view的监听
- ((ListItemViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (isItemChecked(i)) {
- setItemChecked(i, false);
- } else {
- setItemChecked(i, true);
- }
- notifyItemChanged(i);
- ab.setTitle("已选择" + getSelectedItem().size() + "项");
- }
- });
- }
- @Override
- public int getItemCount() {
- return mList == null ? 0 : mList.size();
- }
- public class ListItemViewHolder extends RecyclerView.ViewHolder{
- //ViewHolder
- CheckBox checkBox;
- TextView mainTitle;
- ListItemViewHolder(View view) {
- super(view);
- this.mainTitle = (TextView) view.findViewById(R.id.text);
- this.checkBox = (CheckBox) view.findViewById(R.id.select_checkbox);
- }
- }
- }
- }
- </span>
layout activity_main.xml 的代码
- <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
- <android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
- tools:context="com.enrique.recyclerview.MainActivity">
- <android.support.design.widget.AppBarLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:theme="@style/AppTheme.AppBarOverlay">
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:background="?attr/colorPrimary"
- app:popupTheme="@style/AppTheme.PopupOverlay" />
- </android.support.design.widget.AppBarLayout>
- <include layout="@layout/content_main" />
- <Button
- android:id="@+id/fab"
- android:text="更新"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="bottom|end"
- android:layout_margin="@dimen/fab_margin"
- android:src="@android:drawable/ic_dialog_email" />
- </android.support.design.widget.CoordinatorLayout></span>
- <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- app:layout_behavior="@string/appbar_scrolling_view_behavior"
- tools:context="com.enrique.recyclerview.MainActivity"
- tools:showIn="@layout/activity_main">
- <android.support.v7.widget.RecyclerView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/recyclerview"/>
- </RelativeLayout></span>
recyclerview_item.xml
- <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal" android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:paddingTop="10dp"
- android:paddingBottom="10dp">
- <CheckBox
- android:layout_gravity="center_vertical"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/select_checkbox"/>
- <TextView</span>
- <span style="font-size:14px;"> android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAlignment="center"
- android:layout_gravity="center_vertical"
- android:textSize="15sp"
- android:text="条目"
- android:id="@+id/text"/>
- </LinearLayout></span>
RecyclerView出现已经很久了,使用面也是非常广的,现在就来用Recyclerview实现多选模式
基本思路是为Adapter增加一个Boolean集合
- SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
用来为Adapter 里的数据item设置标记,默认每个条目为false,选中的话就设置为true
- private void setItemChecked(int position, boolean isChecked) {
- mSelectedPositions.put(position, isChecked);
- }
通过条目位置得到该条目的Boolean值,就可以知道条目有没有选中这时可以在onbindview里设置chekbox的状态了,同时设置chekbox的监听
- //根据位置判断条目是否选中
- private boolean isItemChecked(int position) {
- return mSelectedPositions.get(position);
- }
- //绑定界面,设置监听
- @Override
- public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int i) {
- //设置条目状态
- ((ListItemViewHolder) holder).mainTitle.setText(mList.get(i));
- ((ListItemViewHolder) holder).checkBox.setChecked(isItemChecked(i));
- //checkBox的监听
- ((ListItemViewHolder) holder).checkBox.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (isItemChecked(i)) {
- setItemChecked(i, false);
- } else {
- setItemChecked(i, true);
- }
- ab.setTitle("已选择" + getSelectedItem().size() + "项");
- }
- });
- //条目view的监听
- ((ListItemViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (isItemChecked(i)) {
- setItemChecked(i, false);
- } else {
- setItemChecked(i, true);
- }
- notifyItemChanged(i);
- ab.setTitle("已选择" + getSelectedItem().size() + "项");
- }
- });
- }
最后返回选中的条目数据
- //获得选中条目的结果
- public ArrayList<String> getSelectedItem() {
- ArrayList<String> selectList = new ArrayList<>();
- for (int i = 0; i < mList.size(); i++) {
- if (isItemChecked(i)) {
- selectList.add(mList.get(i));
- }
- }
- return se
完整代码如下
MainActivity
- </pre><pre name="code" class="java"><span style="font-size:14px;">package com.enrique.recyclerview;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.ActionBar;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.support.v7.widget.Toolbar;
- import android.util.SparseBooleanArray;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.TextView;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- private ArrayList<String> mList = new ArrayList<>();
- RecyclerView recyclerView;
- LinearLayoutManager layoutManager;
- SelectAdapter mAdapter;
- ActionBar ab;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- ab = getSupportActionBar();
- for(int i = 0; i < 10 ; i++){
- mList.add("条目" + i );
- }
- recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
- layoutManager = new LinearLayoutManager(this);
- recyclerView.setLayoutManager(layoutManager);
- mAdapter = new SelectAdapter(mList);
- recyclerView.setAdapter(mAdapter);
- setItemDecoration();
- Button fab = (Button) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- mAdapter.updateDataSet(mAdapter.getSelectedItem());
- mAdapter.notifyDataSetChanged();
- }
- });
- }
- //设置分割线
- private void setItemDecoration() {
- RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
- recyclerView.addItemDecoration(itemDecoration);
- }
- public class SelectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
- private ArrayList<String> mList = new ArrayList<>();
- private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
- private boolean mIsSelectable = false;
- public SelectAdapter(ArrayList<String> list) {
- if (list == null) {
- throw new IllegalArgumentException("model Data must not be null");
- }
- mList = list;
- }
- //更新adpter的数据和选择状态
- public void updateDataSet(ArrayList<String> list) {
- this.mList = list;
- mSelectedPositions = new SparseBooleanArray();
- ab.setTitle("已选择" + 0 + "项");
- }
- //获得选中条目的结果
- public ArrayList<String> getSelectedItem() {
- ArrayList<String> selectList = new ArrayList<>();
- for (int i = 0; i < mList.size(); i++) {
- if (isItemChecked(i)) {
- selectList.add(mList.get(i));
- }
- }
- return selectList;
- }
- @Override
- public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
- View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_item, viewGroup, false);
- return new ListItemViewHolder(itemView);
- }
- //设置给定位置条目的选择状态
- private void setItemChecked(int position, boolean isChecked) {
- mSelectedPositions.put(position, isChecked);
- }
- //根据位置判断条目是否选中
- private boolean isItemChecked(int position) {
- return mSelectedPositions.get(position);
- }
- //根据位置判断条目是否可选
- private boolean isSelectable() {
- return mIsSelectable;
- }
- //设置给定位置条目的可选与否的状态
- private void setSelectable(boolean selectable) {
- mIsSelectable = selectable;
- }
- //绑定界面,设置监听
- @Override
- public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int i) {
- //设置条目状态
- ((ListItemViewHolder) holder).mainTitle.setText(mList.get(i));
- ((ListItemViewHolder) holder).checkBox.setChecked(isItemChecked(i));
- //checkBox的监听
- ((ListItemViewHolder) holder).checkBox.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (isItemChecked(i)) {
- setItemChecked(i, false);
- } else {
- setItemChecked(i, true);
- }
- ab.setTitle("已选择" + getSelectedItem().size() + "项");
- }
- });
- //条目view的监听
- ((ListItemViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (isItemChecked(i)) {
- setItemChecked(i, false);
- } else {
- setItemChecked(i, true);
- }
- notifyItemChanged(i);
- ab.setTitle("已选择" + getSelectedItem().size() + "项");
- }
- });
- }
- @Override
- public int getItemCount() {
- return mList == null ? 0 : mList.size();
- }
- public class ListItemViewHolder extends RecyclerView.ViewHolder{
- //ViewHolder
- CheckBox checkBox;
- TextView mainTitle;
- ListItemViewHolder(View view) {
- super(view);
- this.mainTitle = (TextView) view.findViewById(R.id.text);
- this.checkBox = (CheckBox) view.findViewById(R.id.select_checkbox);
- }
- }
- }
- }
- </span>
layout activity_main.xml 的代码
- <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
- <android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
- tools:context="com.enrique.recyclerview.MainActivity">
- <android.support.design.widget.AppBarLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:theme="@style/AppTheme.AppBarOverlay">
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:background="?attr/colorPrimary"
- app:popupTheme="@style/AppTheme.PopupOverlay" />
- </android.support.design.widget.AppBarLayout>
- <include layout="@layout/content_main" />
- <Button
- android:id="@+id/fab"
- android:text="更新"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="bottom|end"
- android:layout_margin="@dimen/fab_margin"
- android:src="@android:drawable/ic_dialog_email" />
- </android.support.design.widget.CoordinatorLayout></span>
- <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- app:layout_behavior="@string/appbar_scrolling_view_behavior"
- tools:context="com.enrique.recyclerview.MainActivity"
- tools:showIn="@layout/activity_main">
- <android.support.v7.widget.RecyclerView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/recyclerview"/>
- </RelativeLayout></span>
recyclerview_item.xml
- <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal" android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:paddingTop="10dp"
- android:paddingBottom="10dp">
- <CheckBox
- android:layout_gravity="center_vertical"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/select_checkbox"/>
- <TextView</span>
- <span style="font-size:14px;"> android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAlignment="center"
- android:layout_gravity="center_vertical"
- android:textSize="15sp"
- android:text="条目"
- android:id="@+id/text"/>
- </LinearLayout></span>