1. 程式人生 > >RecycleView的二級列表

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:
[email protected]
' }

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

public class MainActivity extends AppCompatActivity {
    ArrayList<MultiItemEntity> res;
    List<Levelzero> 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 ;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.mycompany.recyeleviewdemo1.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:background="#F6F7F9"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="30dp"
                android:layout_weight="6"
                android:gravity="center"
                android:text="聯絡人"
                android:textSize="25dp" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#D4D4D4">

        </View>
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>

</LinearLayout>
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<Levelone> 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<MultiItemEntity, BaseViewHolder> {
    public static final int TYPE_LEVEL_0 = 0;
    public static final int TYPE_LEVEL_1 = 1;

    public Adapter(List<MultiItemEntity> 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;
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:background="#ffffff"
    android:layout_marginBottom="2dp"
    >
    <com.gcssloop.widget.RCRelativeLayout
        android:layout_width="40dp"
        android:layout_height="40dp"
        app:round_corner="10dp"
        >
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/Lv1_iv"
            android:scaleType="centerCrop"
            />
    </com.gcssloop.widget.RCRelativeLayout>
    <TextView
        android:id="@+id/title_one"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:textColor="@android:color/black"
        android:textSize="18sp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginBottom="2dp"
    android:background="@android:color/white"
    android:gravity="center_vertical"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_weight="2"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:id="@+id/Lv0_tv"
        />
</LinearLayout>

專案中的build.gradle檔案

效果圖

github程式碼Domel