首页 > 代码库 > Android学习笔记--RecyclerView控件的使用

Android学习笔记--RecyclerView控件的使用

首先我们要依赖 

compile ‘com.android.support:recyclerview-v7:25.0.0‘
之后才可以使用
recyclerview

XMl文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context="com.wuxianedu.alistshengjiban.MainActivity">
 8 
 9     <android.support.v7.widget.RecyclerView
10         android:layout_width="match_parent" android:id="@+id/rv_id"
11         android:layout_height="match_parent" android:layout_below="@+id/buju" />
12     <LinearLayout android:id="@+id/buju"  android:orientation="horizontal"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content">
15         <Button android:id="@+id/but_id" android:text="网格视图"
16             android:layout_width="wrap_content"
17             android:layout_height="wrap_content" />
18         <Button android:id="@+id/but_xianxing" android:text="线性布局"
19             android:layout_width="wrap_content"
20             android:layout_height="wrap_content" />
21         <Button android:id="@+id/but_tianjia" android:text="添加ITEM"
22             android:layout_width="wrap_content"
23             android:layout_height="wrap_content" />
24     </LinearLayout>
25 
26 </RelativeLayout>

 

 MainActivity

 1 package com.wuxianedu.alistshengjiban;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.support.v7.widget.LinearLayoutManager;
 6 import android.support.v7.widget.RecyclerView;
 7 import android.support.v7.widget.StaggeredGridLayoutManager;
 8 import android.view.View;
 9 import android.widget.Button;
10 import android.widget.RemoteViews;
11 import android.widget.Toast;
12 
13 import java.util.ArrayList;
14 import java.util.List;
15 
16 public class MainActivity extends AppCompatActivity implements View.OnClickListener{
17     private RecyclerView recyclerView;
18     private MyAdaoter myAdaoter;
19 
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_main);
24         recyclerView = (RecyclerView) findViewById(R.id.rv_id);
25         findViewById(R.id.but_id).setOnClickListener(this);
26         findViewById(R.id.but_xianxing).setOnClickListener(this);
27         findViewById(R.id.but_tianjia).setOnClickListener(this);
28 
29         final List<String> list = new ArrayList<>();
30         //循环赋值
31         for (int i = 0; i < 10; i++) {
32             if(i%2 == 0){
33                 list.add("我"+ i );
34             }else {
35                 list.add(i+"");
36             }
37         }
38         //new一个Adapter
39         myAdaoter = new MyAdaoter(list,this);
40         //这里是接口回调,因为RecyclerView没有点击事件,所有需要自己写接口
41         myAdaoter.setOnItem(new MyAdaoter.IsetOnc() {
42             //点击事件回调
43             @Override
44             public void setOnClickList(int xiabiao) {
45                 //回调过来的下标
46                 Toast.makeText(MainActivity.this, "你点击了"+xiabiao, Toast.LENGTH_SHORT).show();
47             }
48             //长按事件回调
49             @Override
50             public void setOnLongClickList(int xiabiao) {
51                 //移除数据
52                 myAdaoter.recelm(xiabiao);
53             }
54         });
55         recyclerView.setAdapter(myAdaoter);
56 
57 
58         //获取布局管理器
59        recyclerView.setLayoutManager(new LinearLayoutManager(this));
60         //1.context  2 布局方向,3 是否倒序
61         //recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
62         //recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
63         //瀑布流
64         recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL_LIST));
65 
66     }
67 
68     @Override
69     public void onClick(View v) {
70         switch (v.getId()){
71             case R.id.but_id://网格布局
72                 recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
73                 break;
74             case R.id.but_xianxing://线性布局
75                 recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
76                 break;
77             case R.id.but_tianjia://添加数据
78                 myAdaoter.addImte("我是惠");
79                 break;
80         }
81     }
82 }
MyAdaoter代码

  1 package com.wuxianedu.alistshengjiban;
  2 
  3 import android.content.Context;
  4 import android.support.v7.widget.RecyclerView;
  5 import android.view.LayoutInflater;
  6 import android.view.View;
  7 import android.view.ViewGroup;
  8 import android.widget.TextView;
  9 import android.widget.Toast;
 10 
 11 import java.util.List;
 12 
 13 /**
 14  * Created by Administrator on 2016/11/15.
 15  */
 16 public class MyAdaoter extends RecyclerView.Adapter<MyAdaoter.ViewHolder> {
 17     private List<String> list;
 18     private Context context;
 19     private IsetOnc isetonc;
 20 
 21     /**
 22      * 删除集合
 23      */
 24     public  void recelm(int dizhi){
 25         list.remove(dizhi);
 26         notifyItemRemoved(dizhi);
 27         notifyItemRangeChanged(dizhi,list.size()-1);
 28     }
 29 
 30     /**
 31      * 添加子条目
 32      * @param str
 33      */
 34     public void addImte(String str){
 35         list.add(4,str);
 36         notifyItemInserted(4);
 37         notifyItemRangeChanged(4,list.size()-4);
 38     }
 39 
 40     public MyAdaoter(List<String> list,Context context) {
 41         this.list = list;
 42         this.context = context;
 43     }
 44 
 45     @Override
 46     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
 47         View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_adapter,parent,false);
 48         ViewHolder viewHolder = new ViewHolder(view);
 49         return viewHolder;
 50     }
 51     /**
 52      * 类似GetView
 53      * @param holder
 54      * @param position
 55      */
 56     @Override
 57     public void onBindViewHolder(ViewHolder holder, int position) {
 58         holder.textView.setText(list.get(position));
 59         //setTag当前的TAG
 60         holder.itemView.setTag(position);
 61     }
 62     /**
 63      * 长度
 64      * @return
 65      */
 66     @Override
 67     public int getItemCount() {
 68         return list.size();
 69     }
 70     /**
 71      * 初始化组件
 72      */
 73     class ViewHolder extends RecyclerView.ViewHolder{
 74         TextView textView;
 75         public ViewHolder(final View itemView) {
 76             super(itemView);
 77             textView = (TextView) itemView.findViewById(R.id.tv_id);
 78             itemView.setOnClickListener(new View.OnClickListener() {
 79                 @Override
 80                 public void onClick(View v) {
 81                     if(isetonc != null){
 82                         //点击id时候判断如果isetonc不为空的时候再给接口赋值,这样就不会出现空指针了
 83                         //获取之前setTag的值,给接口赋值
 84                         isetonc.setOnClickList((Integer) itemView.getTag());
 85                     }
 86                 }
 87             });
 88             itemView.setOnLongClickListener(new View.OnLongClickListener() {
 89                 @Override
 90                 public boolean onLongClick(View v) {
 91                     if(isetonc != null){
 92                         //点击id时候判断如果isetonc不为空的时候再给接口赋值,这样就不会出现空指针了
 93                         isetonc.setOnLongClickList((Integer) itemView.getTag());
 94                     }
 95                     return true;
 96                 }
 97             });
 98         }
 99     }
100     /**
101      * 给接口赋值
102      * @param isetonc
103      */
104     public void setOnItem(IsetOnc isetonc){
105         this.isetonc = isetonc;
106     }
107     interface IsetOnc{
108         //点击事件
109         void setOnClickList(int xiabiao);
110         //长按事件
111         void setOnLongClickList(int xiabiao);
112     }
113 }

今天有事,就先写这些,以后在补

Android学习笔记--RecyclerView控件的使用