答案 0 :(得分:0)
您可以通过创建2个Adapters
来做到这一点,例如,一个主要的array
和另一个Adapter
的孩子array
:
[
{
"id":1,
"name":"name1",
"items":
[
{"id":2,"name":"name2","valid":true},
{"id":3,"name":"name3","valid":false},
{"id":4,"name":"name4","valid":true}
]
},
{
"id":5,
"name":"name5",
"items":
[
{"id":6,"name":"name6","valid":true},
{"id":7,"name":"name7","valid":false}
]
},
{
"id":8,
"name":"name8",
"items":
[
{"id":9,"name":"name9","valid":true},
{"id":10,"name":"name10","valid":false},
{"id":11,"name":"name11","valid":false},
{"id":12,"name":"name12","valid":true}
]
}
]
设置子适配器时,您会说(items.get(postion).getItems)更清晰,请检查下面的代码。
在您的主要Adapter
布局中添加另一个RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<layout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/main_category_layout"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@color/white"
android:visibility="visible">
<ImageView
android:id="@+id/category_pic"
android:layout_width="37dp"
android:layout_height="37dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="20dp"
android:adjustViewBounds="true"
android:contentDescription="@string/nav_header_desc"
android:scaleType="fitXY"
android:src="@drawable/noimage"
android:visibility="gone" />
<TextView
android:id="@+id/category_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="20dp"
android:layout_marginEnd="35dp"
android:layout_toEndOf="@id/category_pic"
android:gravity="start"
android:singleLine="true"
android:textColor="@color/black"
android:textSize="13sp" />
<ImageView
android:id="@+id/category_arrow"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:background="@drawable/arrow_down"
android:visibility="gone" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height=".5dp"
android:background="@color/white" />
<android.support.v7.widget.RecyclerView
android:id="@+id/secondRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</layout>
现在在主Adapter
中,需要初始化Adapter
内的第二个onBindViewHolder
holder.binding.secondRecyclerView.setHasFixedSize(true);
holder.binding.secondRecyclerView.setLayoutManager(new LinearLayoutManager(context));
if (items.get(position).getChildren() != null) {
Sub_Adapter sub_adapter= new Sub_Adapter(items.get(position).getChildren(), context);
holder.binding.secondRecyclerView.setAdapter(sub_adapter);
}
现在,您所要做的就是创建第二个Adapter
(Sub_Adapter),并在需要或使用相同布局时填充不同的布局。