万能适配器多布局BaseMultiItemQuickAdapter简单用法,适合新手小白

万能适配器多布局BaseMultiItemQuickAdapter简单用法,适合新手小白,第1张

//先贴一下Bean类需要重写MultiItemEntity,这个重写之后会有一个方法,我这里写了一个Index Integer类型的。然后配置一下get和set方法。记得return 返回Index

public class FoodsBeanimplements MultiItemEntity {

    private int ret

    private Listdata

    public int getRet() {

return ret

    }

public void setRet(int ret) {

this.ret = ret

    }

public ListgetData() {

return data

    }

public void setData(List data) {

this.data = data

    }

@Override

    public int getItemType() {

return 0

    }

public static class DataBeanimplements MultiItemEntity {

/**

* id : 8289

        * title : 油焖大虾

        * pic : http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg

* collect_num : 1670

        * food_str : 大虾 葱 生姜 植物油 料酒

        * num : 1670

*/

        private Stringid

        private Stringtitle

        private Stringpic

        private Stringcollect_num

        private Stringfood_str

        private int num

        private Integerindex

        public IntegergetIndex() {

return index

        }

public void setIndex(Integer index) {

this.index = index

        }

public StringgetId() {

return id

        }

public void setId(String id) {

this.id = id

        }

public StringgetTitle() {

return title

        }

public void setTitle(String title) {

this.title = title

        }

public StringgetPic() {

return pic

        }

public void setPic(String pic) {

this.pic = pic

        }

public StringgetCollect_num() {

return collect_num

        }

public void setCollect_num(String collect_num) {

this.collect_num = collect_num

        }

public StringgetFood_str() {

return food_str

        }

public void setFood_str(String food_str) {

this.food_str = food_str

        }

public int getNum() {

return num

        }

public void setNum(int num) {

this.num = num

        }

@Override

        public int getItemType() {

return index

        }

}

}

//我的布局比较简单两个布局一个里边是一个按钮一个里边是一个texView

public class Adapterextends BaseMultiItemQuickAdapter {

public Adapter(@Nullable List data) {

super(data)

//添加自己的多布局

 addItemType(0,R.layout.day8_recitem)

        addItemType(1,R.layout.day8_recitemmain)

    }

@Override

    protected void convert(@NotNull BaseViewHolder baseViewHolder, FoodsBean.DataBean dataBean) {

这里的dataBean的getIndex就是刚刚Bean类自己设置的Integer类型。

switch (dataBean.getIndex()){

case 1:

//数据源绑定控件和正常的ViewHolder一样

baseViewHolder.setText(R.id.day8_text,dataBean.getFood_str())

break

            case 0:

baseViewHolder.setText(R.id.day8_btn,dataBean.getTitle())

break

        }

}

}

public class Day8Activityextends AppCompatActivity {

private RecyclerViewday8Rec

    private ListmyData=new ArrayList<>()

    private Adapteradapter

    int a=1

    @Override

    protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_day8)

        initView()

        day8Rec.setLayoutManager(new LinearLayoutManager(this))

        //Get

        OkGo.get("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1").execute(new StringCallback() {

@Override

            public void onSuccess(Response response) {

String string = response.body()

                Gson gson=new Gson()

//解析数据

                FoodsBean foodsBean = gson.fromJson(string, FoodsBean.class)

                myData=foodsBean.getData()

                adapter =new Adapter(myData)

                day8Rec.setAdapter(adapter)

//这里比较重要我这里设置的a=1;这里和adapter的绑定控件添加数据源有关,同时还设置了要选中的布局是哪一个

                for (int i =0i

myData.get(i).setIndex(a%2)

                a++

                }

adapter =new Adapter(myData)

                day8Rec.setAdapter(adapter)

            }

})

    }

private void initView() {

day8Rec = (RecyclerView) findViewById(R.id.day8_rec)

    }

}

//最后贴下布局自己比较简单,布局有点丑不要介意

//mainAct

    android:orientation="vertical"

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".day8Exam.Day8Activity">

        android:background="@color/colorPrimary"

        android:id="@+id/day8_rec"

        android:layout_width="match_parent"

        android:layout_height="match_parent">

</LinearLayout>

//两个ITEM的

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    xmlns:app="http://schemas.android.com/apk/res-auto">

        android:id="@+id/day8_btn"

        android:text="发送"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        tools:ignore="MissingConstraints">

</androidx.constraintlayout.widget.ConstraintLayout>

//第二个

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="wrap_content">

    android:id="@+id/day8_text"

    android:text="你好"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    tools:ignore="MissingConstraints">

</androidx.constraintlayout.widget.ConstraintLayout>

给Listview里面Item里的每一个控件怎么设定点击事件

adpter用的是继承的baseadapter,给整个listview 上的 Item 设置点击事件就用listview.setOnItemClickListener(listener)就可以, 下面介绍 如果 给 I tem里面某一 控件设置点击事件。方法是重写 Adapter。

ListView工作原理(针对下面代码):

1. 给ListView设置数据适配器,此处程序是自己重写的Adapter,创建Adapter 的时候主要做下面的工作:

(1)把ListView需要显示的数据传给Adapter

(2)把ListView显示Item的界面传给Adapter

(3)把上下文对象传给Adapter,主要用来得到LayoutInflater对象来得到Item界面

2. 给ListView设置当点击Item对象的时候执行的 *** 作,此处要实现给Item中的各项也就是该程序的三个TextView设置监听器,也就是调用Adapter中的getView函数。

3. getView函数实现的 *** 作是:第一要将Item要显示的数据显示,然后设置监听器,为监听器设置 *** 作。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11683862.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存