RecyclerView新增多個樣式不同的Item佈局
阿新 • • 發佈:2019-02-18
RecyclerView載入一種Item顯示,往往不能滿足我們在專案中開發的需要,下面我來給大家演示一下RecyclerView如何給它新增多個佈局檔案
那麼現在問題來了,這樣實現的方式有很多種,1、通過RecyclerView中的Adapter中的getItemType();進行判斷載入不的佈局屬於哪一種樣式,
通過樣式來載入,但是這兒就會寫很多的程式碼,比如每一個viewHolder對應一種將被載入的佈局,這樣寫起來的缺點是,載入耗時,浪費資源
今天我採用另外一種方式呈現給大家,希望給大家帶來一定的幫助。詳情請看程式碼:
public abstract class MoreHeaderRecyclerAdapter<HeaderHolder extends RecyclerView.ViewHolder, HeaderTHolder
extends RecyclerView.ViewHolder, ItemHolder extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static int TYPE_HEAD = 0;//頭部
public static int TYPE_HEAD_TWO = 1;
public static int TYPE_ITEM = 2;
public static int TYPE_FOOT = 3; public abstract HeaderHolder onCreateHeaderViewHolder(ViewGroup parent, int position); public abstract HeaderTHolder onCreateHeaderTViewHolder(ViewGroup parent, int position); public abstract ItemHolder onCreateItemViewHolder(ViewGroup parent, int position); public abstract void onBindHeaderViewHolder(HeaderHolder holder, int position); public abstract void onBindHeaderTViewHolder(HeaderTHolder holder, int position); public abstract void onBindItemHolderViewHolder(ItemHolder holder, int position);
//第三部分和第二部分的介面回撥函式
public interface DetalisCallBack {
void getDetalisData(int index, int position);
} public DetalisCallBack detalisCallBack; public DetalisCallBack getDetalisCallBack() {
return detalisCallBack;
} public void setDetalisCallBack(DetalisCallBack detalisCallBack) {
this.detalisCallBack = detalisCallBack;
} @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 0:
return onCreateHeaderViewHolder(parent, viewType);
case 1:
return onCreateHeaderTViewHolder(parent, viewType);
default:
return onCreateItemViewHolder(parent, viewType);
}
} @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int headerCount = getHeadViewCount();
int headerTCount = getHeadTViewCount();
int itemType = getItemViewType(position);
switch (itemType) {
case 0:
onBindHeaderViewHolder((HeaderHolder) holder, position);
break;
case 1:
onBindHeaderTViewHolder((HeaderTHolder) holder, position - headerCount);
break;
case 2:
onBindItemHolderViewHolder((ItemHolder) holder, position - headerTCount - headerCount);
break;
}
} public abstract int getHeadTViewCount(); public abstract int getHeadViewCount(); public abstract int getItemViewCount(); @Override
public int getItemCount() {
return getItemViewCount() + getHeadTViewCount() + getHeadViewCount();
} @Override
public int getItemViewType(int position) {
int headerCount = getHeadViewCount();
int headerTCount = getHeadTViewCount();
if (position < headerCount) {
return TYPE_HEAD;
}
if (position <= headerTCount) {
return TYPE_HEAD_TWO;
}
return TYPE_ITEM;
}
} 下面我們一起來看看在activity或fragment中的使用: MoreHeaderRecyclerAdapter adapter = new MoreHeaderRecyclerAdapter() {
@Override
public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent, int position) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.public_item_viewpager, null);
return new HeaderHolder(view);
} @Override
public RecyclerView.ViewHolder onCreateHeaderTViewHolder(ViewGroup parent, int position) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.public_item_infos_two, null);
return new HeaderTHolder(view);
} @Override
public RecyclerView.ViewHolder onCreateItemViewHolder(ViewGroup parent, final int position) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.detalis_layout_item, null);
return new ItemHolder(view);
} @Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
HeaderHolder itemHolder = (HeaderHolder) holder;
itemHolder.PmViewPager.setAdapter(infoPageAdapter);
infoPageAdapter.UpDataUrl(dataUtils.initAdapterData(imageViews, urls, simpleDraweeViews, SimpleList, infoPageAdapter));
} @Override
public void onBindHeaderTViewHolder(RecyclerView.ViewHolder holder, final int position) {
HeaderTHolder tHolder = (HeaderTHolder) holder;
tHolder.public_text_content.setText("訂單詳情");
tHolder.alter.setText("詳情");
tHolder.LayoutRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (adapter.detalisCallBack != null) {
adapter.detalisCallBack.getDetalisData(0, position);
}
}
});
} @Override
public void onBindItemHolderViewHolder(RecyclerView.ViewHolder holder, final int position) {
ItemHolder itemHolder = (ItemHolder) holder;
itemHolder.bossName.setText(list.get(position).getClientId());
itemHolder.detalisSate.setText(list.get(position).getDetailsState());
itemHolder.detalisType.setText(list.get(position).getDetailsType());
itemHolder.detalisTime.setText(list.get(position).getOrdersTime());
itemHolder.detallisNumber.setText(list.get(position).getDetailsNumber());
itemHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (adapter.detalisCallBack != null) {
adapter.detalisCallBack.getDetalisData(1, position);
}
}
});
}
@Override
public int getHeadTViewCount() {
return 1;
} @Override
public int getHeadViewCount() {
return 1;
} @Override
public int getItemViewCount() {
return list.size();
}
}; 這樣我們再用起來就簡單多了,對於這樣的封裝, 我們還算滿意,再做完新增header後,相信大家對於footer也有想法了,有想法就實現它吧!