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 Listlist=new ArrayList<>();
private Listlist_t=new ArrayList<>();
private MyAdapter myAdapter_one;
private MyAdapter myAdapter_two;
private Listlist_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.get(path).execute(new StringCallback() {
@Override
public void onSuccess(Response response) {
String body = response.body();
List 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
public MyAdapter_two(int layoutResId, @Nullable List 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 {
public MyAdapter(@Nullable List data) {
super(data);
//设置布局
setMultiTypeDelegate(new MultiTypeDelegate() {
@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