RecycleView的二级列表

 
allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
        maven { url "http://lib.gcssloop.com/repository/gcssloop-central/" }
    }
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    compile 'com.android.support:recyclerview-v7:26.1.0'
    compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    compile 'com.gcssloop.widget:rclayout:1.4.1@aar'

}

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

public class MainActivity extends AppCompatActivity {
    ArrayList res;
    List lv0 = new ArrayList<>();
    RecyclerView mrecycleView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mrecycleView = findViewById(R.id.rv);
        addData();
        RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
        mrecycleView.setAdapter(new Adapter(res));
        mrecycleView.setLayoutManager(manager);
    }

    private void addData() {
        res = new ArrayList<>();
        lv0.clear();
        lv0.add(new Levelzero("我的好友"));
        lv0.add(new Levelzero("家人"));
        for(int i = 0;i<5;i++){
            Levelone lv1 = new Levelone("好友名称",R.drawable.doghead);
            lv0.get(0).addSubItem(lv1);
        }
        for(int i = 0;i<5;i++){
            Levelone lv1 = new Levelone("家人名称",R.drawable.doghead);
            lv0.get(1).addSubItem(lv1);
        }
        for (int j = 0; j < lv0.size(); j++) {
            res.add(lv0.get(j));
        }
        return ;
    }
}



    

        

            
        

        

        
    

    


 

public class Levelone implements MultiItemEntity {

    public String friendName;
    public int friendSculpture;

    public Levelone(String friendName, int friendSculpture) {
        this.friendName = friendName;
        this.friendSculpture = friendSculpture;
    }

    @Override
    public int getItemType() {
        return Adapter.TYPE_LEVEL_1;
    }
}

 

public class Levelzero extends AbstractExpandableItem implements MultiItemEntity {

    public String friendGroup;

    public Levelzero(String friendName) {
        this.friendGroup = friendName;
    }

    @Override
    public int getLevel() {
        return 0;
    }

    @Override
    public int getItemType() {
        return Adapter.TYPE_LEVEL_0;
    }
}

 

public class Adapter extends BaseMultiItemQuickAdapter {
    public static final int TYPE_LEVEL_0 = 0;
    public static final int TYPE_LEVEL_1 = 1;

    public Adapter(List data) {
        super(data);
        addItemType(TYPE_LEVEL_0, R.layout.levelzero);
        addItemType(TYPE_LEVEL_1, R.layout.levelone);
    }

    @Override
    protected void convert(final BaseViewHolder helper, MultiItemEntity item) {
        switch (helper.getItemViewType()) {
            case 0:
                final Levelzero lv0 = (Levelzero) item;
                helper.setText(R.id.Lv0_tv, lv0.friendGroup);
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = helper.getAdapterPosition();
                        if (lv0.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });
                break;
            case 1:
                final Levelone lv1 = (Levelone) item;
                helper.setImageResource(R.id.Lv1_iv, lv1.friendSculpture);
                helper.setText(R.id.title_one, lv1.friendName);
                break;
        }
    }
}

 

 



    
        
    
    


    


项目中的build.gradle文件

效果图

RecycleView的二级列表_第1张图片

github代码Domel

https://github.com/1357297270/recycleViewdemo

 

你可能感兴趣的:(RecycleView的二级列表)