42.android 简单的万能适配器+多布局

//万能适配器

//第一步Project下 配置maven { url "https://jitpack.io" }


allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

//第二步 导依赖


implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'

//第三步  开始适配

//初始化

LinearLayoutManager manager = new LinearLayoutManager(getContext());
mReZhiBoZhongGuoTianJia = (RecyclerView) findViewById(R.id.mReZhiBoZhongGuoTianJia);
mReZhiBoZhongGuoTianJia.setLayoutManager(manager);
//这里第一个参数放的是你要适配的布局,第二个是你要适配的数据
ZhiBoZhongGuoApaderTianJia zhiBoZhongGuoApaderTianJia = new ZhiBoZhongGuoApaderTianJia(R.layout.zhi_bo_zhong_guo2, mZhongGuoList);
mReZhiBoZhongGuoTianJia.setAdapter(zhiBoZhongGuoApaderTianJia);

//第四步 适配器,继承BaseQuickAdapter,第一个参数放的是你要解析的Bean类,第二个固定就是BaseViewHolder就行

public class ZhiBoZhongGuoApaderTianJia extends BaseQuickAdapter<zhibozhongguoyemian,BaseViewHolder>{
    private RecyclerView mZhiBoZhongGuoTianjiaRe1;
    private RecyclerView mZhiBoZhongGuoTianjiaRe2;

//重写的方法
    public ZhiBoZhongGuoApaderTianJia(int layoutResId, @Nullable List<zhibozhongguoyemian> data) {
        super(layoutResId, data);
    }

//重写的方法bean类数据在item里
    @Override
    protected void convert(BaseViewHolder helper, zhibozhongguoyemian item) {
        List<zhibozhongguoyemian.TablistBean> tablist = item.getTablist();
        ArrayList<String> mList1=new ArrayList<>();
        for (int i = 0; i <tablist.size() ; i++) {
            zhibozhongguoyemian.TablistBean tablistBean = tablist.get(i);
            String title = tablistBean.getTitle();
            mList1.add(title);
        }


        List<zhibozhongguoyemian.AlllistBean> alllist = item.getAlllist();
        ArrayList<String> mList2=new ArrayList<>();
        for (int i = 0; i <alllist.size() ; i++) {
            zhibozhongguoyemian.AlllistBean alllistBean = alllist.get(i);
            String title = alllistBean.getTitle();
            mList2.add(title);
        }

        mZhiBoZhongGuoTianjiaRe1 = helper.getView(R.id.mZhiBoZhongGuoTianjiaRe1);
        mZhiBoZhongGuoTianjiaRe2 = helper.getView(R.id.mZhiBoZhongGuoTianjiaRe2);

//底下这俩是我又写了适配器,适配了两次,因为布局里,设置的是上下两部分不同

        GridLayoutManager gridLayoutManager1 = new GridLayoutManager(mContext, 3);
        mZhiBoZhongGuoTianjiaRe1.setLayoutManager(gridLayoutManager1);
        ZhiBoZhongGuoApaderTianJia2 zhiBoZhongGuoApaderTianJia2 = new ZhiBoZhongGuoApaderTianJia2(R.layout.zhi_bo_zhong_guo_tianjia, mList1);
        mZhiBoZhongGuoTianjiaRe1.setAdapter(zhiBoZhongGuoApaderTianJia2);


        GridLayoutManager gridLayoutManager2 = new GridLayoutManager(mContext, 3);
        mZhiBoZhongGuoTianjiaRe2.setLayoutManager(gridLayoutManager2);
        ZhiBoZhongGuoApaderTianJia2 zhiBoZhongGuoApaderTianJia2s = new ZhiBoZhongGuoApaderTianJia2(R.layout.zhi_bo_zhong_guo_tianjia, mList2);
        mZhiBoZhongGuoTianjiaRe2.setAdapter(zhiBoZhongGuoApaderTianJia2s);


        //x的点击事件,这是万能适配器的添加点击事件

        helper.addOnClickListener(R.id.mZhiBoZhongGuoX);

    }
}

//第四步 返回你的Activity里通过适配器得到点击事件

//x的点击事件

zhiBoZhongGuoApaderTianJia.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        if (view.getId()==R.id.mZhiBoZhongGuoX){
            finish();
        }
    }
});

//第五步 顺便把我的布局贴出来

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    >

    <ImageView
        android:id="@+id/mZhiBoZhongGuoX"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:src="@mipmap/live_china_delect_channel" />

    <RelativeLayout
        android:layout_marginTop="20dp"
        android:layout_below="@+id/mZhiBoZhongGuoX"
        android:id="@+id/mZhiBoZhongGuoRE"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:textSize="20sp"
            android:textColor="#FFFFFF"
            android:text="切换栏目"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


        <TextView
            android:textSize="20sp"
            android:background="#FFFFFF"
            android:text="  编辑  "
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>


    <android.support.v7.widget.RecyclerView
        android:layout_marginTop="20dp"
        android:layout_below="@+id/mZhiBoZhongGuoRE"
        android:id="@+id/mZhiBoZhongGuoTianjiaRe1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>


    <TextView
        android:layout_marginTop="20dp"
        android:textSize="20sp"
        android:id="@+id/mXXXXX"
        android:textColor="#FFFFFF"
        android:layout_below="@+id/mZhiBoZhongGuoTianjiaRe1"
        android:text="点击添加更多栏目"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <android.support.v7.widget.RecyclerView
        android:layout_marginTop="20dp"
        android:layout_below="@+id/mXXXXX"
        android:id="@+id/mZhiBoZhongGuoTianjiaRe2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

//-----------------------------------多布局 ------------------------------

//你的activity里或者fragment里适配

 LinearLayoutManager manager = new LinearLayoutManager(getContext());
            mFuYong_RecyclerView.setLayoutManager(manager);
//这里是你要适配的数据,多布局只需要一个参数就行了
            ShouYeShipei shouYeShipei = new ShouYeShipei( newList);
            mFuYong_RecyclerView.setAdapter(shouYeShipei);

//------------------------继承的是BaseMultiItemQuickAdapter

//首先在Bean类里添加int类型的字段,用来返回一个int值,在适配器里判断这个int值适配布局。

//我是在FuYongShuJu_Bean.DataBean.NewListBean里添加的。

public static final int ONE = 0;
            public static final int TWO = 1;
            public static  final int THREE = 2;
            public static final int FOUR = 3;
            public static final int FIVE=4;



//判断getLayoutType()返回int 值,这个getLayoutType()是串里的字段。


            @Override
            public int getItemType() {
                switch (getLayoutType()){
                    case "0":
                        return ONE;
                    case "1":
                        return TWO;
                    case "2":
                        return THREE;
                    case "3":
                        return FOUR;
                }
//                if (getLayoutType().equals(0)){
//                    return ONE;
//                }
                return FIVE;
            }

//----------------------------------------------适配器----------------------------------------------

public class ShouYeShipei extends BaseMultiItemQuickAdapter<FuYongShuJu_Bean.DataBean.NewListBean,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
     */
//-----------通过itemType添加5个布局,通过Bean类可以调用出写的这个int类型的值
    public ShouYeShipei(List<FuYongShuJu_Bean.DataBean.NewListBean> data) {
        super(data);
        addItemType(FuYongShuJu_Bean.DataBean.NewListBean.ONE,R.layout.shouye_biaoti);
        addItemType(FuYongShuJu_Bean.DataBean.NewListBean.TWO,R.layout.shouye_zuoziyoutu);
        addItemType(FuYongShuJu_Bean.DataBean.NewListBean.THREE,R.layout.shouye_shuju);
        addItemType(FuYongShuJu_Bean.DataBean.NewListBean.FOUR,R.layout.shouye_shangzixiasantu);
        addItemType(FuYongShuJu_Bean.DataBean.NewListBean.FIVE,R.layout.shouye_shangzixiashiping);
    }

//-----------------在这里判断如果是那个布局的话,就开始适配那个布局
    @Override
    protected void convert(BaseViewHolder helper, FuYongShuJu_Bean.DataBean.NewListBean item) {
        switch (helper.getItemViewType()) {
            case FuYongShuJu_Bean.DataBean.NewListBean.ONE:
                helper.setText(R.id.mShouYe_BiaoTi, item.getTitle());
                helper.setText(R.id.mShouYe_BiaoTi_LaiYua, item.getOrigin());
                helper.setText(R.id.mShouYe_BiaoTi_ShiJian, item.getPublishTime());
                break;
            case FuYongShuJu_Bean.DataBean.NewListBean.TWO:
                helper.setText(R.id.mShouYe_ZuoZiYouTu_LaiYua, item.getOrigin());
                helper.setText(R.id.mShouYe_ZuoZiYouTu_ShiJian, item.getPublishTime());
                helper.setText(R.id.mShouYe_ZuoZiYouTu_Text, item.getTitle());
                Glide.with(mContext).load(item.getImageListThumb().get(0)).into((ImageView) helper.getView(R.id.mShouYe_ZuoZiYouTu_Img));
                break;
            case FuYongShuJu_Bean.DataBean.NewListBean.THREE:
                helper.setText(R.id.mShouYe_Text1_LaiYua, item.getOrigin());
                helper.setText(R.id.mShouYe_Text1_ShiJian, item.getPublishTime());
                helper.setText(R.id.mShouYe_Text1, item.getTitle());
                Glide.with(mContext).load(item.getImageListThumb().get(0)).into((ImageView) helper.getView(R.id.mShouYe_Img1));
                break;
            case FuYongShuJu_Bean.DataBean.NewListBean.FOUR:
                helper.setText(R.id.mShouYe_ShangZiXiaSanTu_LaiYua, item.getOrigin());
                helper.setText(R.id.mShouYe_ShangZiXiaSanTu_ShiJian, item.getPublishTime());
                helper.setText(R.id.mShouYe_ShangZiXiaSanTu_Text, item.getTitle());
                Glide.with(mContext).load(item.getImageListThumb().get(0)).into((ImageView) helper.getView(R.id.mShouYe_ShangZiXiaSanTu_Img1));
                Glide.with(mContext).load(item.getImageListThumb().get(1)).into((ImageView) helper.getView(R.id.mShouYe_ShangZiXiaSanTu_Img2));
                Glide.with(mContext).load(item.getImageListThumb().get(2)).into((ImageView) helper.getView(R.id.mShouYe_ShangZiXiaSanTu_Img3));
                break;
            case FuYongShuJu_Bean.DataBean.NewListBean.FIVE:
                helper.setText(R.id.mShouye_ShangZiXiaShiPin_LaiYua, item.getOrigin());
                helper.setText(R.id.mShouye_ShangZiXiaShiPin_ShiJian, item.getPublishTime());
                helper.setText(R.id.mShouye_ShangZiXiaShiPin_Text, item.getTitle());
                break;

        }
    }
}

//---------

//----------------------------------------------------------------------完----------------------------------------------------------------------------

猜你喜欢

转载自blog.csdn.net/weixin_42061754/article/details/81937603