自從Android 5.0以後,googlet推出了一個RecyclerView控件,他是support-v7包中的新組件,是一個強大的滑動組件,與經典的ListView相比,一樣擁有item回收複用的功能,RecyclerView至關因而ListView的升級版。java
RecyclerView封裝了ViewHolder的回收複用,也就是說RecyclerView標準化了ViewHolder,編寫Adapter面向的是ViewHolder而不是View了,複用的邏輯被封裝了,寫起來更加簡單。android
RecyclerView提供一種插拔式的體驗,高度的解耦,異常的靈活,針對一個Item的顯示RecyclerView專門抽取了相應的類,來控制Item的顯示,使其的擴展性特別強。bash
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的三個方法:dom
該方法主要是爲每一個Item加載一個View,可是該方法返回的是一個ViewHolder,該方法吧View直接封裝的ViewHolder中,而後咱們面向的是ViewHolder這個實例,這個ViewHolder也由本身編寫,但不用像ListView調用convertView.setTag(vh)和convertView.getTag()了。ide
該方法主要用來把數據綁定在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參考以下:this
/** * 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,一塊兒交流學習。google