自從Android 5.0 之後,google 推出了一個 RecyclerView 控制元件,他是support-v7包中的新元件,是一個強大的滑動元件,與經典的ListView相比,同樣擁有item回收複用的功能,RecyclerView相當於是ListView的升級版。
RecyclerView 封裝了 ViewHolder 的回收複用,也就是說RecyclerView標準化了 ViewHolder,編寫Adapter面向的是 ViewHolder 而不是 View 了,複用的邏輯被封裝了,寫起來更加簡單。
RecyclerView提供一種插拔式的體驗,高度的解耦,異常的靈活,針對一個Item的顯示RecyclerView專門抽取了相應的類,來控制Item的顯示,使其的擴充套件性特別強。
引入RecyclerVIew
compile 'com.android.support:recyclerview-v7:25.1.0'
複製程式碼
建立佈局檔案
主佈局檔案
/*activity_main.xml*/
<?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="match_parent"
tools:context="com.manu.mrecyclerview.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
複製程式碼
Item佈局檔案
/*item.xml*/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<TextView
android:id="@+id/tv_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="data"
android:background="#cac3c3"
android:padding="10dp"
android:textSize="20sp"/>
</LinearLayout>
複製程式碼
建立Adapter
RecyclerView的Adapter要比ListView的Adapter設定起來稍微複雜一點,這也是RecyclerView高度解耦的體現,雖然程式碼複雜一點,但擴充套件性很好,下面介紹一下實現RecyclerView的Adapter的三個方法:
onCreateViewHolder()
該方法主要是為每個Item載入一個View,但是該方法返回的是一個ViewHolder,該方法吧View直接封裝的ViewHolder中,然後我們面向的是ViewHolder這個例項,這個ViewHolder也由自己編寫,但不用像ListView呼叫convertView.setTag(vh)和convertView.getTag()了。
onBindViewHolder()
該方法主要用來把資料繫結在View上,直接提供一個ViewHolder而不是convertView。
getItemCount()
該方法返回選項總數。
Adapter程式碼
/**
* Created by jzman on 2017/5/13 0013.
*/
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.DataViewHolder>{
private Context mContext;
private ArrayList<String> mList;
public RvAdapter() {}
public RvAdapter(Context mContext, ArrayList<String> mList) {
this.mContext = mContext;
this.mList = mList;
}
//用於建立ViewHolder
@Override
public DataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item,null);
//使用程式碼設定寬高(xml佈局設定無效時)
view.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
DataViewHolder holder = new DataViewHolder(view);
return holder;
}
//繫結資料
@Override
public void onBindViewHolder(DataViewHolder holder, int position) {
holder.tv_data.setText(mList.get(position));
}
//資料總數
@Override
public int getItemCount() {
return mList.size();
}
//建立ViewHolder
public static class DataViewHolder extends RecyclerView.ViewHolder{
TextView tv_data;
public DataViewHolder(View itemView) {
super(itemView);
tv_data = (TextView) itemView.findViewById(R.id.tv_recycle);
}
}
複製程式碼
使用StaggeredGridLayoutManager管理器時,Adapter參考如下:
/**
* Created by jzman on 2017/5/13 0013.
* RecycleView的Adapter
*/
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.DataViewHolder>{
private Context mContext;
private RecyclerView recyclerView;
private ArrayList<String> mList;
private ArrayList<Integer> mHeight;
public RvAdapter() {}
public RvAdapter(Context mContext, ArrayList<String> mList) {
this.mContext = mContext;
this.mList = mList;
}
/**
* 初始化每個Item的高度(瀑布流效果)
* @return
*/
public ArrayList<Integer> initHeight(){
mHeight = new ArrayList<>();
for (int i=0;i<mList.size();i++){
mHeight.add((int) (Math.random()*300)+200);
}
return mHeight;
}
/**
* 用於建立ViewHolder
* @param parent
* @param viewType
* @return
*/
@Override
public DataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item,null);
//使用程式碼設定寬高(xml佈局設定無效時)
view.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
DataViewHolder holder = new DataViewHolder(view);
return holder;
}
/**
* 繫結資料
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(DataViewHolder holder, int position) {
//設定每個Item的高度
ViewGroup.LayoutParams h = holder.tv_data.getLayoutParams();
h.height = mHeight.get(position);
holder.tv_data.setText(mList.get(position));
}
/**
* 選項總數
* @return
*/
@Override
public int getItemCount() {
return mList.size();
}
/**
* 建立ViewHolder
*/
public static class DataViewHolder extends RecyclerView.ViewHolder{
TextView tv_data;
public DataViewHolder(View itemView) {
super(itemView);
tv_data = (TextView) itemView.findViewById(R.id.tv_recycle);
}
}
/**
* 將RecycleView附加到Adapter上
*/
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
this.recyclerView= recyclerView;
//初始化每個Item的高度
initHeight();
}
/**
* 將RecycleView從Adapter解除
*/
@Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
super.onDetachedFromRecyclerView(recyclerView);
this.recyclerView = null;
}
}
複製程式碼
MainActivity
/**
* Created by jzman on 2017/5/13 0013.
*/
public class MainActivity extends AppCompatActivity {
private RecyclerView rv;
RvAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = (RecyclerView) findViewById(R.id.rv);
//設定佈局管理器
rv.setLayoutManager(new LinearLayoutManager(this));//線性
// rv.setLayoutManager(new GridLayoutManager(this,4));//線性
// rv.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL));//線性
adapter = new RvAdapter(this,initData());
rv.setAdapter(adapter);
}
public static ArrayList<String> initData(){
ArrayList<String> arrayList = new ArrayList<>();
for (int i=0;i<50;i++){
arrayList.add("第"+i+"條資料");
}
return arrayList;
}
}
複製程式碼
顯示效果
LinearLayoutManager | GridLayoutManager | StaggeredGridLayoutManager |
---|---|---|
如果覺得對自己有幫助,可以關注公眾號:jzman-blog,一起交流學習。