多可扩展回收器视图

Tim*_*imo 7 android expandable android-linearlayout android-recyclerview

我尝试实现我自己的多可扩展 RecyclerView 它适用于一个子项,但是当我想要子子项时,视图显示但我需要多次单击它。层次结构示例:

Title1
  SubTitle1
  SubTitle2
  SubTitle3
    SubSubTitle1
    SubSubTitle2
Title2
...
Run Code Online (Sandbox Code Playgroud)

编辑:问题仅在 SubTitle 上显示 SubSub'sItem。我想我需要通知父适配器我的视图改变了,但我不工作。

为此,我将 RecyclerView 与 itemLayout 一起使用,它们也包含一个 RecyclerView,当我单击项目时,我将适配器设置为关闭子 RecyclerView。我使用我在这里找到的自定义 LinearLayoutManager /sf/answers/2048316721/

我的 onBindViewHolder :

@Override
public void onBindViewHolder(final DrawerViewHolder holder, final int position) {
    if (holder.type == TYPE_EXPANDABLE_ITEM) {
        final ItemDrawer item;
        if (header) {
            item = itemDrawers.get(position - 1);
        } else {
            item = itemDrawers.get(position);
        }
        holder.textView.setText(item.getTitle());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.isOpen()) {
                    item.setOpen(false);
                    holder.recyclerView_child.setVisibility(View.GONE);
                    notifyItemChanged(position);
                } else {
                    item.setOpen(true);
                    holder.recyclerView_child.setVisibility(View.VISIBLE);
                    holder.recyclerView_child.setAdapter(new DrawerAdapter(item.getSubItem(), drawer, fragmentManager, false));
                    holder.recyclerView_child.setLayoutManager(new MyLinearLayoutManager(holder.itemView.getContext().getApplicationContext()));
                    notifyItemChanged(position);

                }
            }
        });

    } else if (holder.type == TYPE_SIMPLE_ITEM) {
        final ItemDrawer item;
        if (header) {
            item = itemDrawers.get(position - 1);
        } else {
            item = itemDrawers.get(position);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.isActivity()) {
                    holder.itemView.getContext().startActivity(item.getActivityIntent());
                } else {
                    fragmentManager.beginTransaction().replace(R.id.drawer_frame_layout, item.getFragment()).commit();
                }
            }
        });
    } else {
        //header
    }
}
Run Code Online (Sandbox Code Playgroud)

和布局:

<?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="wrap_content"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackground"
        android:paddingBottom="8dp"
        android:paddingTop="8dp">

        <ImageView
            android:id="@+id/imageView_drawer_row_expandable"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingStart="16dp"
            android:src="@drawable/ic_drawer" />

        <TextView
            android:id="@+id/textView_drawer_row_expandable"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@id/imageView_drawer_row_expandable"
            android:paddingStart="12dp"
            android:paddingTop="4dp"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <ImageButton
            android:id="@+id/imageView_drawer_row_expandable_open"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:background="#00000000"
            android:paddingEnd="16dp"
            android:src="@android:drawable/arrow_up_float" />
    </RelativeLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView_drawer_child"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:paddingStart="16dp"
        android:scrollbars="vertical"
        android:visibility="gone" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

谢谢。

小智 1

https://github.com/itsnothingg/RecursiveRecyclerView

我为此做了一个开源。您可以轻松实现多重扩展的recyclerview,并为每个扩展深度使用不同的布局。