RecyclerView的簡單使用

自從Android 5.0 以後,google 推出了一個 RecyclerView 控件,他是support-v7包中的新組件,是一個強大的滑動組件,與經典的ListView相比,一樣擁有item回收複用的功能,RecyclerView至關因而ListView的升級版。php

RecyclerView 封裝了 ViewHolder 的回收複用,也就是說RecyclerView標準化了 ViewHolder,編寫Adapter面向的是 ViewHolder 而不是 View 了,複用的邏輯被封裝了,寫起來更加簡單。java

RecyclerView提供一種插拔式的體驗,高度的解耦,異常的靈活,針對一個Item的顯示RecyclerView專門抽取了相應的類,來控制Item的顯示,使其的擴展性特別強。android

引入RecyclerVIew

compile 'com.android.support:recyclerview-v7:25.1.0'
複製代碼

建立佈局文件

主佈局文件
/*activity_main.xml*/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.manu.mrecyclerview.MainActivity">
    <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>

複製代碼
Item佈局文件
/*item.xml*/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp">
    <TextView android:id="@+id/tv_recycle" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="data" android:background="#cac3c3" android:padding="10dp" android:textSize="20sp"/>
</LinearLayout>

複製代碼

建立Adapter

RecyclerView的Adapter要比ListView的Adapter設置起來稍微複雜一點,這也是RecyclerView高度解耦的體現,雖然代碼複雜一點,但擴展性很好,下面介紹一下實現RecyclerView的Adapter的三個方法:bash

onCreateViewHolder()

該方法主要是爲每一個Item加載一個View,可是該方法返回的是一個ViewHolder,該方法吧View直接封裝的ViewHolder中,而後咱們面向的是ViewHolder這個實例,這個ViewHolder也由本身編寫,但不用像ListView調用convertView.setTag(vh)和convertView.getTag()了。dom

onBindViewHolder()

該方法主要用來把數據綁定在View上,直接提供一個ViewHolder而不是convertView。ide

getItemCount()

該方法返回選項總數。佈局

Adapter代碼
/** * Created by jzman on 2017/5/13 0013. */
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.DataViewHolder>{
    private Context mContext;
    private ArrayList<String> mList;

    public RvAdapter() {}

    public RvAdapter(Context mContext, ArrayList<String> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    //用於建立ViewHolder
    @Override
    public DataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item,null);
        //使用代碼設置寬高(xml佈局設置無效時)
        view.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        DataViewHolder holder = new DataViewHolder(view);
        return holder;
    }
    //綁定數據
    @Override
    public void onBindViewHolder(DataViewHolder holder, int position) {
        holder.tv_data.setText(mList.get(position));
    }
    //數據總數
    @Override
    public int getItemCount() {
        return mList.size();
    }

    //建立ViewHolder
    public static class DataViewHolder extends RecyclerView.ViewHolder{
        TextView tv_data;
        public DataViewHolder(View itemView) {
            super(itemView);
            tv_data = (TextView) itemView.findViewById(R.id.tv_recycle);
        }
    }
複製代碼

使用StaggeredGridLayoutManager管理器時,Adapter參考以下:學習

/** * Created by jzman on 2017/5/13 0013. * RecycleView的Adapter */
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.DataViewHolder>{
    private Context mContext;
    private RecyclerView recyclerView;
    private ArrayList<String> mList;
    private ArrayList<Integer> mHeight;

    public RvAdapter() {}

    public RvAdapter(Context mContext, ArrayList<String> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    /** * 初始化每一個Item的高度(瀑布流效果) * @return */
    public ArrayList<Integer> initHeight(){
        mHeight = new ArrayList<>();
        for (int i=0;i<mList.size();i++){
            mHeight.add((int) (Math.random()*300)+200);
        }
        return mHeight;
    }

    /** * 用於建立ViewHolder * @param parent * @param viewType * @return */
    @Override
    public DataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item,null);
        //使用代碼設置寬高(xml佈局設置無效時)
        view.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        DataViewHolder holder = new DataViewHolder(view);
        return holder;
    }

    /** * 綁定數據 * @param holder * @param position */
    @Override
    public void onBindViewHolder(DataViewHolder holder, int position) {
        //設置每一個Item的高度
        ViewGroup.LayoutParams h = holder.tv_data.getLayoutParams();
        h.height = mHeight.get(position);
        holder.tv_data.setText(mList.get(position));
    }

    /** * 選項總數 * @return */
    @Override
    public int getItemCount() {
        return mList.size();
    }

    /** * 建立ViewHolder */
    public static class DataViewHolder extends RecyclerView.ViewHolder{
        TextView tv_data;
        public DataViewHolder(View itemView) {
            super(itemView);
            tv_data = (TextView) itemView.findViewById(R.id.tv_recycle);
        }
    }
    /** * 將RecycleView附加到Adapter上 */
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView= recyclerView;
        //初始化每一個Item的高度
        initHeight();
    }
    /** * 將RecycleView從Adapter解除 */
    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }
}
複製代碼

MainActivity

/** * Created by jzman on 2017/5/13 0013. */
public class MainActivity extends AppCompatActivity {
    private RecyclerView rv;
    RvAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rv = (RecyclerView) findViewById(R.id.rv);
        //設置佈局管理器
        rv.setLayoutManager(new LinearLayoutManager(this));//線性
// rv.setLayoutManager(new GridLayoutManager(this,4));//線性
// rv.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL));//線性
        adapter = new RvAdapter(this,initData());
        rv.setAdapter(adapter);
    }
    public static ArrayList<String> initData(){
        ArrayList<String> arrayList = new ArrayList<>();
        for (int i=0;i<50;i++){
            arrayList.add("第"+i+"條數據");
        }
        return arrayList;
    }
}
複製代碼

顯示效果

LinearLayoutManager GridLayoutManager StaggeredGridLayoutManager
image
image

若是以爲對本身有幫助,能夠關注公衆號:jzman-blog,一塊兒交流學習。 this

jzman-blog.jpg
相關文章
相關標籤/搜索