@Tyhj
2018-11-05T11:43:46.000000Z
字数 1143
阅读 1213
Android
Step 1. Add the JitPack repository to your build file
//Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
//Add the dependency
dependencies {
implementation 'com.github.tyhjh:CommonAdapter:v1.0.0'
}
1.简单使用
CommonAdapter<String> commonAdapter = new CommonAdapter<String>(MainActivity.this, stringList, R.layout.item_test) {
@Override
public void onBindView(CommonViewHolder holder, String s) {
holder.setText(R.id.tv, s);
}
};
rv.setAdapter(commonAdapter);
2.添加viewType使用
MultiItemTypeSupport<String> multiItemTypeSupport = new MultiItemTypeSupport<String>() {
@Override
public int getLayoutId(int itemType) {
return 0;//根据itemType返回的布局
}
@Override
public int getItemViewType(int position, String s) {
return 0;//设置ViewType
}
};
MultiItemCommonAdpter<String> adpter = new MultiItemCommonAdpter<String>(MainActivity.this, stringList, multiItemTypeSupport) {
@Override
public void onBindView(final CommonViewHolder holder, String s) {
if (holder.getItemViewType() == 0) {
holder.setText(R.id.tv, s);
} else {
holder.setText(R.id.tv_txt, s);
}
}
};
rv.setAdapter(commonAdapter);