RecyclerView加载一种Item显示,往往不能满足我们在项目中开发的需要,下面我来给大家演示一下RecyclerView如何给它添加多个布局文件
那么现在问题来了,这样实现的方式有很多种,1、通过RecyclerView中的Adapter中的getItemType();进行判断加载不的布局属于哪一种样式,
通过样式来加载,但是这儿就会写很多的代码,比如每一个viewHolder对应一种将被加载的布局,这样写起来的缺点是,加载耗时,浪费资源
今天我采用另外一种方式呈现给大家,希望给大家带来一定的帮助。详情请看代码:
public abstract class MoreHeaderRecyclerAdapter
extends RecyclerView.ViewHolder, ItemHolder extends RecyclerView.ViewHolder> extends RecyclerView.Adapter {
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也有想法了,有想法就实现它吧!