Android RecyclerView实现列表多选



RecyclerView出现已经很久了,使用面也是非常广的,现在就来用Recyclerview实现多选模式

基本思路是为Adapter增加一个Boolean集合

[java]  view plain  copy
  1. SparseBooleanArray mSelectedPositions = new SparseBooleanArray();  

 用来为Adapter 里的数据item设置标记,默认每个条目为false,选中的话就设置为true
[java]  view plain  copy
  1. private void setItemChecked(int position, boolean isChecked) {  
  2.             mSelectedPositions.put(position, isChecked);  
  3.         }  

通过条目位置得到该条目的Boolean值,就可以知道条目有没有选中这时可以在onbindview里设置chekbox的状态了,同时设置chekbox的监听

[java]  view plain  copy
  1. //根据位置判断条目是否选中  
  2.         private boolean isItemChecked(int position) {  
  3.             return mSelectedPositions.get(position);  
  4.         }  


[java]  view plain  copy
  1. //绑定界面,设置监听  
  2.         @Override  
  3.         public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int i) {  
  4.             //设置条目状态  
  5.             ((ListItemViewHolder) holder).mainTitle.setText(mList.get(i));  
  6.             ((ListItemViewHolder) holder).checkBox.setChecked(isItemChecked(i));  
  7.   
  8.             //checkBox的监听  
  9.             ((ListItemViewHolder) holder).checkBox.setOnClickListener(new View.OnClickListener() {  
  10.                 @Override  
  11.                 public void onClick(View v) {  
  12.                     if (isItemChecked(i)) {  
  13.                         setItemChecked(i, false);  
  14.                     } else {  
  15.                         setItemChecked(i, true);  
  16.                     }  
  17.                     ab.setTitle("已选择" + getSelectedItem().size() + "项");  
  18.                 }  
  19.             });  
  20.   
  21.             //条目view的监听  
  22.             ((ListItemViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {  
  23.                 @Override  
  24.                 public void onClick(View v) {  
  25.                     if (isItemChecked(i)) {  
  26.                         setItemChecked(i, false);  
  27.                     } else {  
  28.                         setItemChecked(i, true);  
  29.                     }  
  30.                     notifyItemChanged(i);  
  31.                     ab.setTitle("已选择" + getSelectedItem().size() + "项");  
  32.                 }  
  33.             });  
  34.   
  35.   
  36.         }  


最后返回选中的条目数据

[java]  view plain  copy
  1. //获得选中条目的结果  
  2.        public ArrayList<String> getSelectedItem() {  
  3.            ArrayList<String> selectList = new ArrayList<>();  
  4.            for (int i = 0; i < mList.size(); i++) {  
  5.                if (isItemChecked(i)) {  
  6.                    selectList.add(mList.get(i));  
  7.                }  
  8.            }  
  9.            return se  



完整代码如下

MainActivity


[java]  view plain  copy
  1. </pre><pre name="code" class="java"><span style="font-size:14px;">package com.enrique.recyclerview;  
  2.   
  3. import android.app.FragmentTransaction;  
  4. import android.os.Bundle;  
  5. import android.support.design.widget.FloatingActionButton;  
  6. import android.support.design.widget.Snackbar;  
  7.   
  8.   
  9. import android.support.v7.app.ActionBar;  
  10. import android.support.v7.app.AppCompatActivity;  
  11. import android.support.v7.widget.LinearLayoutManager;  
  12. import android.support.v7.widget.RecyclerView;  
  13. import android.support.v7.widget.Toolbar;  
  14. import android.util.SparseBooleanArray;  
  15. import android.view.LayoutInflater;  
  16. import android.view.View;  
  17. import android.view.Menu;  
  18. import android.view.MenuItem;  
  19. import android.view.ViewGroup;  
  20. import android.widget.Button;  
  21. import android.widget.CheckBox;  
  22. import android.widget.TextView;  
  23.   
  24. import java.util.ArrayList;  
  25.   
  26. public class MainActivity extends AppCompatActivity {  
  27.   
  28.     private ArrayList<String> mList = new ArrayList<>();  
  29.     RecyclerView recyclerView;  
  30.     LinearLayoutManager layoutManager;  
  31.     SelectAdapter mAdapter;  
  32.     ActionBar ab;  
  33.   
  34.     @Override  
  35.     protected void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.activity_main);  
  38.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);  
  39.         setSupportActionBar(toolbar);  
  40.         ab = getSupportActionBar();  
  41.   
  42.         for(int i = 0; i < 10 ; i++){  
  43.             mList.add("条目" + i );  
  44.         }  
  45.   
  46.         recyclerView = (RecyclerView) findViewById(R.id.recyclerview);  
  47.         layoutManager = new LinearLayoutManager(this);  
  48.         recyclerView.setLayoutManager(layoutManager);  
  49.         mAdapter = new SelectAdapter(mList);  
  50.         recyclerView.setAdapter(mAdapter);  
  51.   
  52.         setItemDecoration();  
  53.   
  54.         Button fab = (Button) findViewById(R.id.fab);  
  55.         fab.setOnClickListener(new View.OnClickListener() {  
  56.             @Override  
  57.             public void onClick(View view) {  
  58.   
  59.                 mAdapter.updateDataSet(mAdapter.getSelectedItem());  
  60.                 mAdapter.notifyDataSetChanged();  
  61.             }  
  62.         });  
  63.     }  
  64.   
  65.     //设置分割线  
  66.     private void setItemDecoration() {  
  67.         RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);  
  68.         recyclerView.addItemDecoration(itemDecoration);  
  69.     }  
  70.   
  71.     public class SelectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {  
  72.   
  73.         private ArrayList<String> mList = new ArrayList<>();  
  74.   
  75.         private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();  
  76.         private boolean mIsSelectable = false;  
  77.   
  78.   
  79.         public SelectAdapter(ArrayList<String> list) {  
  80.             if (list == null) {  
  81.                 throw new IllegalArgumentException("model Data must not be null");  
  82.             }  
  83.             mList = list;  
  84.         }  
  85.   
  86.         //更新adpter的数据和选择状态  
  87.         public void updateDataSet(ArrayList<String> list) {  
  88.             this.mList = list;  
  89.             mSelectedPositions = new SparseBooleanArray();  
  90.             ab.setTitle("已选择" + 0 + "项");  
  91.         }  
  92.   
  93.   
  94.         //获得选中条目的结果  
  95.         public ArrayList<String> getSelectedItem() {  
  96.             ArrayList<String> selectList = new ArrayList<>();  
  97.             for (int i = 0; i < mList.size(); i++) {  
  98.                 if (isItemChecked(i)) {  
  99.                     selectList.add(mList.get(i));  
  100.                 }  
  101.             }  
  102.             return selectList;  
  103.         }  
  104.   
  105.   
  106.         @Override  
  107.         public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {  
  108.             View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_item, viewGroup, false);  
  109.             return new ListItemViewHolder(itemView);  
  110.         }  
  111.   
  112.         //设置给定位置条目的选择状态  
  113.         private void setItemChecked(int position, boolean isChecked) {  
  114.             mSelectedPositions.put(position, isChecked);  
  115.         }  
  116.   
  117.         //根据位置判断条目是否选中  
  118.         private boolean isItemChecked(int position) {  
  119.             return mSelectedPositions.get(position);  
  120.         }  
  121.   
  122.         //根据位置判断条目是否可选  
  123.         private boolean isSelectable() {  
  124.             return mIsSelectable;  
  125.         }  
  126.         //设置给定位置条目的可选与否的状态  
  127.         private void setSelectable(boolean selectable) {  
  128.             mIsSelectable = selectable;  
  129.         }  
  130.   
  131.         //绑定界面,设置监听  
  132.         @Override  
  133.         public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int i) {  
  134.             //设置条目状态  
  135.             ((ListItemViewHolder) holder).mainTitle.setText(mList.get(i));  
  136.             ((ListItemViewHolder) holder).checkBox.setChecked(isItemChecked(i));  
  137.   
  138.             //checkBox的监听  
  139.             ((ListItemViewHolder) holder).checkBox.setOnClickListener(new View.OnClickListener() {  
  140.                 @Override  
  141.                 public void onClick(View v) {  
  142.                     if (isItemChecked(i)) {  
  143.                         setItemChecked(i, false);  
  144.                     } else {  
  145.                         setItemChecked(i, true);  
  146.                     }  
  147.                     ab.setTitle("已选择" + getSelectedItem().size() + "项");  
  148.                 }  
  149.             });  
  150.   
  151.             //条目view的监听  
  152.             ((ListItemViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {  
  153.                 @Override  
  154.                 public void onClick(View v) {  
  155.                     if (isItemChecked(i)) {  
  156.                         setItemChecked(i, false);  
  157.                     } else {  
  158.                         setItemChecked(i, true);  
  159.                     }  
  160.                     notifyItemChanged(i);  
  161.                     ab.setTitle("已选择" + getSelectedItem().size() + "项");  
  162.                 }  
  163.             });  
  164.   
  165.   
  166.         }  
  167.   
  168.         @Override  
  169.         public int getItemCount() {  
  170.             return mList == null ? 0 : mList.size();  
  171.         }  
  172.   
  173.         public class ListItemViewHolder extends RecyclerView.ViewHolder{  
  174.             //ViewHolder  
  175.             CheckBox checkBox;  
  176.             TextView mainTitle;  
  177.   
  178.             ListItemViewHolder(View view) {  
  179.                 super(view);  
  180.                 this.mainTitle = (TextView) view.findViewById(R.id.text);  
  181.                 this.checkBox = (CheckBox) view.findViewById(R.id.select_checkbox);  
  182.   
  183.             }  
  184.         }  
  185.     }  
  186.   
  187.   
  188.   
  189.   
  190.   
  191. }  
  192. </span>  

 

layout  activity_main.xml 的代码

[html]  view plain  copy
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:fitsSystemWindows="true"  
  8.     tools:context="com.enrique.recyclerview.MainActivity">  
  9.   
  10.     <android.support.design.widget.AppBarLayout  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:theme="@style/AppTheme.AppBarOverlay">  
  14.   
  15.         <android.support.v7.widget.Toolbar  
  16.             android:id="@+id/toolbar"  
  17.             android:layout_width="match_parent"  
  18.             android:layout_height="?attr/actionBarSize"  
  19.             android:background="?attr/colorPrimary"  
  20.             app:popupTheme="@style/AppTheme.PopupOverlay" />  
  21.   
  22.     </android.support.design.widget.AppBarLayout>  
  23.         <include layout="@layout/content_main" />  
  24.     <Button  
  25.         android:id="@+id/fab"  
  26.         android:text="更新"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_gravity="bottom|end"  
  30.         android:layout_margin="@dimen/fab_margin"  
  31.         android:src="@android:drawable/ic_dialog_email" />  
  32.   
  33.   
  34. </android.support.design.widget.CoordinatorLayout></span>  


content_main.xml
[html]  view plain  copy
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     app:layout_behavior="@string/appbar_scrolling_view_behavior"  
  12.     tools:context="com.enrique.recyclerview.MainActivity"  
  13.     tools:showIn="@layout/activity_main">  
  14.     <android.support.v7.widget.RecyclerView  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="match_parent"  
  17.         android:id="@+id/recyclerview"/>  
  18. </RelativeLayout></span>  


recyclerview_item.xml

[html]  view plain  copy
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal" android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:paddingTop="10dp"  
  6.     android:paddingBottom="10dp">  
  7.     <CheckBox  
  8.         android:layout_gravity="center_vertical"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:id="@+id/select_checkbox"/>  
  12. <TextView</span>  

[html]  view plain  copy
  1. <span style="font-size:14px;">    android:layout_width="wrap_content"  
  2.     android:layout_height="wrap_content"  
  3.     android:textAlignment="center"  
  4.     android:layout_gravity="center_vertical"  
  5.     android:textSize="15sp"  
  6.     android:text="条目"  
  7.     android:id="@+id/text"/>  
  8. </LinearLayout></span>  

[html]  view plain  copy
  1.   

        

RecyclerView出现已经很久了,使用面也是非常广的,现在就来用Recyclerview实现多选模式

基本思路是为Adapter增加一个Boolean集合

[java]  view plain  copy
  1. SparseBooleanArray mSelectedPositions = new SparseBooleanArray();  

 用来为Adapter 里的数据item设置标记,默认每个条目为false,选中的话就设置为true
[java]  view plain  copy
  1. private void setItemChecked(int position, boolean isChecked) {  
  2.             mSelectedPositions.put(position, isChecked);  
  3.         }  

通过条目位置得到该条目的Boolean值,就可以知道条目有没有选中这时可以在onbindview里设置chekbox的状态了,同时设置chekbox的监听

[java]  view plain  copy
  1. //根据位置判断条目是否选中  
  2.         private boolean isItemChecked(int position) {  
  3.             return mSelectedPositions.get(position);  
  4.         }  


[java]  view plain  copy
  1. //绑定界面,设置监听  
  2.         @Override  
  3.         public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int i) {  
  4.             //设置条目状态  
  5.             ((ListItemViewHolder) holder).mainTitle.setText(mList.get(i));  
  6.             ((ListItemViewHolder) holder).checkBox.setChecked(isItemChecked(i));  
  7.   
  8.             //checkBox的监听  
  9.             ((ListItemViewHolder) holder).checkBox.setOnClickListener(new View.OnClickListener() {  
  10.                 @Override  
  11.                 public void onClick(View v) {  
  12.                     if (isItemChecked(i)) {  
  13.                         setItemChecked(i, false);  
  14.                     } else {  
  15.                         setItemChecked(i, true);  
  16.                     }  
  17.                     ab.setTitle("已选择" + getSelectedItem().size() + "项");  
  18.                 }  
  19.             });  
  20.   
  21.             //条目view的监听  
  22.             ((ListItemViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {  
  23.                 @Override  
  24.                 public void onClick(View v) {  
  25.                     if (isItemChecked(i)) {  
  26.                         setItemChecked(i, false);  
  27.                     } else {  
  28.                         setItemChecked(i, true);  
  29.                     }  
  30.                     notifyItemChanged(i);  
  31.                     ab.setTitle("已选择" + getSelectedItem().size() + "项");  
  32.                 }  
  33.             });  
  34.   
  35.   
  36.         }  


最后返回选中的条目数据

[java]  view plain  copy
  1. //获得选中条目的结果  
  2.        public ArrayList<String> getSelectedItem() {  
  3.            ArrayList<String> selectList = new ArrayList<>();  
  4.            for (int i = 0; i < mList.size(); i++) {  
  5.                if (isItemChecked(i)) {  
  6.                    selectList.add(mList.get(i));  
  7.                }  
  8.            }  
  9.            return se  



完整代码如下

MainActivity


[java]  view plain  copy
  1. </pre><pre name="code" class="java"><span style="font-size:14px;">package com.enrique.recyclerview;  
  2.   
  3. import android.app.FragmentTransaction;  
  4. import android.os.Bundle;  
  5. import android.support.design.widget.FloatingActionButton;  
  6. import android.support.design.widget.Snackbar;  
  7.   
  8.   
  9. import android.support.v7.app.ActionBar;  
  10. import android.support.v7.app.AppCompatActivity;  
  11. import android.support.v7.widget.LinearLayoutManager;  
  12. import android.support.v7.widget.RecyclerView;  
  13. import android.support.v7.widget.Toolbar;  
  14. import android.util.SparseBooleanArray;  
  15. import android.view.LayoutInflater;  
  16. import android.view.View;  
  17. import android.view.Menu;  
  18. import android.view.MenuItem;  
  19. import android.view.ViewGroup;  
  20. import android.widget.Button;  
  21. import android.widget.CheckBox;  
  22. import android.widget.TextView;  
  23.   
  24. import java.util.ArrayList;  
  25.   
  26. public class MainActivity extends AppCompatActivity {  
  27.   
  28.     private ArrayList<String> mList = new ArrayList<>();  
  29.     RecyclerView recyclerView;  
  30.     LinearLayoutManager layoutManager;  
  31.     SelectAdapter mAdapter;  
  32.     ActionBar ab;  
  33.   
  34.     @Override  
  35.     protected void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.activity_main);  
  38.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);  
  39.         setSupportActionBar(toolbar);  
  40.         ab = getSupportActionBar();  
  41.   
  42.         for(int i = 0; i < 10 ; i++){  
  43.             mList.add("条目" + i );  
  44.         }  
  45.   
  46.         recyclerView = (RecyclerView) findViewById(R.id.recyclerview);  
  47.         layoutManager = new LinearLayoutManager(this);  
  48.         recyclerView.setLayoutManager(layoutManager);  
  49.         mAdapter = new SelectAdapter(mList);  
  50.         recyclerView.setAdapter(mAdapter);  
  51.   
  52.         setItemDecoration();  
  53.   
  54.         Button fab = (Button) findViewById(R.id.fab);  
  55.         fab.setOnClickListener(new View.OnClickListener() {  
  56.             @Override  
  57.             public void onClick(View view) {  
  58.   
  59.                 mAdapter.updateDataSet(mAdapter.getSelectedItem());  
  60.                 mAdapter.notifyDataSetChanged();  
  61.             }  
  62.         });  
  63.     }  
  64.   
  65.     //设置分割线  
  66.     private void setItemDecoration() {  
  67.         RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);  
  68.         recyclerView.addItemDecoration(itemDecoration);  
  69.     }  
  70.   
  71.     public class SelectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {  
  72.   
  73.         private ArrayList<String> mList = new ArrayList<>();  
  74.   
  75.         private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();  
  76.         private boolean mIsSelectable = false;  
  77.   
  78.   
  79.         public SelectAdapter(ArrayList<String> list) {  
  80.             if (list == null) {  
  81.                 throw new IllegalArgumentException("model Data must not be null");  
  82.             }  
  83.             mList = list;  
  84.         }  
  85.   
  86.         //更新adpter的数据和选择状态  
  87.         public void updateDataSet(ArrayList<String> list) {  
  88.             this.mList = list;  
  89.             mSelectedPositions = new SparseBooleanArray();  
  90.             ab.setTitle("已选择" + 0 + "项");  
  91.         }  
  92.   
  93.   
  94.         //获得选中条目的结果  
  95.         public ArrayList<String> getSelectedItem() {  
  96.             ArrayList<String> selectList = new ArrayList<>();  
  97.             for (int i = 0; i < mList.size(); i++) {  
  98.                 if (isItemChecked(i)) {  
  99.                     selectList.add(mList.get(i));  
  100.                 }  
  101.             }  
  102.             return selectList;  
  103.         }  
  104.   
  105.   
  106.         @Override  
  107.         public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {  
  108.             View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_item, viewGroup, false);  
  109.             return new ListItemViewHolder(itemView);  
  110.         }  
  111.   
  112.         //设置给定位置条目的选择状态  
  113.         private void setItemChecked(int position, boolean isChecked) {  
  114.             mSelectedPositions.put(position, isChecked);  
  115.         }  
  116.   
  117.         //根据位置判断条目是否选中  
  118.         private boolean isItemChecked(int position) {  
  119.             return mSelectedPositions.get(position);  
  120.         }  
  121.   
  122.         //根据位置判断条目是否可选  
  123.         private boolean isSelectable() {  
  124.             return mIsSelectable;  
  125.         }  
  126.         //设置给定位置条目的可选与否的状态  
  127.         private void setSelectable(boolean selectable) {  
  128.             mIsSelectable = selectable;  
  129.         }  
  130.   
  131.         //绑定界面,设置监听  
  132.         @Override  
  133.         public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int i) {  
  134.             //设置条目状态  
  135.             ((ListItemViewHolder) holder).mainTitle.setText(mList.get(i));  
  136.             ((ListItemViewHolder) holder).checkBox.setChecked(isItemChecked(i));  
  137.   
  138.             //checkBox的监听  
  139.             ((ListItemViewHolder) holder).checkBox.setOnClickListener(new View.OnClickListener() {  
  140.                 @Override  
  141.                 public void onClick(View v) {  
  142.                     if (isItemChecked(i)) {  
  143.                         setItemChecked(i, false);  
  144.                     } else {  
  145.                         setItemChecked(i, true);  
  146.                     }  
  147.                     ab.setTitle("已选择" + getSelectedItem().size() + "项");  
  148.                 }  
  149.             });  
  150.   
  151.             //条目view的监听  
  152.             ((ListItemViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {  
  153.                 @Override  
  154.                 public void onClick(View v) {  
  155.                     if (isItemChecked(i)) {  
  156.                         setItemChecked(i, false);  
  157.                     } else {  
  158.                         setItemChecked(i, true);  
  159.                     }  
  160.                     notifyItemChanged(i);  
  161.                     ab.setTitle("已选择" + getSelectedItem().size() + "项");  
  162.                 }  
  163.             });  
  164.   
  165.   
  166.         }  
  167.   
  168.         @Override  
  169.         public int getItemCount() {  
  170.             return mList == null ? 0 : mList.size();  
  171.         }  
  172.   
  173.         public class ListItemViewHolder extends RecyclerView.ViewHolder{  
  174.             //ViewHolder  
  175.             CheckBox checkBox;  
  176.             TextView mainTitle;  
  177.   
  178.             ListItemViewHolder(View view) {  
  179.                 super(view);  
  180.                 this.mainTitle = (TextView) view.findViewById(R.id.text);  
  181.                 this.checkBox = (CheckBox) view.findViewById(R.id.select_checkbox);  
  182.   
  183.             }  
  184.         }  
  185.     }  
  186.   
  187.   
  188.   
  189.   
  190.   
  191. }  
  192. </span>  

 
 

layout  activity_main.xml 的代码

[html]  view plain  copy
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:fitsSystemWindows="true"  
  8.     tools:context="com.enrique.recyclerview.MainActivity">  
  9.   
  10.     <android.support.design.widget.AppBarLayout  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:theme="@style/AppTheme.AppBarOverlay">  
  14.   
  15.         <android.support.v7.widget.Toolbar  
  16.             android:id="@+id/toolbar"  
  17.             android:layout_width="match_parent"  
  18.             android:layout_height="?attr/actionBarSize"  
  19.             android:background="?attr/colorPrimary"  
  20.             app:popupTheme="@style/AppTheme.PopupOverlay" />  
  21.   
  22.     </android.support.design.widget.AppBarLayout>  
  23.         <include layout="@layout/content_main" />  
  24.     <Button  
  25.         android:id="@+id/fab"  
  26.         android:text="更新"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_gravity="bottom|end"  
  30.         android:layout_margin="@dimen/fab_margin"  
  31.         android:src="@android:drawable/ic_dialog_email" />  
  32.   
  33.   
  34. </android.support.design.widget.CoordinatorLayout></span>  


content_main.xml
[html]  view plain  copy
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     app:layout_behavior="@string/appbar_scrolling_view_behavior"  
  12.     tools:context="com.enrique.recyclerview.MainActivity"  
  13.     tools:showIn="@layout/activity_main">  
  14.     <android.support.v7.widget.RecyclerView  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="match_parent"  
  17.         android:id="@+id/recyclerview"/>  
  18. </RelativeLayout></span>  


recyclerview_item.xml

[html]  view plain  copy
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal" android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:paddingTop="10dp"  
  6.     android:paddingBottom="10dp">  
  7.     <CheckBox  
  8.         android:layout_gravity="center_vertical"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:id="@+id/select_checkbox"/>  
  12. <TextView</span>  

[html]  view plain  copy
  1. <span style="font-size:14px;">    android:layout_width="wrap_content"  
  2.     android:layout_height="wrap_content"  
  3.     android:textAlignment="center"  
  4.     android:layout_gravity="center_vertical"  
  5.     android:textSize="15sp"  
  6.     android:text="条目"  
  7.     android:id="@+id/text"/>  
  8. </LinearLayout></span>  

[html]  view plain  copy
  1.   

        


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值