【问题标题】:Multi expandable recyclerview多可扩展回收站视图
【发布时间】:2015-07-07 14:31:24
【问题描述】:

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

Title1
  SubTitle1
  SubTitle2
  SubTitle3
    SubSubTitle1
    SubSubTitle2
Title2
...

编辑:问题仅在 SubTitle 上显示 SubSub'sItem。我想我需要通知父适配器我的视图发生了变化,但我没有这样做。

为此,我将 RecyclerView 与包含 RecyclerView 的 itemLayout 一起使用,当我单击项目时,我将适配器设置为子 RecyclerView。我使用我在这里找到的自定义 LinearLayoutManager https://stackoverflow.com/a/29261667/3289338

我的 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
    }
}

还有布局:

<?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>

谢谢。

【问题讨论】:

标签: android android-linearlayout android-recyclerview expandable


【解决方案1】:

https://github.com/itsnothingg/RecursiveRecyclerView

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

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,解决方案设置为 setHasFixedSize(false) 到您的父适配器

    注意:我使用 vanilla LinearLayoutManager

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-21
      • 2019-02-15
      相关资源
      最近更新 更多