BRVAH中BaseMultiItemQuickAdapter使用注意事项

近期使用BRVAH框架进行小项目的开发,在多布局地方有些疑问。现在记录下

主要问题出在多布局addItemtype()这个方法中,经过思考,发现:如果你有多个布局文件需要在RecycleView中,那么你得根据官方文档提示创建一个实体类并且实现MultiItemEntity接口。(这里面你要设置类型就是type用于区分当前 数据模型 需要展示在那一种 item上)
之后使用 addItemtype(int type, int layoutResId) 根据开发工具的提示,在此方法中输入对应的参数。你有几种布局,就写几个addItemType,如果你的list中样式有10种,但你只写了8种,一定会报错的!!!
下面是我的小例子

public class MtestEntity implements MultiItemEntity {
    public String name;
    public int type;

//必须重写的方法,开发工具会提示你的
    @Override
    public int getItemType() {
        return type;
    }
}

上面是数据模型,我写的很简便。看明白就好0.0

 //多样式数据请求测试
    private void initMultiContent() {
        MtestEntity item = new MtestEntity();
        item.name = "yang";
        item.type = 1;
        MtestEntity item1 = new MtestEntity();
        item1.name = "yangxixi";
        item1.type = 3;
        MtestEntity item2 = new MtestEntity();
        item2.name = "fangfeizi";
        item2.type = 2;
        List<MtestEntity> list = new ArrayList<>();
        list.add(item);
        list.add(item1);
        list.add(item2);
        list.add(item2);
        list.add(item2);
        list.add(item);
        list.add(item1);
        list.add(item1);
        list.add(item2);
        list.add(item1);
        list.add(item2);
        list.add(item);
        list.add(item1);
        list.add(item2);
        list.add(item);
        list.add(item2);
        list.add(item1);
        list.add(item2);
        list.add(item2);
        list.add(item1);
        MultiItemAdapter adapter = new MultiItemAdapter(list);
        _rcv.setLayoutManager(new LinearLayoutManager(this));
        _rcv.setAdapter(adapter);
//记录一下,很重要,这个东西是实现分块展示不同不同布局的第二核心代码,
//少了这个,样式可能不会是想要的。
//本人使用的是V7包提供的,大家也可以使用adapter.setSpanSizeLookup(),由BRVAH提供一个这样的接口
//两者使用起来一样,没什么区别。下列方式必须在setAdapter之后写,否则不会调用。我也不知道原因。自己试试吧
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {//表示需要占据几个位置的span
                    //因为在声明gridlayoutManager的时候进行了设置,so每一行2个span
                    switch (adapter.getItemViewType(position)) {
                        case RecommendMultiItem.BANNER:
                            return 2;//占据两个位置的span
                        default:
                            return 1;//占据一个位置
                    }
                }
            });
    }

上面的代码是我写在activity中的,数据传入adapter的地方,以及数据形成的地方0.0

public class MultiItemAdapter extends BaseMultiItemQuickAdapter<MtestEntity, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public MultiItemAdapter(List<MtestEntity> data) {
        super(data);
        /**
         * addItemType中的type种类,必须和接收到的种类数目一模一样。
         * 种类:有几种type,就要写几个addItemType。少写或者错写,会直接报错!!!
         *  (android.content.res.Resources$NotFoundException: Resource ID *******)
         *  时刻注意!!!!
         *  例如:这个type有10种!。(type=1,2,3...10)你就得写
         *     addItemType(1, R.layout.item_test_one);
         *     addItemType(2, R.layout.item_test_two);
         *     addItemType(3, R.layout.item_test_two);
         *     ....
         *     addItemType(10, R.layout.item_test_two);
         *     漏写一个就会报错!!!血的教训啊!!!!
         */
        try {
            addItemType(1, R.layout.item_test_one);
            addItemType(2, R.layout.item_test_two);
            addItemType(3, R.layout.item_test_two);

        } catch (Exception ex) {
//请忽略这个try {}catch啊!这个加不加都行,。。我当时是为了捕获异常。。。结果没有捕获到
            Log.d("yang_multi", ex.getMessage());
        }

    }

    @Override
    protected void convert(BaseViewHolder helper, MtestEntity item) {
        int iii = helper.getItemViewType();
        switch (iii) {
            case 1:
                helper.setText(R.id.it_tv_one, item.name);
                break;
            default:
                helper.setText(R.id.it_tv_two, item.name);
                break;
        }
    }
}

上面的就是这个recycleview的adapter了!也是最关键的地方了。自己看吧。。
`
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.33'
这是引用的版本

看下效果图吧(做的demo的效果图不是上述代码的效果图哦)
第一个banner占用2个位置,下面的每个item占用1个位置。每行共2个位置


效果图,多布局展示.png

当前截图页面为fragment页面,下面是其对应的layout(只是fragment的layout哦,外层容器的就不说了,很简单)

<?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">
<!--我能说我忘记了是干什么的了吗?既然没有id就证明他是无关紧要的仅仅是效果而已-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@color/top_title_rank_tag_recommend" />
<!--这个也是吧?没id。估计是当做一条线了-->
    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="@color/nav_bottom_line" />
<!--这个不用说吧。android原生下拉刷新在外层,里面有个recycleview-->
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/rfr_swipe_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rfr_recycle"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

记录一下

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,376评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,399评论 19 139
  • 01 每天,我们的生活中总会有奇迹发生:有的人突然发了财,有的人遇到心爱之人,有的人奇迹生...
    小英成长手札阅读 4,136评论 3 9
  • c++中位操作操作符 这些位操作符只能用于整形的操作,其他会编译报错。位操作符的运算优先级比较低,因为尽量使用括号...
    Fengya阅读 3,968评论 0 50