1. 程式人生 > >Android元件之RecyclerView的基本使用

Android元件之RecyclerView的基本使用

效果圖
這裡寫圖片描述

1.匯入依賴包

在build.gradle(Module:app)的dependencies中加入
compile ‘com.android.support:recyclerview-v7:25.1.0’

這裡的版本號與上下保持一致

2.在XML檔案中使用

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.csdn.csdn.MainActivity"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recyclerView"
/> </RelativeLayout>

item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height
="80dp" android:id="@+id/textView"/>
</LinearLayout>

3.在Java檔案中載入

    recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    //設定豎向佈局
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);

    /*************************
    *佈局有三種選擇:
    *LinerlayoutManager(豎向)
    *GridLayoutManager(橫向)
    *StaggeredGirdLayoutManager(瀑布流式)
    ***********************/

    //載入佈局
    recyclerView.setLayoutManager(linearLayoutManager);
    //高度固定,不是瀑布流時可以節省資源
    recyclerView.setHasFixedSize(true);
    //設定Adapter
    adapter = new Adapter(dataset);
    recyclerView.setAdapter(adapter);

此外dataset一般還要進行初始化

private void initDataset() {
        dataset = new String[10];
        for (int i = 0; i < 10; i++) {
            dataset[i] = ("item" + i);
        }
    }

這裡寫圖片描述
RecyclerView的使用

4.編寫Adapter

Adapter需要繼承 RecyclerView,並且複寫3個方法:
onCreateViewHolder
onBindViewHolder
getItemCount

(1)onCreateViewHolder


public Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    //獲取父容器parent,向parent中填充item子容器。即關聯item佈局
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
    }

(2)onBindViewHolder


    //繫結ViewHolder,即載入資料
    public void onBindViewHolder(Adapter.ViewHolder holder, int position) {
        holder.textView.setText(dataset[position]);
    }

(3)getItemCount

    //獲取item數目
    public int getItemCount() {
        return mDataset.size();
    }

此外一般還有再寫構造器方法與ViewHolder方法

構造器方法用於資料的初始化

    //初始化資料
    public MyAdapter(List<> mDataSet) {
        this.mDataset = mDataSet;
    }

ViewHolder方法用於元件的初始化

    //載入並初始化元件
    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView name, teacher, detail, day, type, credit;

        public ViewHolder(View v) {
            super (v);
            name = (TextView) v.findViewById(R.id.name);
            teacher = (TextView) v.findViewById(R.id.teacher);
            detail = (TextView) v.findViewById(R.id.detail);
            day = (TextView) v.findViewById(R.id.day);
            type = (TextView) v.findViewById(R.id.type);
            credit = (TextView) v.findViewById(R.id.credit);
        }
    }