**RecyclerView**是谷歌V7包下新增的控件,用來替代ListView的使用,在RecyclerView標準化了ViewHolder類似于ListView中convertView用來做視圖緩.
可以通過設置LayoutManager來快速實現listview、gridview、瀑布流的效果,而且還可以設置橫向和縱向顯示,添加動畫效果也非常簡單(自帶了ItemAnimation,可以設置加載和移除時的動畫,方便做出各種動態瀏覽的效果),也是官方推薦使用的.以下是官方的說明:
> RecyclerView is a more advanced and flexible version of ListView. This widget is a container for large sets of views that can be recycled and scrolled very efficiently. Use the RecyclerView widget when you have lists with elements that change dynamically. 簡單說就是當你需要動態展示一組數據的時候就會需要用到它。
>
使用步驟:
1、引入類庫
~~~
implementation 'com.android.support:appcompat-v7:26.1.0'
~~~
2、布局文件
~~~
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_projects"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray" />
~~~
3、Activity實現
~~~
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
adapter = new ProjectAdapter(R.layout.list_item_project, projectList);
recyclerView.addOnItemTouchListener(new OnItemClickListener() {
@Override
public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
}
});
recyclerView.setAdapter(adapter);
~~~