BaseQuickAdapter万能适配器

BaseQuickAdapter的有机以及作用域:
(1)减少重复 Adapter 代码
(2)添加 Item 的点击事件,长按事件以及子控件的点击事件
(3)添加头部、尾部,下拉刷新、上拉加载(上拉加载的5种加载更多动画任你选择,后期会添加更多的加载动画)、没有更多数据
(4)可以自定义头部、尾部、加载更多布局
(5)添加 Item滑动动画 (9种动画切换,轻松一行代码)
(6)添加新增、删除 Item动画 (目前支持默认的动画方式)
(7)网格,列表,流式随意切换
(8)添加空布局(列表无数据时,显示更加人性化)
(9)拖拽和侧滑删除
(10)支持多类型布局
(11)类似淘宝列表切换
(12)字母导航
(13)类似探探翻牌

BaseQuickAdapterd的实现步骤:
1.首先在 项目的build中导入 maven { url ‘https://jitpack.io’} 依赖
在这里插入图片描述
2.在modle的build中导入implementation ‘com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.42’(最好在导入一个implementation ‘com.android.support:recyclerview-v7:28.0.0’ 适配器一般与recyclerview配套使用)
在这里插入图片描述
3.继承BaseQuickAdapter后填写数据类型:
(1)第一个参数:提供数据的Bean类
(2)第二个参数:BaseViewHolder
在这里插入图片描述
Activity中的代码:

 private RecyclerView recyclerView_a,recyclerView_b,recyclerView_c;
    private List<BeanDate>list=new ArrayList<>();
    private List<BeanDate>list_t=new ArrayList<>();
    private MyAdapter myAdapter_one;
    private MyAdapter myAdapter_two;
    private List<JsonBean.DataBean>list_json=new ArrayList<>();
    private String path="https://www.apiopen.top/novelApi";
    private MyAdapter_two myAdapter_three;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        find();
        //数据的获取
        a();
        //展示
        show();
    }

    private void a() {
        OkGo.<String>get(path).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String body = response.body();
                List<JsonBean.DataBean> data = new Gson().fromJson(body, JsonBean.class).getData();
                list_json.addAll(data);
                myAdapter_three.notifyDataSetChanged();
            }
        });
    }

    private void show() {
        Log.i("!","开始展示数据");
        //设置适配器
        myAdapter_one=new MyAdapter(list);
        myAdapter_two=new MyAdapter(list_t);
        myAdapter_three=new MyAdapter_two(R.layout.three,list_json);

        recyclerView_a.setAdapter(myAdapter_one);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView_a.setLayoutManager(linearLayoutManager);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        DividerItemDecoration d_1 = new DividerItemDecoration(MainActivity.this, RecyclerView.HORIZONTAL);
        recyclerView_a.addItemDecoration(d_1);

        recyclerView_b.setAdapter(myAdapter_two);
        recyclerView_b.setLayoutManager(new GridLayoutManager(MainActivity.this, 4));

        recyclerView_c.setAdapter(myAdapter_three);
        LinearLayoutManager a = new LinearLayoutManager(this);
        recyclerView_c.setLayoutManager(a);
        a.setOrientation(LinearLayoutManager.HORIZONTAL);
        DividerItemDecoration d_2 = new DividerItemDecoration(MainActivity.this, RecyclerView.HORIZONTAL);
        recyclerView_c.addItemDecoration(d_2);
        myAdapter_three.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                int id = view.getId();
                switch (id){
                    case R.id.three_img:
                        Toast.makeText(MainActivity.this, "图片", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.three_text:
                        Toast.makeText(MainActivity.this, ""+list_json.get(position).getBookname().toString(), Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }

    //初始化控件
    private void find() {
        Log.i("!","初始化控件");
        recyclerView_a=findViewById(R.id.main_re_one);
        recyclerView_b=findViewById(R.id.main_re_two);
        recyclerView_c=findViewById(R.id.main_re_three);
        list.add(new BeanDate("000",R.mipmap.ic_launcher,BeanDate.zero));
        list.add(new BeanDate("000",R.mipmap.ic_launcher,BeanDate.zero));
        list.add(new BeanDate("000",R.mipmap.ic_launcher,BeanDate.zero));
        list.add(new BeanDate("000",R.mipmap.ic_launcher,BeanDate.zero));
        list.add(new BeanDate("000",R.mipmap.ic_launcher,BeanDate.zero));
        list_t.add(new BeanDate("123",R.mipmap.ic_launcher,BeanDate.one));
        list_t.add(new BeanDate("123",R.mipmap.ic_launcher,BeanDate.one));
        list_t.add(new BeanDate("123",R.mipmap.ic_launcher,BeanDate.one));
        list_t.add(new BeanDate("123",R.mipmap.ic_launcher,BeanDate.one));
        list_t.add(new BeanDate("123",R.mipmap.ic_launcher,BeanDate.one));
        list_t.add(new BeanDate("123",R.mipmap.ic_launcher,BeanDate.one));
        list_t.add(new BeanDate("123",R.mipmap.ic_launcher,BeanDate.one));
        list_t.add(new BeanDate("123",R.mipmap.ic_launcher,BeanDate.one));


    }

适配器代码:
单布局:

public class MyAdapter_two extends BaseQuickAdapter<JsonBean.DataBean, BaseViewHolder> {

public MyAdapter_two(int layoutResId, @Nullable List<JsonBean.DataBean> data) {
    super(layoutResId, data);
}

@Override
protected void convert(BaseViewHolder helper, JsonBean.DataBean item) {
    ImageView img_3 = helper.getView(R.id.three_img);
    Glide.with(mContext).load(item.getBook_cover()).into(img_3);
    helper.setText(R.id.three_text,item.getBookname());
    //自视图的点击事件
    helper.addOnClickListener(R.id.three_img);
    helper.addOnClickListener(R.id.three_text);
}

}
多布局:

public class MyAdapter extends BaseQuickAdapter<BeanDate, BaseViewHolder> {

    public MyAdapter(@Nullable List<BeanDate> data) {
        super(data);
        //设置布局
        setMultiTypeDelegate(new MultiTypeDelegate<BeanDate>() {
            @Override
            protected int getItemType(BeanDate beanDate) {
                return beanDate.getType();
            }
        });
        //添加布局
        getMultiTypeDelegate()
                .registerItemType(BeanDate.zero,R.layout.one)
                .registerItemType(BeanDate.one,R.layout.two);
    }

    @Override
    protected void convert(BaseViewHolder helper, BeanDate item) {
        //获取势图
        int itemViewType = helper.getItemViewType();
        //判断视图
        switch (itemViewType){
            case BeanDate.zero:
                ImageView img_1 = helper.getView(R.id.one_img);
                Glide.with(mContext).load(item.getPic()).apply(new RequestOptions().circleCrop()).into(img_1);
                helper.setText(R.id.one_text,item.getMessage());
                break;
            case BeanDate.one:
                ImageView img_2 = helper.getView(R.id.two_img);
                Glide.with(mContext).load(item.getPic()).apply(new RequestOptions().circleCrop()).into(img_2);
                helper.setText(R.id.two_text,item.getMessage());
                break;
        }

    }
}

别人写的很全面:
http://www.jianshu.com/p/b343fcff51b0
http://www.jianshu.com/p/a00b4a3a1838

  • 6
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值