RecyclerView通用Adapter简单封装

2018-11-05  本文已影响0人  Tyhj

集成方法:

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更加好用

上一篇下一篇

猜你喜欢

热点阅读