Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recyclerview binds all views at the same time

I have a vertical recyclerview (with a GridLayoutManager) inside another recyclerview (with LinearLayoutManager). The problem I am facing right now is that, the inner recyclerview (with GridLayoutManager) binds all of it's items at the same time, even the views that are not on the screen at the moment (onBindViewHolder() gets called for all of its items).

To give you more information, in my layout file, I put height of my recycler view as wrap_content.

I think the problem is, since there are 2 nested vertically recyclerviews, when the parent RV wants to measure its children and the children is another RV, in onMeasure() it computes the size needed for the entire RV, not just the portion that it wants to bind on the screen.

Any idea how to solve this?

Here is the layout file for my outer recyclerview:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

</FrameLayout>

And here is the code for my inner recyclerview:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/gutter"
android:paddingBottom="@dimen/gutter">

<TextView
    android:id="@+id/title_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/gutter"
    android:textSize="30sp"
    android:textColor="@android:color/white"
    android:fontFamily="sans-serif-thin"/>

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

</LinearLayout>

P.S.: I'm using this adapter delegate for my outer recyclerview: https://github.com/sockeqwe/AdapterDelegates

like image 489
Gabriel Avatar asked Jul 21 '16 18:07

Gabriel


Video Answer


2 Answers

I think nested recyclerviews are a very bad idea. When i try to scroll, which recyclerview has to respond the the scolling, the parrent or child.

That is why I think you are looking for the ExpandableListView? That's limited to only two levels of listings, but that sounds like it would work for your needs). It also solves the soling issue.

It would look something like this:

enter image description here

EDIT: even nested ExpandableListViews are possible:

enter image description here

EDIT: check this lib for horizontal scroling

like image 190
Robin Dijkhof Avatar answered Sep 23 '22 07:09

Robin Dijkhof


This is a known bug.
You should not put a RecyclerView inside another RecyclerView because RecyclerView gives its children infinite space.
Hence the inner RecyclerView keeps measuring till the dataset is exhausted.
Try setting setAutoMeasureEnabled(false) to false on layout manager or you can solve this problem by using a wrapper adapter instead of inner recycler view.

like image 29
abhishesh Avatar answered Sep 24 '22 07:09

abhishesh