Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi expandable recyclerview

i try to implements my own multi expandable RecyclerView it'w work fine for one sub-item but when i want sub-sub-item the view display but i need to click on it several times. Example of hierarchy :

Title1
  SubTitle1
  SubTitle2
  SubTitle3
    SubSubTitle1
    SubSubTitle2
Title2
...

EDIT : The problem is only on the SubTitle to display SubSub'sItem. I think i need to notify parent adapter that my view changed but i doesn't work to.

To do that i used RecyclerView with itemLayout who contain too an RecyclerView and when i click on item i set adapter off the child RecyclerView. I use custom LinearLayoutManager i found here https://stackoverflow.com/a/29261667/3289338.

My 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
    }
}

And the layout :

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

Thanx.

like image 582
Timo Avatar asked Jul 07 '15 14:07

Timo


People also ask

What is expandable RecyclerView?

Expandable RecyclerView is one of the most important feature in Android which can be easily created for our application . It contains two views one is parent view and other is child view. Parent is visible by default but the child view has to be expanded and collapsed. It will expand when we click on parent view.

What is expandable RecyclerView Android?

A custom RecyclerView which allows for an expandable view to be attached to each ViewHolder. JavaDocs.

How do I make my recycler view scrollable?

To be able to scroll through a vertical list of items that is longer than the screen, you need to add a vertical scrollbar. Inside RecyclerView , add an android:scrollbars attribute set to vertical .


1 Answers

https://github.com/itsnothingg/RecursiveRecyclerView

I made a open source for this. you can easily implement multi-expanding recyclerview and use different layout for each expanding depth.

like image 104
itsnothingg Avatar answered Oct 16 '22 07:10

itsnothingg