您好,登錄后才能下訂單哦!
背景
隨著公司業務越來越復雜,在同一個列表中需要展示各種類型的數據。
總體結構
使用方法
每種類型卡片Item都實現ItemViewAdapter
package com.lkh.multiadapter; import android.support.annotation.LayoutRes; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.util.List; /** * 列表單項布局與數據綁定 * Created by luokanghui on 2017/5/24. */ public abstract class ItemViewAdapter<E>{ /** * 返回列表單項View,如果View由資源layout加載而來,直接重寫{@link #onGetLayoutId()}即可 * @param parent 父view,一般為RecyclerView * @return 列表單項View */ public View onCreateView(ViewGroup parent){ return LayoutInflater.from(parent.getContext()).inflate(onGetLayoutId() , parent, false); } /** * 當RecyclerViewHolder創建成功后調用,只會調用一次 * @param viewHolder 單項view集合 */ public void onCreate(RecyclerViewHolder viewHolder){ } /** * 返回單項布局的資源id,如果重寫了{@link #onCreateView(ViewGroup)},則此方法可能失效 * @return 單項布局layout id */ @LayoutRes protected abstract int onGetLayoutId(); /** * 把數據與view進行綁定,滑動時都會調用 * @param viewHolder 單項view集合 * @param data 具體數據 * @param position 在列表中的位置 */ public abstract void bindData(RecyclerViewHolder viewHolder, E data, int position); /** * 局部更新時調用 * @param viewHolder 單項view集合 * @param data 具體數據 * @param position 在列表中的位置 * @param payloads 局部更新標志,不會為空(isEmpty()==false) */ public void bindData(RecyclerViewHolder viewHolder, E data, int position, List<Object> payloads){ } }
卡片1:
package com.lkh.multiadapter.sample; import android.widget.TextView; import com.lkh.multiadapter.ItemViewAdapter; import com.lkh.multiadapter.R; import com.lkh.multiadapter.RecyclerViewHolder; /** * 卡片1實現 * Created by luokanghui on 2019/3/18 */ public class SampleOneItemViewAdapter extends ItemViewAdapter<DataOne> { @Override protected int onGetLayoutId() { //布局layout資源id return R.layout.item_one; } @Override public void bindData(RecyclerViewHolder viewHolder, DataOne data, int position) { //根據id獲取view TextView tvContent = viewHolder.getView(R.id.tv_content); //數據綁定 tvContent.setText(data.getContent()); } }
package com.lkh.multiadapter.sample; /** * 卡片1數據 * Created by luokanghui on 2019/3/18 */ public class DataOne { private String content; public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
item_one.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#eeeeee" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="卡片1" android:textColor="#000000" /> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="center" android:textColor="#000000" /> </LinearLayout>
卡片2
package com.lkh.multiadapter.sample; import android.widget.TextView; import com.lkh.multiadapter.ItemViewAdapter; import com.lkh.multiadapter.R; import com.lkh.multiadapter.RecyclerViewHolder; /** * 卡片2實現 * Created by luokanghui on 2019/3/18 */ public class SampleTwoItemViewAdapter extends ItemViewAdapter<DataTwo> { @Override protected int onGetLayoutId() { //布局layout資源id return R.layout.item_two; } @Override public void bindData(RecyclerViewHolder viewHolder, DataTwo data, int position) { //根據id獲取view TextView tvNum = viewHolder.getView(R.id.tv_num); //數據綁定 tvNum.setText("num="+data.getNum()); } }
package com.lkh.multiadapter.sample; /** * 卡片2數據 * Created by luokanghui on 2019/3/18 */ public class DataTwo { private int num; public int getNum() { return num; } public void setNum(int num) { this.num = num; } }
item_two.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#999999" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="卡片2" android:textColor="#0000ff" /> <TextView android:id="@+id/tv_num" android:layout_width="wrap_content" android:layout_height="100dp" android:gravity="center" android:textColor="#0000ff" /> </LinearLayout>
總Adapter,繼承MultiRecyclerViewAdapter
package com.lkh.multiadapter.sample; import com.lkh.multiadapter.ItemViewAdapter; import com.lkh.multiadapter.MultiRecyclerViewAdapter; /** * 多布局adapter,根據不同data及position,使用不同ItemViewAdapter卡片 * Created by luokanghui on 2019/3/18 */ public class SampleMultiAdapter extends MultiRecyclerViewAdapter<Object> { private static final int TYPE_EMPTY = 0;//空item private static final int TYPE_ONE = 1;//卡片1 private static final int TYPE_TWO = 2;//卡片2 @Override public int getViewType(Object data, int position) { if (data instanceof DataOne){//卡片1 return TYPE_ONE; } if (data instanceof DataTwo){//卡片2 return TYPE_TWO; } return TYPE_EMPTY;//空item } @Override public ItemViewAdapter onCreateItemViewAdapter(int viewType) { switch (viewType){ case TYPE_ONE://卡片1 return new SampleOneItemViewAdapter(); case TYPE_TWO://卡片2 return new SampleTwoItemViewAdapter(); default://空item return new EmptyItemViewAdapter(); } } }
RecyclerView中使用
package com.lkh.multiadapter; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import com.lkh.multiadapter.sample.DataOne; import com.lkh.multiadapter.sample.DataTwo; import com.lkh.multiadapter.sample.SampleMultiAdapter; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerView recyclerView = findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(this)); SampleMultiAdapter adapter = new SampleMultiAdapter(); //設置數據 adapter.setData(generateData()); //設置adapter recyclerView.setAdapter(adapter); } //造測試數據 private List<Object> generateData(){ List<Object> list = new ArrayList<>(); for (int i=0; i<20; i++){ DataOne dataOne = new DataOne(); dataOne.setContent("這是卡片1數據:"+i); list.add(dataOne); DataTwo dataTwo = new DataTwo(); dataTwo.setNum(i); list.add(dataTwo); } return list; } }
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
運行效果如下:
總的來說,實現一個多類型布局列表只需要寫多個不同卡片ItemViewAdapter、繼承MultiRecyclerViewAdapter用來控制不同數據使用不同ItemViewAdapter,新增一個卡片只需要新增一個ItemViewAdapter,在MultiRecyclerViewAdapter新加一項即可,不會影響其它卡片使用,而且ItemViewAdapter完全獨立,可以很好的復用。
核心代碼
package com.lkh.multiadapter; import android.support.v7.widget.RecyclerView; import android.view.ViewGroup; import java.util.List; /** * 多種布局adapter * Created by luokanghui on 2017/5/24. */ public abstract class MultiRecyclerViewAdapter<E> extends RecyclerView.Adapter<RecyclerViewHolder> implements ItemViewAdapterFactory<E> { public static final int NO_TYPE = -1; private List<E> dataList; protected final MapData mMapData = new MapData(); public MultiRecyclerViewAdapter setData(List<E> list) { this.dataList = list; return this; } @Override public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { ItemViewAdapter itemViewModule = onCreateItemViewAdapter(viewType); RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(itemViewModule.onCreateView(parent), itemViewModule, this, getContextMap()); itemViewModule.onCreate(recyclerViewHolder); return recyclerViewHolder; } @Override public void onBindViewHolder(RecyclerViewHolder holder, int position) { if (checkItems(position)) { return; } holder.itemViewAdapter.bindData(holder, dataList.get(position), position); } @Override public void onBindViewHolder(RecyclerViewHolder holder, int position, List<Object> payloads) { if (checkItems(position)) { return; } if (payloads.isEmpty()) { super.onBindViewHolder(holder, position, payloads); } else { holder.itemViewAdapter.bindData(holder, dataList.get(position), position, payloads); } } @Override public int getItemViewType(int position) { if (checkItems(position)) { return NO_TYPE; } return getViewType(dataList.get(position), position); } @Override public int getItemCount() { return dataList == null ? 0 : dataList.size(); } /** * true表示沒通過 */ private boolean checkItems(int position) { return dataList == null || position < 0 || position >= dataList.size(); } @Override public MapData getContextMap() { return mMapData; } }
package com.lkh.multiadapter; /** * 多布局ItemViewAdapter創建者 * Created by luokanghui on 2017/5/24. */ public interface ItemViewAdapterFactory<E> { /** * 返回ItemViewAdapter的類型 * 建議根據data的數據類型判斷不同的viewType * @param data 具體數據 * @param position 在列表中的位置 * @return 類型 */ int getViewType(E data, int position); /** * 根據不同的viewType返回不同的ItemViewAdapter * @param viewType 類型 * @return ItemViewAdapter */ ItemViewAdapter<? extends E> onCreateItemViewAdapter(int viewType); /** * 上下文數據 * @return */ MapData getContextMap(); }
package com.lkh.multiadapter; import android.support.v7.widget.RecyclerView; import android.util.SparseArray; import android.view.View; /** * ViewHolder基類 */ public final class RecyclerViewHolder extends RecyclerView.ViewHolder { private final SparseArray<View> views; ItemViewAdapter itemViewAdapter; private final RecyclerView.Adapter adapter; private final MapData mMapData ; public RecyclerViewHolder(View itemView, ItemViewAdapter itemViewAdapter, RecyclerView.Adapter adapter, MapData mapData) { super(itemView); this.views = new SparseArray<>(); this.itemViewAdapter = itemViewAdapter; this.adapter = adapter; this.mMapData = mapData; } /** * 根據id獲取view,如果緩存中存在,直接使用緩存中的,避免重復執行findViewById */ @SuppressWarnings("unchecked") public <T extends View> T getView(int viewId) { View view = views.get(viewId); if (view == null) { view = itemView.findViewById(viewId); views.put(viewId, view); } return (T) view; } public RecyclerView.Adapter getAdapter(){ return adapter; } /** * 獲取數據上下文 */ public MapData getContextMap(){ return mMapData; } }
package com.lkh.multiadapter; import android.support.annotation.LayoutRes; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.util.List; /** * 列表單項布局與數據綁定 * Created by luokanghui on 2017/5/24. */ public abstract class ItemViewAdapter<E>{ /** * 返回列表單項View,如果View由資源layout加載而來,直接重寫{@link #onGetLayoutId()}即可 * @param parent 父view,一般為RecyclerView * @return 列表單項View */ public View onCreateView(ViewGroup parent){ return LayoutInflater.from(parent.getContext()).inflate(onGetLayoutId() , parent, false); } /** * 當RecyclerViewHolder創建成功后調用,只會調用一次 * @param viewHolder 單項view集合 */ public void onCreate(RecyclerViewHolder viewHolder){ } /** * 返回單項布局的資源id,如果重寫了{@link #onCreateView(ViewGroup)},則此方法可能失效 * @return 單項布局layout id */ @LayoutRes protected abstract int onGetLayoutId(); /** * 把數據與view進行綁定,滑動時都會調用 * @param viewHolder 單項view集合 * @param data 具體數據 * @param position 在列表中的位置 */ public abstract void bindData(RecyclerViewHolder viewHolder, E data, int position); /** * 局部更新時調用 * @param viewHolder 單項view集合 * @param data 具體數據 * @param position 在列表中的位置 * @param payloads 局部更新標志,不會為空(isEmpty()==false) */ public void bindData(RecyclerViewHolder viewHolder, E data, int position, List<Object> payloads){ } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。