RecyclerView用法詳解

使用RecyclerView首先需要導入相應的架包:

compile 'com.android.support:recyclerview-v7:24.0.0-alpha2'


主界面:

<android.support.v7.widget.RecyclerView
        android:id="@+id/main_rv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

Item:

<?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="wrap_content"
    android:background="@android:color/holo_red_dark"
    android:orientation="vertical"
    android:gravity="center"
    tools:context="com.wanglun.recyclerviewtest.MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher"
        />

    <TextView
        android:id="@+id/item_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        />

</LinearLayout>

Adapter:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyAdapter> {

    private Context context;

    public RecyclerViewAdapter(Context context){
        this.context=context;
    }
    

    @Override
    public MyAdapter onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.i("woyaokk","onCreateViewHolder======================");
        MyAdapter myAdapter;
        myAdapter=new MyAdapter(LayoutInflater.from(context).inflate(R.layout.recycler_item,parent,false));
        return myAdapter;
    }

    @Override
    public void onBindViewHolder(MyAdapter holder, int position) {
        Log.i("woyaokk", "onBindViewHolder======================" + position);
        holder.item_tv.setText("第" + position + "行");
    }

    @Override
    public int getItemCount() {
        Log.i("woyaokk","getItemCount======================");
        return 40;
    }

    public class MyAdapter extends RecyclerView.ViewHolder {

        TextView item_tv;

        public MyAdapter(View itemView) {
            super(itemView);
            Log.i("woyaokk", "MyAdapter======================");
            item_tv= (TextView) itemView.findViewById(R.id.item_tv);
        }
    }
}
Activity:

        main_rv= (RecyclerView) findViewById(R.id.main_rv);
        main_rv.setLayoutManager(new LinearLayoutManager(this));
        main_rv.setAdapter(new RecyclerViewAdapter(this));

以上就是RecyclerView的基本使用方法。


詳解:

1、分割線:

RecyclerView中與ListView不同,沒有默認的分割線,需要手動添加,可以使用網上一個開源項目:


2、RecyclerView.LayoutManager,這是一個抽象類,系統提供了3個實現類:

LinearLayoutManager 線性管理器,支持橫向、縱向。
GridLayoutManager 網格佈局管理器
StaggeredGridLayoutManager 瀑布就式佈局管理器

使用方法類型於:

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
或者

main_rv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.HORIZONTAL));

3、ItemAnimator動畫

RecyclerView刷新列表提供了notifyDataSetChanged(),還有添加 notifyItemInserted(position) 指定位置添加一個item和刪除 notifyItemRemoved(position)

RecyclerView增加或者刪除item時,可以有相應的動畫,默認的效果使用的還是:

main_rv.setItemAnimator(new DefaultItemAnimator());
也可以使用itemAnimator實現想要的動畫

4、RecyclerView中不包含點擊事件,需要我們手動添加點擊事件的回調或者在item中直接實現點擊事件

5、上拉加載下拉刷新https://github.com/kodyan/SwipeRefreshLayout






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章