Android智能的Adapter for RecyclerView,支持各种item类型的情况
动图演示如下:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
仅支持AndroidX
dependencies {
implementation 'com.github.ydstar:smartadapter:1.0.0'
}
public class DataAdapter extends CommonAdapter<String> {
public DataAdapter(Context context) {
super(context, R.layout.item_normal);
}
@Override
protected void convert(ViewHolder holder, String s, int position) {
holder.setText(R.id.tv, s);
}
}
只需要写自己的Adapter继承CommonAdapter,重写convert方法即可。
MultiItemTypeAdapter adapter = new MultiItemTypeAdapter(this,mDatas);
adapter.addItemViewDelegate(new User0Delegate());
adapter.addItemViewDelegate(new User1Delegate());
adapter.addItemViewDelegate(new User3Delegate());
每种Item类型对应一个ItemViewDelegate
public class UserAdapter extends MultiItemTypeAdapter<User> {
public UserAdapter(Context context, List<User> datas) {
super(context, datas);
addItemViewDelegate(new User0Delegate());
addItemViewDelegate(new User1Delegate());
addItemViewDelegate(new User3Delegate());
}
}
SectionRVAdapter adapter = new SectionRVAdapter(this);
adapter.addSection(new DataSection(this, "我是Section1", mStrList));
adapter.addSection(new DataSection(this, "我是Section2", mStrList));
adapter.addSection(new DataSection(this, "我是Section3", mStrList));
一个组对应一个list
public class DataSection extends StatelessSection {
private String title;
private List<String> strList;
private Context context;
public DataSection(Context context, String title, List<String> strList) {
super(R.layout.section_title, R.layout.section_content);
this.title = title;
this.context = context;
this.strList = strList;
}
}
SectionRVAdapter adapter = new SectionRVAdapter(this);
adapter.addSection(new DataSection(this, "我是Section1", mStrList));
adapter.addSection(new DataSection(this, "我是Section2", mStrList));
adapter.addSection(new DataSection(this, "我是Section3", mStrList));
mTopWrapper = new TopWrapper(adapter);
public class TopWrapper extends HeaderAndFooterWrapper {
public TopWrapper(RecyclerView.Adapter adapter) {
super(adapter);
View view = View.inflate(Main5Activity.this, R.layout.top, null);
addHeaderView(view);
addFootView(view);
}
}