美文网首页
RecyclerView通用Adapter简单封装

RecyclerView通用Adapter简单封装

作者: Tyhj | 来源:发表于2018-11-05 11:44 被阅读0次

    集成方法:

    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);
    

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

    相关文章

      网友评论

          本文标题:RecyclerView通用Adapter简单封装

          本文链接:https://www.haomeiwen.com/subject/cwtsxqtx.html