RecyclerView万能适配器
一:BaseRecyclerViewAdapterHelper介绍
BaseRecyclerViewAdapterHelper是Github上为实现方便使用RecyclerView而开发的一个框架,使用较为简单方便,
能实现RecyclerView的下拉加载,Item点击事件,Item子控件的点击事件,Item加载动画等等
该适配器虽号称万能适配器,但并非万能,有很多功能本身是实现不了的,还会和其他开源的冲突,比如不能和XRecyclerView一起使用
配置
在 build.gradle 的 repositories 添加:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
然后在Module的 build.gradle(Module:app) 的 dependencies 添加:
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:v1.9.7'
二:使用方法
1,单类型BaseQuickAdapter使用
适配器
public class MyQuickAdapter extends BaseQuickAdapter<String> {
private int layoutId;
private List<String> list;
public MyQuickAdapter(int layoutId,List<String> list){
super(layoutId,list);
this.layoutId = layoutId;
this.list = list;
}
@Override
protected void convert(BaseViewHolder baseViewHolder, String s) {
//holder里有控件 showText
baseViewHolder.setText(R.id.title_id,s);
}
}
Activity
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerViewId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerViewId = (RecyclerView) findViewById(R.id.recyclerView_id);
//线性
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(RecyclerView.VERTICAL);
recyclerViewId.setLayoutManager(manager);
List<String> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add("蜡笔小新"+i);
}
MyQuickAdapter myQuickAdapter = new MyQuickAdapter(R.layout.item_layout, list);
recyclerViewId.setAdapter(myQuickAdapter);
}
}
多类型BaseMultiItemQuickAdapter的用法:
public class RVAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
ArrayList<Bean> datas;
public RVAdapter(ArrayList<Bean> datas) {
this.datas = datas;
}
@Override
public int getItemViewType(int position) {
if (position % 2 == 0){//偶数返回0
return 0;
}else {
return 1;
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType =&#