[关闭]
@Tyhj 2018-11-05T03:43:46.000000Z 字数 1143 阅读 693

RecyclerView通用Adapter简单封装

Android


集成方法:

Step 1. Add the JitPack repository to your build file

  1. //Add it in your root build.gradle at the end of repositories:
  2. allprojects {
  3. repositories {
  4. ...
  5. maven { url 'https://jitpack.io' }
  6. }
  7. }

Step 2. Add the dependency

  1. //Add the dependency
  2. dependencies {
  3. implementation 'com.github.tyhjh:CommonAdapter:v1.0.0'
  4. }

基本使用

1.简单使用

  1. CommonAdapter<String> commonAdapter = new CommonAdapter<String>(MainActivity.this, stringList, R.layout.item_test) {
  2. @Override
  3. public void onBindView(CommonViewHolder holder, String s) {
  4. holder.setText(R.id.tv, s);
  5. }
  6. };
  7. rv.setAdapter(commonAdapter);

2.添加viewType使用

  1. MultiItemTypeSupport<String> multiItemTypeSupport = new MultiItemTypeSupport<String>() {
  2. @Override
  3. public int getLayoutId(int itemType) {
  4. return 0;//根据itemType返回的布局
  5. }
  6. @Override
  7. public int getItemViewType(int position, String s) {
  8. return 0;//设置ViewType
  9. }
  10. };
  11. MultiItemCommonAdpter<String> adpter = new MultiItemCommonAdpter<String>(MainActivity.this, stringList, multiItemTypeSupport) {
  12. @Override
  13. public void onBindView(final CommonViewHolder holder, String s) {
  14. if (holder.getItemViewType() == 0) {
  15. holder.setText(R.id.tv, s);
  16. } else {
  17. holder.setText(R.id.tv_txt, s);
  18. }
  19. }
  20. };
  21. rv.setAdapter(commonAdapter);

参考文章:为RecyclerView打造通用Adapter 让RecyclerView更加好用

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注