RecycleView基本使用

RecyclerView是ListView的升級版,它具備了更好的性能,且更容易使用。和ListView一樣,RecyclerView是用來顯示大量數據的容器,并通過復用有限數量的View,來提高滾動時的性能。當你的視圖上的元素經常動態的且有規律的改變時候,可以使用RecyclerView控件。

與ListView不同的是RecyclerView現在不再負責布局,只專注于復用機制,布局交由LayoutManager來管理。
RecyclerView仍然通過Adapter來獲取需要顯示的對象。

RecyclerView.png

1 布局

SwipeRefreshLayout 作下拉刷新用

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swip_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycle_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>

2 代碼

1 創建DataBean

public class DataBean {

    public int iconRes;
    public String des;

    public DataBean(int iconRes, String des) {
        this.iconRes = iconRes;
        this.des = des;
    }
}

2 創建Adapter與Viewhodler

/**
 * Created by cynic on 2016/5/27.
 */
public class ListAdapter extends    RecyclerView.Adapter<ListAdapter.ListHolder> {


    private Context context;
    private List<DataBean> datas;

    public ListAdapter(Context context, List<DataBean> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public ListHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = View.inflate(context, R.layout.item_list, null);
        return new ListHolder(view);
    }

    @Override
    public void onBindViewHolder(ListHolder holder, int position) {
        holder.refreshDataUI(datas.get(position));//數據顯示
    }

    @Override
    public int getItemCount() {
        if (datas != null) {
            return datas.size();
        }
        return 0;
    }

    public class ListHolder extends RecyclerView.ViewHolder {

        private ImageView iv;
        private TextView tv;

        public ListHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.item_list_iv);
            tv = (TextView) itemView.findViewById(R.id.item_list_tv);
        }

        public void refreshDataUI(DataBean dataBean) {
            iv.setImageResource(dataBean.iconRes);
            tv.setText(dataBean.des);
        }
    }

}

3 給recycleview設置adapter和layoutmanager adapter

//listview 垂直顯示
private void initRecycleListV() {
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getBaseContext(),
            LinearLayoutManager.VERTICAL, false);
    mRecycleView.setLayoutManager(layoutManager);
    ListAdapter adapter = new ListAdapter(getBaseContext(), datas);
    mRecycleView.setAdapter(adapter);
}
//listview 水平顯示
private void initRecycleListH() {
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getBaseContext(),
            LinearLayoutManager.HORIZONTAL, false);
    mRecycleView.setLayoutManager(layoutManager);
    ListAdapter adapter = new ListAdapter(getBaseContext(), datas);
    mRecycleView.setAdapter(adapter);
}
//gridview 垂直顯示
private void initRecycleGridV() {
    RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getBaseContext(), 2);
    mRecycleView.setLayoutManager(layoutManager);
    ListAdapter adapter = new ListAdapter(getBaseContext(), datas);
    mRecycleView.setAdapter(adapter);
}
//gridview 水平顯示
private void initRecycleGridH() {
    RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getBaseContext(), 2,
            LinearLayoutManager.HORIZONTAL, false);
    mRecycleView.setLayoutManager(layoutManager);
    ListAdapter adapter = new ListAdapter(getBaseContext(), datas);
    mRecycleView.setAdapter(adapter);
}

5 瀑布流

1 item布局

CardView 包裹的內容以卡片顯示,字布局為LinearLayout包裹時,設置layout_width="match_parent"
不起作用,不知為何。其Adapter、ViewHolder與上一個相似,這里不在記錄。

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp">

        <ImageView
            android:id="@+id/item_straggered_iv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/item_straggered_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/item_straggered_iv"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:text="gagaga"
            android:textColor="#212121"
            android:textSize="16sp" />
    </RelativeLayout>
</android.support.v7.widget.CardView>
代碼設置
//瀑布流 垂直顯示
private void initRecycleStraggeredV() {
    RecyclerView.LayoutManager layoutManager = new StaggeredGridLayoutManager(2,LinearLayoutManager.VERTICAL);
    mRecycleView.setLayoutManager(layoutManager);
    StraggeredAdapter adapter = new StraggeredAdapter(getBaseContext(), mStraggeredDatas);
    mRecycleView.setAdapter(adapter);
}
//瀑布流 水平顯示
private void initRecycleStraggeredH() {
    RecyclerView.LayoutManager layoutManager = new StaggeredGridLayoutManager(2,LinearLayoutManager.HORIZONTAL);
    mRecycleView.setLayoutManager(layoutManager);
    StraggeredAdapter adapter = new StraggeredAdapter(getBaseContext(), mStraggeredDatas);
    mRecycleView.setAdapter(adapter);
}

//設置下拉刷新
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        new Thread(){
            @Override
            public void run() {
                SystemClock.sleep(2000);
                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        swipeRefreshLayout.setRefreshing(false);
                        mRecycleView.getAdapter().notifyDataSetChanged();
                    }
                });
            }
        }.start();
    }
});

github 地址 https://github.com/cynicgit/android5.0demo

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容