Android滾動組件RecyclerView 的用法

RecyclerView屬於新增控件,爲了讓RecyclerView在全部Android版本都能使用,因此Android團隊將RecyclerView定義在了support庫中,所以,想要使用RecyclerView控件,須要在build.gradle中添加相應的依賴庫java

implementation 'com.android.support:recyclerview-v7:26.1.0'

或者也能夠直接引入support:design這個依賴,這個包括Recyclerandroid

implementation 'com.android.support:design:26.1.0'
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//    implementation 'com.android.support:recyclerview-v7:26.1.0'
    implementation 'com.android.support:design:26.1.0'
}

在佈局中經過這個加入RecyclerView控件app

<android.support.v7.widget.RecyclerView
        android:id="@+id/act_test_rv"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


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

RecyclerView須要一些經常使用的方法,具體方法以下:ide

◆ setLayoutManager:設置列表項的佈局管理器,函數

LinearLayoutManager爲線性佈局管理器、GridLayoutManager爲網格佈局管理器、StaggeredGridLayoutManager爲瀑布流網格佈局管理器。佈局

◆ setItemAnimator:設置列表項增長或者是刪除時的動畫,可使用關鍵字new建立 DefaultItemAnimator()對象實現系統默認的動畫效果。測試

◆ addItemDecoration:添加列表項分割線。gradle

◆ addOnItemTouchListener:添加列表項的觸摸監聽器。動畫

◆ removeOnItemTouchListener:移除列表項的觸摸監聽器。ui

◆ setAdapter:設置列表項的適配器,使用RecyclerView.Adapter。

RecyclerView.Adapter是爲RecyclerView所單獨設計的適配器類,RecyclerView.Adapter的相關方法以下:

◆ getItemCount:獲取列表項的數目。

◆ onBindViewHolder:綁定列表項中所顯示的數據。

◆ onCreateViewHolder:在該方法中能夠加載列表item(子項)中的佈局文件。


在onCreate方法中設置佈局管理器

private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);
        recyclerView = findViewById(R.id.act_recyclerView);
        //設置爲線性佈局管理器
//      上面的LinearLayoutManager有三個參數:
//     (1) Context   (2)方向,下面是垂直方向,還能夠選擇水平方向LinearLayoutManager.HORIZONTAL
//     (3)是否顛倒循序,這個是針對數據源的,就是把你傳進去的數據進行倒着顯示,
//      好比你傳進去的集合是{1,2,3,4},那麼這個參數爲false的時候,顯示的是1,2,3,4,
//      可是若是你選擇的是true,顯示的是4,3,2,1。
//      效果跟listView差很少
        recyclerView.setLayoutManager(new LinearLayoutManager(RecyclerActivity.this
                , LinearLayoutManager.VERTICAL, false));

//        網格佈局管理器(GridLayoutManager),這個佈局管理器有兩個參數:
//        (1)Context  (2)顯示多少列
//        這種是比較常見和經常使用的佈局管理器,把數據整整齊齊的想網格同樣的把數據顯示出來。
//        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));


//        瀑布流佈局管理器(StaggeredLayouManager):
//        有兩個參數:
//        (1)Context  (2)顯示列數
//        這個通常是用來顯示那些item高度不一的佈局的
//        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, 
//        StaggeredGridLayoutManager.VERTICAL));

    }

新建一個Student類,其中屬性有id和name,添加構造函數和Getter and Setter

這裏就不放Student 的代碼了

建立一個item所需的佈局layout_student_item.xml,這裏就簡單寫了兩個TextView

<TextView
        android:id="@+id/item_student_id_tv"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:layout_weight="1"
        android:gravity="center"
        android:text="id"
        />

 

下一步,新建一個適配器,並繼承 android.support.v7.widget.RecyclerView.Adapter,

並自動實現其中的三個方法

StduentAdapter中的代碼以下:

public class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.LinearViewHolder> {
    private Context mContext;
    private List mList;

    public LinearAdapter(Context mContext, List mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    //建立
    @Override
    public LinearViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //在這裏設置佈局管理器
        return new LinearViewHolder(LayoutInflater.from(mContext)
        .inflate(R.layout.layout_student_item,parent,false));
    }

    @Override
    public void onBindViewHolder(LinearViewHolder holder, int position) {
        //將條目信息設置上
        Student stu = (Student) mList.get(position);
        holder.idTv.setText(""+stu.getId());
        holder.nameTv.setText(stu.getName());

    }

    @Override
    public int getItemCount() {
        //設置條目的長度
        return mList.size();
    }

    //建立一個LinearViewHolder類,
    // 而後在RecyclerView.Adapter中添加泛型LinearAdapter.LinearViewHolder
    class LinearViewHolder extends RecyclerView.ViewHolder {


        private final TextView idTv;
        private final TextView nameTv;

        public LinearViewHolder(View itemView) {
            super(itemView);
            //聲明佈局裏的控件
            idTv = itemView.findViewById(R.id.item_student_id_tv);
            nameTv = itemView.findViewById(R.id.item_student_name_tv);
        }
    }
}

最後在RecyclerActivity中添加以下代碼

//建立一個集合用於存儲對象
        List ls = new ArrayList();
        //這裏測試,先循環插入一些數據
        for (int i = 1; i <= 10000; i++) {
            ls.add(new Student(i, "student"));
        }
        recyclerView.setAdapter(new LinearAdapter(this,ls));

這樣列表基本功能就完成了,但是和ListView相比,少了條目分割線

RecyclerView能夠自定義分割線,能夠參照個人另外一篇博客

RecyclerView列表調用addItemDecoration實現添加自定義分割線

相關文章
相關標籤/搜索