您好,登錄后才能下訂單哦!
自從Android 5.0之后,google推出了一個RecyclerView控件,他是support-v7包中的新組件,是一個強大的滑動組件,與經典的ListView相比,同樣擁有item回收復用的功能,RecyclerView相當于是ListView的升級版。
RecyclerView封裝了ViewHolder的回收復用,也就是說RecyclerView標準化了ViewHolder,編寫Adapter面向的是ViewHolder而不是View了,復用的邏輯被封裝了,寫起來更加簡單。
RecyclerView提供一種插拔式的體驗,高度的解耦,異常的靈活,針對一個Item的顯示RecyclerView專門抽取了相應的類,來控制Item的顯示,使其的擴展性特別強。
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.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>
RecyclerView的Adapter要比ListView的Adapter設置起來稍微復雜一點,這也是RecyclerView高度解耦的體現,雖然代碼復雜一點,但擴展性很好,下面介紹一下實現RecyclerView的Adapter的三個方法:
該方法主要是為每個Item加載一個View,但是該方法返回的是一個ViewHolder,該方法吧View直接封裝的ViewHolder中,然后我們面向的是ViewHolder這個實例,這個ViewHolder也由自己編寫,但不用像ListView調用convertView.setTag(vh)和convertView.getTag()了。
該方法主要用來把數據綁定在View上,直接提供一個ViewHolder而不是convertView。
該方法返回選項總數。
/**
* 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;
}
}
/**
* 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,一起交流學習。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。