关于不同类型多布局的展示BaseMultiItemQuickAdapter详解
二 新篇章
对于Android 初学者来说,说到数据展示,相信大家对一定会想到ListView 或者 RecyclerView,但是简单的数据展示这个确实没毛病。当出现复杂数据需要多布局展示的时候,这个时候相信大部分人想到的都是各种嵌套 , 最常见的 是ScrollView , ListView 以及 RecyclerView 三者之间的 嵌套;当你嵌套的时候问题来了,最大的问题不外乎滑动事件冲突,以及滑动卡顿问题 ,这个时候要么去自定义 要么去重写方法,对于初学者来说是一件很头疼的事情,其实当你看Google官方文档的时候你会发现 当初在设计的时候并没有要开发者去嵌套使用,接下来我给大家介绍一种三方的框架 BaseMultiItemQuickAdapter
,希望能够帮助到Android 初学者!这是一个简单易懂使用的框架
如果需要定义多个不同类型的布局,需要知道以下几点:
重写getItemViewType()方法,根据需求规则给出不同的type值。
在onCreateViewHolder()方法里面根据不同的type来创建不同的ViewHolder
在onBindViewHolder()里面根据不同holder的类型来赋予相对于的数据
如果有额外布局(如:头部尾部)需要修改getItemCount()方法的返回数量
多个不同的布局的实现
1.重写了getDefItemViewType()方法,其实也就是父类的getItemViewType()里面的方法,
2.type搞定了,接下来就是调用onCreateDefViewHolder()了,根据上面的type来创建不同的ViewHolder,
3.然后再接下来调用onBindDefViewHolder(),根据上面创建的ViewHolder的类型不同来进行相应的赋值。
添加头部和尾部
下面直接上代码:
MultipleItemQuickAdapter 类:
public class MultipleItemQuickAdapter extends BaseMultiItemQuickAdapter
private String SUGGEST_DATE = "SUGGEST_DATE";
private String SUGGEST_GOODS_DATE = "SUGGEST_GOODS_DATE";
private String TIME_DATE = "TIME_DATE";
private Context mContext;
private HomeHeadResponse mHomeHeadResponse;
private int page = 1;
private NewsShopsItemAdapter itemClickAdapter;
public MultipleItemQuickAdapter(Context context, List data) {
super(data);
this.mContext = context;
addItemType(MultipleItem.ITEM_TYPE_1, R.layout.item_type_1);
addItemType(MultipleItem.ITEM_TYPE_2, R.layout.item_type_2);
addItemType(MultipleItem.ITEM_TYPE_3, R.layout.item_type_3);
addItemType(MultipleItem.ITEM_TYPE_4, R.layout.item_type_4);
addItemType(MultipleItem.ITEM_TYPE_5, R.layout.item_type_5);
addItemType(MultipleItem.ITEM_TYPE_6, R.layout.item_type_6);
}
@Override
protected void convert(BaseViewHolder helper, MultipleItem item) {
switch (helper.getItemViewType()) {
case MultipleItem.ITEM_TYPE_1:
initView1(helper);
break;
case MultipleItem.ITEM_TYPE_2:
initView2(helper);
break;
case MultipleItem.ITEM_TYPE_3:
initView3(helper);
break;
case MultipleItem.ITEM_TYPE_4:
initView4(helper);
break;
case MultipleItem.ITEM_TYPE_5:
initView5(helper);
break;
case MultipleItem.ITEM_TYPE_6:
initView6(helper, false);
break;
}
//其中一个视图以及数据如下:
private void initView1(BaseViewHolder helper) {
RecyclerView recyclerView = helper.getView(R.id.rv_list);
final LinearLayoutManager manager = new LinearLayoutManager(mContext);
recyclerView.setLayoutManager(manager);
List
for (int i = 0; i < 10; i++) {
data.add(new ClickEntity(ClickEntity.CLICK_ITEM_VIEW_1));
}
final ItemClickAdapter itemClickAdapter = new ItemClickAdapter(data);
recyclerView.setAdapter(itemClickAdapter);
}
}
}
MultipleItem 类:
public class MultipleItem implements MultiItemEntity {
private int itemType;
private int spanSize;
public static final int ITEM_TYPE_1 = 1;
public static final int ITEM_TYPE_2 = 2;
public static final int ITEM_TYPE_3 = 3;
public static final int ITEM_TYPE_4 = 4;
public static final int ITEM_TYPE_5 = 5;
public static final int ITEM_TYPE_6 = 6;
public MultipleItem(int itemType, int spanSize, String content) {
this.itemType = itemType;
this.spanSize = spanSize;
this.content = content;
}
public MultipleItem(int itemType, int spanSize) {
this.itemType = itemType;
this.spanSize = spanSize;
}
public MultipleItem(int itemType) {
this.itemType = itemType;
}
public int getSpanSize() {
return spanSize;
}
public void setSpanSize(int spanSize) {
this.spanSize = spanSize;
}
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public int getItemType() {
return itemType;
}
}
ItemClickAdapter 类:
public class ItemClickAdapter extends BaseMultiItemQuickAdapter
public ItemClickAdapter(List
super(data);
addItemType(ClickEntity.CLICK_ITEM_VIEW_1, R.layout.item_image_text_1);
addItemType(ClickEntity.CLICK_ITEM_VIEW_2, R.layout.item_image_text_2);
addItemType(ClickEntity.CLICK_ITEM_VIEW_3, R.layout.item_image_text_3);
addItemType(ClickEntity.CLICK_ITEM_VIEW_4, R.layout.item_image_text_4);
addItemType(ClickEntity.CLICK_ITEM_VIEW_5, R.layout.item_image_text_5);
addItemType(ClickEntity.CLICK_ITEM_VIEW_6, R.layout.item_image_text_6);
}
}
ClickEntity 类:
public class ClickEntity implements MultiItemEntity {
public static final int CLICK_ITEM_VIEW_1 = 1;
public static final int CLICK_ITEM_VIEW_2 = 2;
public static final int CLICK_ITEM_VIEW_3 = 3;
public static final int CLICK_ITEM_VIEW_4 = 4;
public static final int CLICK_ITEM_VIEW_5 = 5;
public static final int CLICK_ITEM_VIEW_6 = 6;
public int Type;
public ClickEntity(final int type) {
Type = type;
}
@Override
public int getItemType() {
return Type;
}
}
item_image_text_1布局:
android:layout_height="wrap_content"
xmlns:fresco="http://schemas.android.com/apk/res-auto">
android:layout_marginLeft="12dp"
android:layout_width="93dp"
android:layout_height="wrap_content">
android:layout_width="93dp"
android:layout_height="110dp"
fresco:actualImageScaleType="fitCenter"
fresco:failureImage="@drawable/icon_zc_tpyjz"
fresco:failureImageScaleType="fitCenter"
fresco:placeholderImage="@drawable/icon_zc_tpyjz"
fresco:placeholderImageScaleType="fitCenter"
fresco:progressBarImage="@drawable/icon_zc_tpyjz"
fresco:progressBarImageScaleType="fitCenter"/>
android:ellipsize="end"
android:maxLines="1"
android:id="@+id/text"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="秘制排骨汤"
android:textSize="14sp"
android:textColor="#171717"
android:layout_below="@+id/img"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥657.00"
android:textSize="12sp"
android:textColor="#ec1f26"
android:layout_below="@+id/text"
android:layout_marginTop="3dp"/>
//其他布局参考上面
核心类:HomePagerFragment
public class HomePagerFragment extends Fragment {
private View mContentView;
private RecyclerView mRecyclerView;
private MultipleItemQuickAdapter multipleItemAdapter;
public HomePagerFragment() {
}
public static HomePagerFragment newInstance() {
return new HomePagerFragment();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mContentView = inflater.inflate(R.layout.fragment_jd_home_page, container, false);//setContentView(inflater, container);
setUpView();
setta();
return mContentView;
}
protected void setUpView() {
mRecyclerView = (RecyclerView)mContentView.findViewById(R.id.rv_list);
}
protected void setta() {
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
initAdapter();
multipleItemAdapter.addHeaderView(getHeaderView());
multipleItemAdapter.addFooterView(getFooterView());
mRecyclerView.setAdapter(multipleItemAdapter);
}
private View getHeaderView() {
return getActivity().getLayoutInflater().inflate(R.layout.header_view_image, (ViewGroup) mRecyclerView.getParent(), false);
}
private View getFooterView() {
return getActivity().getLayoutInflater().inflate(R.layout.footer_view_text, (ViewGroup) mRecyclerView.getParent(), false);
}
private void initAdapter() {
multipleItemAdapter = new MultipleItemQuickAdapter(getContext(), getMultipleItemData());
}
private List
List
list.add(new MultipleItem(MultipleItem.ITEM_TYPE_1));
list.add(new MultipleItem(MultipleItem.ITEM_TYPE_2));
list.add(new MultipleItem(MultipleItem.ITEM_TYPE_3));
list.add(new MultipleItem(MultipleItem.ITEM_TYPE_4));
list.add(new MultipleItem(MultipleItem.ITEM_TYPE_5));
list.add(new MultipleItem(MultipleItem.ITEM_TYPE_6));
return list;
}
}
当大家看完这个简单的Demo后是不是发现有了BaseMultiItemQuickAdapter 即使在复杂的界面展示都不用怕了!哈哈,觉得好用就点赞分享哦!