RecyclerView通用Adapter简单封装

集成方法:

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 commonAdapter = new CommonAdapter(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 multiItemTypeSupport = new MultiItemTypeSupport() {

            @Override
            public int getLayoutId(int itemType) {
                return 0;//根据itemType返回的布局
            }

            @Override
            public int getItemViewType(int position, String s) {
                return 0;//设置ViewType
            }
        };

MultiItemCommonAdpter adpter = new MultiItemCommonAdpter(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简单封装)