UI控件RecyclerView浅析

RecyclerView是Android5.0新加入的用于替代ListView的UI控件,该控件与适配器Adapter、数据源DataSet的关系如下图所示:

UI控件RecyclerView浅析_第1张图片

RecyclerView的特性

相比于ListView,RecyclerView多了一个LayoutManager,Adapter也有所不同,下面将RecyclerView的新特性罗列如下:

  • 必须指定Adapter和LayoutManager;
    • 1、需要继承类RecyclerView.Adapter,重写下列方法:
      • onCreateViewHolder(ViewGroup parent, int viewType);
      • onBindViewHolder(ViewHolder holder, int position);
      • getItemCount();
    • 2、三种内置的LayoutManager
      • LinearLayoutManager;
      • GridLayoutManager;
      • StaggeredGridLayoutManager;
  • 必须使用ViewHolder模式,以便复用每一个Item;
  • 简化了大量数据的显示和处理;
    • 1、提供了LayoutManager,用于定位Item Views,用于决定何时重用不再可见的Item Views;
    • 2、提供了默认的动画,用于Items的添加和删除等操作;
  • 可以自定义LayoutManager和动画;
    • 1、继承类RecyclerView.LayoutManager;
    • 2、继承类RecyclerView.ItemAnimator,重写方法RecyclerView.setItemAnimator();

在Eclipse中配置并使用RecyclerView

在Android官方文档中仅介绍了如何在Android Studio使用RecyclerView,并没有介绍如何在Eclipse中使用,其具体配置方法与同样是在Android5.0版本中引入的系统控件CardView相仿,具体步骤请参阅我的博文《在Eclipse中配置并使用UI控件CardView》。

XML布局

整体布局

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" >

</android.support.v7.widget.RecyclerView>

注意:RecyclerView需使用全限定类名。

每个Item的布局

<?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="match_parent" android:orientation="vertical" android:padding="8dp" >

    <TextView  android:id="@+id/textview_1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textColor="@android:color/holo_green_dark" android:textSize="21sp" />

    <TextView  android:id="@+id/textview_2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textColor="@android:color/holo_orange_light" android:textSize="15sp" />

</LinearLayout>

每个Item纵向布置了两个TextView。

定义DataSet

为了给每个Item装入数据,应定义一个类存放相应数据:

package com.test.recyclerview_test2;

public class ItemObject {
    private String mText_1;

    public String getText_1() {
        return mText_1;
    }

    public ItemObject(String text_1, String text_2) {
        super();
        mText_1 = text_1;
        mText_2 = text_2;
    }

    public ItemObject() {
        super();
    }

    public void setText_1(String text_1) {
        mText_1 = text_1;
    }

    public String getText_2() {
        return mText_2;
    }

    public void setText_2(String text_2) {
        mText_2 = text_2;
    }

    private String mText_2;

}

定义Adapter

继承RecyclerView.Adapter,并重写该类中的3个方法;由于RecyclerView要求必须定义ViewHolder,故还需要在定义的Adapter中定义一个继承了RecyclerView.ViewHolder的内部类。具体代码示例如下所示:

//自定义RecyclerView的Adapter,继承于RecyclerView.Adapter<MyViewHolder>
public class CustomRecyclerViewAdapter extends Adapter<CustomRecyclerViewAdapter.MyViewHolder> {

    private ItemObject[] mItemObjects;

    private Context mContext;

    public CustomRecyclerViewAdapter(ItemObject[] itemObjects, Context context) {
        super();
        mItemObjects = itemObjects;
        mContext = context;
    }

    public CustomRecyclerViewAdapter() {
        super();
    }

    // 必须定义ViewHolder,继承于RecyclerView.ViewHolder
    public static class MyViewHolder extends ViewHolder {
        private TextView mTextView_1;
        private TextView mTextView_2;

        public MyViewHolder(View view) {
            super(view);
            // TODO Auto-generated constructor stub
            mTextView_1 = (TextView) view.findViewById(R.id.textview_1);
            mTextView_2 = (TextView) view.findViewById(R.id.textview_2);
        }

    }

    // 返回Item的个数
    @Override
    public int getItemCount() {
        // TODO Auto-generated method stub
        return mItemObjects.length;
    }

    // 绑定ViewHolder与DataSet
    @Override
    public void onBindViewHolder(MyViewHolder myViewHolder, int position) {
        // TODO Auto-generated method stub
        myViewHolder.mTextView_1.setText(mItemObjects[position].getText_1());
        myViewHolder.mTextView_2.setText(mItemObjects[position].getText_2());

    }

    // 创建ViewHolder,绑定ViewHolder与Item的XML布局文件
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
        // TODO Auto-generated method stub
        View _view = LayoutInflater.from(mContext).inflate(R.layout.item, arg0,
                false);
        MyViewHolder _myViewHolder = new MyViewHolder(_view);
        return _myViewHolder;
    }

}

activity逻辑实现

与ListView相仿,设置RecyclerView为如下步骤:

  1. 设置数据源;
  2. 创建LayoutManager,为RecyclerView设置LayoutManager;
  3. 绑定数据源和Adapter;
  4. 为RecyclerView设置Adapter;

可见,设置RecyclerView比ListView多了第二步,就是设置LayoutManager。

具体示例代码如下:

public class MainActivity extends Activity {

    private RecyclerView mRecyclerView;
    private LayoutManager mLayoutManager;
    private CustomRecyclerViewAdapter mCustomRecyclerViewAdapter;
    private ItemObject[] mItemObjects = new ItemObject[100];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        // 创建LayoutManager
        mLayoutManager = new LinearLayoutManager(this);
        // 为RecyclerView设置LayoutManager
        mRecyclerView.setLayoutManager(mLayoutManager);

        // 设置数据源
        for (int i = 0; i < mItemObjects.length; ++i) {
            ItemObject _itemObject = new ItemObject();
            _itemObject.setText_1("Title Of Item No." + (i + 1));
            _itemObject.setText_2("Content Of Item No." + (i + 1));
            mItemObjects[i] = _itemObject;
        }
        // 绑定数据源和Adapter
        mCustomRecyclerViewAdapter = new CustomRecyclerViewAdapter(
                mItemObjects, this);

        // 为RecyclerView设置Adapter
        mRecyclerView.setAdapter(mCustomRecyclerViewAdapter);

    }
}

demo的实现效果

UI控件RecyclerView浅析_第2张图片UI控件RecyclerView浅析_第3张图片

你可能感兴趣的:(Adapter,viewholder,Recycler)