HomeAdapter适配器

Android 开发中,适配器扮演了,非常重要的角色,作为组件与数据的桥梁,我们时常会用到适配器,//思路简明扼要莫说废话,总结,常用的适配器有四种,ArrayAdapter,SimpleAdapter,与数据库操作有关的常用SimpleCursorAdapter,剩下一种就是自定义适配器,自定义适配器在前边的文章中,已经详细概述了,适配器离不开,数据源布局和上下文,其实就是将集合中数据,拆成一条条放置在布局文件中,通过适配器给容器使用
在使用适配器的时候,我们可以自己新建布局对象,也引用系统的,常用的有,android.R.layout.simple_dropdown_item_1line, android.R.layout.simple_spinner_dropdown_item,若数据源是文本的话可以采用这两个布局,当然你也可以自己新建一个 但是不能有外部容器,ArrayAdapter 常用语文本,而SimpleAdapter 可以实现图文混排,可以自行设置布局,和自定义适配器之间数据源不同,自定义适配器数据源可随意,simpleAdapter适配器的数据源需要是盛有map集合的List
simpleCursorAdapter 适配器可以通过游标访问数据库,在使用SqLiteDataBase进行一系列的操作之后 返回值通常是Cursor ,此时可以用来构建,simpleCursor 适配器,最后将适配器给容器,(先给容器设置,最后,通过adapter.swap(simpleCursorAdapter adapter))的方式,更改数据源,此处和List 作为数据源不同,不需要,notify 操作。使用simpleCursorAdapter 适配器自定义布局时,和simpleAdapter 类似,但simpleCursorAdapter 的参数略有不同,new String[]{};数组中存放的是,你需要从数据库中查询的字段名,new int[]{},依旧是存放,自定义布局控件的id 值
需要注意的是,simpleCursorAdapter适配器有六个参数,最后一个flags 为观察者 SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER

public class HomeAdapter extends BaseQuickAdapter {

private Context context;

public HomeAdapter(int layoutResId, @Nullable List data, Context context) {
    super(layoutResId, data);
    this.context = context;
}

@Override
protected void convert(BaseViewHolder helper, HomeShowsBean item) {

    TextView textview = helper.itemView.findViewById(R.id.adapter_home_item_text);
    RelativeLayout relativeLayout = helper.itemView.findViewById(R.id.adapter_home_item_relative);
    textview.setText(item.getName());
    RecyclerView recyclerView = helper.itemView.findViewById(R.id.adapter_home_item_recycler);

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
    recyclerView.setLayoutManager(linearLayoutManager);

    HomecartAdapter adapter = new HomecartAdapter(item.getList(), context);
    if (item.getName().equals("热销新品")) {
        textview.setTextColor(Color.parseColor("#ff7f57"));
        recyclerView.setBackgroundResource(R.mipmap.bg_rxxp_syf);
        adapter.typeItem = 1;
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    } else if (item.getName().equals("魔力时尚")) {
        textview.setTextColor(Color.parseColor("#8789F7"));
        recyclerView.setBackgroundResource(R.mipmap.bg_mlss_syf);
        adapter.typeItem = 2;
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    } else if (item.getName().equals("品质生活")) {
        textview.setTextColor(Color.parseColor("#ff7f57"));
        recyclerView.setBackgroundResource(R.mipmap.bg_pzsh_syf);
        adapter.typeItem = 3;
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 2);
        recyclerView.setLayoutManager(gridLayoutManager);
    }
    recyclerView.setAdapter(adapter);
}

}

相信接触过开发的人一定会接触ListView,相信接触过ListView的人一定接触过BaseAdapter,相信接触过BaseAdapter的人一定很痛恨编写适配器,因为它的数量往往跟ListView的总数是成正比的,所以我今天给大家介绍一款神器,万能适配器!
在自定义适配器的时候,我们通常会自定义一个ViewHolder类,虽然这个名字是随便起的,但是就像约定俗成一样,我们还是这么用了这么多年!当然,自定义万能适配器,从它-ViewHolder开始,我们把所有与ViewHolder有关的操作都封装起来,这样在我们的适配器中,getView中的操作就会简单很多。

public class HomecartAdapter extends BaseQuickAdapter {

private Context context;
// 热销新品
public static final int TYPE_ITEM_1 = 1;
// 魔力时尚
public static final int TYPE_ITEM_2 = 2;
// 品质生活
public static final int TYPE_ITEM_3 = 3;


public int typeItem = 1;

public HomecartAdapter(List data, @Nullable Context context) {
    super(data);
    this.context = context;
    setMultiTypeDelegate(new MultiTypeDelegate() {
        @Override
        protected int getItemType(HomeShowBean homeShowBean) {
            return typeItem == 1 ? TYPE_ITEM_1 : (typeItem == 2 ? TYPE_ITEM_2 : TYPE_ITEM_3);
        }
    });
    getMultiTypeDelegate()
            .registerItemType(TYPE_ITEM_1, R.layout.adapter_item_recycler_rxxp)
            .registerItemType(TYPE_ITEM_2, R.layout.adapter_item_recycler_mlss)
            .registerItemType(TYPE_ITEM_3, R.layout.adapter_item_recycler_pzsh);
}

@Override
protected void convert(BaseViewHolder helper, HomeShowBean item) {
    int viewType = helper.getItemViewType();
    switch (viewType) {
        case TYPE_ITEM_1:
            helper.setText(R.id.home_item_recycler_rxxp_name, item.getCommodityName());
            helper.setText(R.id.home_item_recycler_rxxp_price, "¥" + item.getPrice());
            SimpleDraweeView simple = helper.itemView.findViewById(R.id.home_item_recycler_rxxp_simple);
            simple.setImageURI(item.getMasterPic());
            break;
        case TYPE_ITEM_2:
            helper.setText(R.id.home_item_recycler_mlss_name, item.getCommodityName());
            helper.setText(R.id.home_item_recycler_mlss_price, "¥" + item.getPrice());
            SimpleDraweeView simple1 = helper.itemView.findViewById(R.id.home_item_recycler_mlss_simple);
            simple1.setImageURI(item.getMasterPic());
            break;
        case TYPE_ITEM_3:
            helper.setText(R.id.home_item_recycler_pzsh_name, item.getCommodityName());
            helper.setText(R.id.home_item_recycler_pzsh_price, "¥" + item.getPrice());
            SimpleDraweeView simple2 = helper.itemView.findViewById(R.id.home_item_recycler_pzsh_simple);
            simple2.setImageURI(item.getMasterPic());
            break;
    }
}

}

你可能感兴趣的:(HomeAdapter适配器)