RecyclerView是什么
从Android 5.0开始,谷歌公司推出了一个用于大量数据展示的新控件RecylerView,可以用来代替传统的ListView,更加强大和灵活。RecyclerView的官方定义如下:
灵活的视图,用于为大型数据集提供有限的窗口。
从定义可以看出,柔性(可扩展性)是RecyclerView的特点。
RecyclerView是支持-V7包中的新组件,是一个强大的滑动组件,与经典的ListView控件相比,自身就拥有View回收复用的功能,这一点从它的名字Recyclerview即回收视图也可以看出。
RecyclerView的优点
RecyclerView并不会完全替代ListView(这点从ListView没有被标记为@Deprecated可以看出),两者的使用场景不一样。但是RecyclerView的出现会让很多开源项目被废弃,例如横向滚动的ListView,横向滚动的GridView,瀑布流控件,因为RecyclerView能够实现所有这些功能。
比如:有一个需求是屏幕竖着的时候的显示形式是ListView,屏幕横着的时候的显示形式是2列的GridView,此时如果用RecyclerView,则通过设置LayoutManager 一行代码实现替换。
RecylerView相对于ListView控件的优点罗列如下:
RecyclerView 封装了viewholder的回收复用,也就是说RecyclerView 标准化了ViewHolder,编写适配器面向的是ViewHolder而不再是View了,复用的逻辑被封装了,写起来更加简单。
直接省去了listview中convertView .setTag(保持器)和convertView.getTag()这些繁琐的步骤。
提供了一种插拔式的体验,高度的解耦,异常的灵活,针对一个项目的显示RecyclerView抽取专门出了相应的类,来控制项目的显示,使其的扩展性非常强。
设置布局管理器以控制项目的布局方式,横向,竖向以及瀑布流方式
例如:你想控制横向或者纵向滑动列表效果可以通过LinearLayoutManager这个类来进行控制(与GridView效果对应的是GridLayoutManager,与瀑布流对应的还StaggeredGridLayoutManager等)。也就是说RecyclerView不再拘泥于ListView中的线性展示方式,它也可以实现的GridView的效果等多种效果。
可设置项目的间隔样式(可绘制)
通过继承RecyclerView的ItemDecoration这个类,然后针对自己的业务需求去书写代码。
可以控制项目增删的动画,可以通过ItemAnimator这个类进行控制,当然针对增删的动画,RecyclerView有其自己默认的实现。
但是关于项目的点击和长按事件,需要用户自己去实现。
基本使用
引用
在的build.gradle中文件引入该类。
1
| compile 'com.android.support:recyclerview-v7:25.3.1'
|
布局
活动布局文件activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.plain.recyclerviewtest.MainActivity">
<android.support.v7.widget.RecyclerView android:id="@+id/recycle_view" android:layout_width="match_parent" android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
|
Item的布局文件item_xm.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="30sp" android:text="1" android:padding="10dp"/>
</LinearLayout>
|
创建适配器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| package com.plain.recyclerviewtest;
import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ItemHolder> {
private final Context mContext; private final List<String> mList;
public RecyclerViewAdapter(Context context, List<String> list) { this.mList = list; this.mContext = context; }
@Override public RecyclerViewAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(mContext).inflate(R.layout.item_xm, parent, false); return new ItemHolder(v); }
@Override public void onBindViewHolder(RecyclerViewAdapter.ItemHolder holder, int position) { String data = mList.get(position);
holder.setData(data); }
@Override public int getItemCount() { if (mList != null) { return mList.size(); } return 0; }
public class ItemHolder extends RecyclerView.ViewHolder {
private final TextView mText;
public ItemHolder(View itemView) { super(itemView);
mText = (TextView) itemView.findViewById(R.id.text); }
public void setData(String data) { mText.setText(data); } } }
|
设置RecyclerView
创建完Adapter,接着对RecyclerView进行设置,一般来说,需要为RecyclerView进行四大设置,也就是后文说的四大组成:
- Layout Manager(必选)
- Adapter(必选)
- Item Decoration(可选,默认为空)
- Item Animator(可选,默认为DefaultItemAnimator)
如果要实现ListView的效果,只需要设置Adapter和Layout Manager
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| package com.plain.recyclerviewtest;
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import java.util.ArrayList; import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recycle_view);
initDatas(); }
private void initDatas() {
List<String> datas = new ArrayList<>();
for (int i = 0;i <= 50;i ++){ datas.add(" " + i); }
LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); mRecyclerView.setLayoutManager(manager);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this,datas); mRecyclerView.setAdapter(adapter); } }
|
参考
未来还会继续学习RecyclerView的更多用法