- RecyclerView在項(xiàng)目中使用大家比較簡(jiǎn)單仅醇,但當(dāng)頁(yè)面中item類(lèi)型超過(guò)三個(gè)以上(比如絢麗的首頁(yè)等),通過(guò)不同的類(lèi)型去選擇不同的viewHolder會(huì)導(dǎo)致adapter中的代碼過(guò)于復(fù)雜魔种,后期增加或刪減item更加痛苦。本文將采用另一種方式來(lái)應(yīng)對(duì)復(fù)雜多變的item粉洼。
詳細(xì)類(lèi)圖參考.png
使用方式:
1.所有itemBean統(tǒng)一實(shí)現(xiàn)Visitable接口中的type方法
/**
* Created by beifeng on 2017/2/17.
* 所有itembean統(tǒng)一實(shí)現(xiàn)的接口
*/
public interface Visitable {
int type(TypeFactory typeFactory);
}
itemBean中通過(guò)typeFactory返回對(duì)應(yīng)的type區(qū)分
/**
* Created by beifeng on 2017/2/17.
* 頭部bean
*/
public class BannerBean implements Visitable {
String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public BannerBean(String url) {
this.url = url;
}
/**
* 工廠類(lèi)返回對(duì)應(yīng)itembean的類(lèi)型
*/
@Override public int type(TypeFactory typeFactory) {
return typeFactory.type(this);
}
}
2. 簡(jiǎn)單的工廠模式节预,通過(guò)不同類(lèi)型bean返回不同類(lèi)型的holder
/**
* Created by beifeng on 2017/2/17.
* type 工廠類(lèi) 用來(lái)返回不同的itmetpe
*/
public interface TypeFactory {
// 定義所有的返回類(lèi)型
int type(BannerBean bannerBean);
int type(ContentBean contentBean);
BaseViewHolder createViewHolder(int type, View itemView);
}
具體實(shí)現(xiàn)類(lèi)ItemTypeFactory
通過(guò)將不同item的layoutID作為type返回對(duì)應(yīng)item的viewholder,這種做法的好處是 在adapter的 onCreateViewHolder(ViewGroup parent, int viewType)
中通過(guò)viewtype直接獲取view属韧,進(jìn)而獲取相應(yīng)的viewHoler安拟,大大減輕了adapter中的復(fù)雜的類(lèi)型判斷。
/**
* Created by beifeng on 2017/2/17.
* 實(shí)現(xiàn)type工程類(lèi)
*/
public class ItemTypeFactory TypeFactory {
public static final int BANNER_ITEM_LAYOUT = R.layout.item_banner_mulittype;// 測(cè)試布局1
public static final int CONTENT_ITEM_LAYOUT = R.layout.item_content_mulittype;// 測(cè)試布局2
@Override public int type(BannerBean bannerBean) {
return BANNER_ITEM_LAYOUT;
}
@Override public int type(ContentBean contentBean) {
return CONTENT_ITEM_LAYOUT;
}
@Override public BaseViewHolder createViewHolder(int type, View itemView) {
switch (type) {
case BANNER_ITEM_LAYOUT:
return new BannerViewHolder(itemView);
case CONTENT_ITEM_LAYOUT:
return new ContentViewHolder(itemView);
default:
return null;
}
}
}
3.此時(shí)可以在recycleView的Adpter中愉快的玩耍啦
/**
* Created by beifeng on 2017/2/17.
*/
public class MulitAdpter extends RecyclerView.Adapter<BaseViewHolder> {
private ItemTypeFactory typeFactory;
List<Visitable> mItems;
/**
* 構(gòu)造函數(shù)
*/
public MulitAdpter(List<Visitable> mData) {
// item工廠類(lèi) 生產(chǎn)viewholder
this.typeFactory = new ItemTypeFactory();
mItems=mData;
}
@Override public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent,false);
return typeFactory.createViewHolder(viewType, view);//劃重點(diǎn)
}
@Override public void onBindViewHolder(BaseViewHolder holder, int position) {
holder.bindViewData(mItems.get(position));
}
@Override public int getItemViewType(int position) {
return mItems.get(position).type(typeFactory); //劃重點(diǎn)
}
@Override public int getItemCount() {
return (mItems != null ? mItems.size() : 0);
}
}
4.總結(jié)
此時(shí)可以在recycleView的Adpter中愉快的玩耍啦宵喂,面對(duì)更復(fù)雜的recycleView頁(yè)面輕松應(yīng)對(duì)糠赦。這種方式雖然類(lèi)結(jié)構(gòu)較復(fù)雜,但將復(fù)雜的多類(lèi)型item與adapter解耦锅棕,便于維護(hù)拙泽。后期需要新增個(gè)類(lèi)型,完全可以不用修改adapter裸燎,只需要增加對(duì)應(yīng)的bean顾瞻,viewHolder即可。
Demo鏈接
實(shí)現(xiàn)思路
Jan M and Dmitri Kudrenko on Github
https://github.com/meierjan/BetterAdapters