Recycler view item fill up entire recycler view height after upgrading support library from "23.1.1" to "23.2.1"

32,866

Solution 1

Update

It appears that you are updating the LayoutParam for your View in your Adapter.

It is possible to tell this because your UI appears absolutely fine until you begin scrolling. This means that your XML is correct as it is defined in your XML layout file.

The fact that it changes after scrolling begins, means there is a logic error in your onBindViewHolder implementation. That is why the error appears when you scroll down, and then the error sticks when you scroll back up.

Old answer

Your issue is that your divider has gone rogue:

<View
    android:layout_width="1px"
    android:layout_height="match_parent"
    android:background="?attr/buyPortfolioSeperatorBackground"
    android:layout_marginRight="5dp"
    android:layout_marginLeft="5dp" />

For testing purposes, set it to:

<View
    android:layout_width="1px"
    android:layout_height="30dp"
    android:background="?attr/buyPortfolioSeperatorBackground"
    android:layout_marginRight="5dp"
    android:layout_marginLeft="5dp" />

Make sure you change both of them!

Solution 2

I had a similar problem. It endend up being that the recycler was not the problem. Check that your CardView item measurements translate to something like this:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ...
/>

If you're not using CardView, ensure that the element you use in your Adapter for the view has android:layout_height="wrap_content" and not match_parent.

If that fails to work, you can add another attribute setting the minHeight or maxHeight for the view item.

Solution 3

The good news:

I can pinpoint you to the exact version that changed RecyclerView's behavior: it's not a change in 23.2.1 but rather a change in 23.2.0 (February 2016). More specifically:

RecyclerView.LayoutManager no longer ignores some RecyclerView.LayoutParams settings, such as MATCH_PARENT in the scroll direction.

Note: These lifted restrictions may cause unexpected behavior in your layouts. Make sure you specify the correct layout parameters.

Indeed if you fire up the 23.2.0 libraries, you will see the same behavior. That behavior can be simplified in your case as:

Now, when you have RecyclerView's children with android:layout_x="match_parent", that will affect RecyclerView's android:layout_x, which was not the case in 23.1.1 and earlier versions.

The bad news:

Even if I'm 99% sure that this is the reason behind your problem, I still can't see an issue in your code. I've actually set up a RecyclerView with your XML structure (changing just the color/background parameters), with a LinearLayoutManager and it works as expected in 23.2.1. I can share my implementation if you want to perform a sanity check.

You should double check your adapter implementation/manipulation even if it's far-stretched.

Solution 4

To fix this bug row_layout should have height fixed or wrap_content! I also had this problem and just realized that the height of row_layout was match_parent.

Solution 5

The height of recycle view must be "wrap_content" only. The recycle view will handle height if the size of cell increases.

buy_portfolio_fragment.xml

 <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/buyPortfolioListViewBackground"
        android:requiresFadingEdge="none"
        android:scrollbars="vertical"
        android:paddingTop="@dimen/default_tab_layout_height"
        android:clipToPadding="false" />
Share:
32,866

Related videos on Youtube

Lumii
Author by

Lumii

WeNote - Notes, To-do lists, Reminders &amp; Calendar JStock Android JStock - Free Stock Market Software WeFocus - Focus, Pomodoro, Do one thing at a time

Updated on April 07, 2020

Comments

  • Lumii
    Lumii about 4 years

    Previously, I'm using the following old support libraries "23.1.1".

    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:support-v4:23.1.1'
    compile 'com.android.support:preference-v7:23.1.1'
    compile 'com.android.support:preference-v14:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    

    It works pretty well. Here's how my RecyclerView looks like

    enter image description here

    Now, I wish to migrate to "23.2.1", due to some bug fixes done.

    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:support-v4:23.2.1'
    compile 'com.android.support:preference-v7:23.2.1'
    compile 'com.android.support:preference-v14:23.2.1'
    compile 'com.android.support:design:23.2.1'
    compile 'com.android.support:recyclerview-v7:23.2.1'
    

    However, suddenly, all my RecyclerView items, seem to fill up the RecyclerView entire height.

    enter image description here

    enter image description here

    Here's the code snippet of my layout file : https://gist.github.com/yccheok/241a0d38d56305a1be24d09b54eb1600

    What really puzzle me is that, although I'm using "wrap_content" in my recycler view item layout, it doesn't work as expected.

    I don't use any custom layout manager for my RecyclerView.

    From http://developer.android.com/tools/support-library/index.html, I realize 23.2.1 makes quite a number of changes on RecyclerView this time.

    • Fixed bugs related to various measure-spec methods. (Issue 201856)
    • Reduced the lockdown period in which RecyclerView does not allow adapter changes while calculating a layout or scroll. (Issue 202046)
    • Fixed a crash when calling notifyItemChanged() on an item that is out of view. (Issue 202136)
    • Fixed a crash that occurs when RecyclerView.LayoutManager adds and removes a view in the same measurement pass. (Issue 193958)

    What I suspect most is https://code.google.com/p/android/issues/detail?id=201856 , as it involves changing various measure-spec methods

    So far, I try to reproduce the problem with a simple RecyclerView project, with 23.2.1 but failed! It doesn't have "item fills up the RecyclerView entire height" problem. My guess is that, my simple project doesn't simulate the complex layout structure of my production project. My production project is having the following layout

    <Activity>
        <Fragment>
            <View Pager>
                <Fragment>
                    <RecyclerView />
                </Fragment>
            </View Pager>
        </Fragment>
    </Activity>
    

    After debugging for few hours, I'm still cannot find root cause for such problem, any hint?

    Thanks.

    What I had tried

    I had tried to change RecyclerView

    from

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

    to

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

    It looks good initially. However, when you perform scrolling, thing doesn't work as expected : https://www.youtube.com/watch?v=U2EChFn6WkI

    UPDATE: I finally figure out the root cause

    Is mistake at my side! Since I need to have different margin for the last row item, here's my adapter code.

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final List<TransactionSummary> transactionSummaries = buyArray.transactionSummaries;
    
        if (position == transactionSummaries.size() - 1) {
            holder.itemView.setLayoutParams(lastLayoutParams);
        } else {
            holder.itemView.setLayoutParams(normalLayoutParams);
        }
    

    Unfortunately, lastLayoutParams and normalLayoutParams is being initialized as

        normalLayoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT
        );
    
        lastLayoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT
        );
    

    Using LinearLayout.LayoutParams.WRAP_CONTENT solve the problem.

    • Patrick W
      Patrick W about 8 years
      Did you set the minHeight or maxHeight or your view item?
    • Lumii
      Lumii about 8 years
      @patwanjau But, I don't know what should be the value of minHeight and maxHeight.
    • Santosh  V M
      Santosh V M about 8 years
      @CheokYanCheng I'm facing this as well.
    • Santosh  V M
      Santosh V M about 8 years
      @CheokYanCheng I fixed the problem by changing layout height to "wrap_content" from "match_parent"
    • Lumii
      Lumii about 8 years
      As u can see my layout file, I'm also using wrap_content. Any idea why it still happen?
    • njzk2
      njzk2 about 8 years
      looks like a dup of stackoverflow.com/questions/35638638/… but apparently not?
    • Hiroga Katageri
      Hiroga Katageri about 8 years
      Can you try taking out the stateListAnimator? I've had problems with RecyclerView and Animations before when scrolling.
    • tjeubaoit
      tjeubaoit about 8 years
      I've actually set up a RecyclerView with your XML structure (changing just the color/background parameters), with Activity -> ViewPager -> Fragment -> RecyclerView and it works as expected in 23.2.1. Attach your adapter code
    • Lumii
      Lumii about 8 years
      @tjeubaoit do you mind to share your code? So that I can compare against mine. Thanks.
    • Knossos
      Knossos about 8 years
      The issue is almost certainly to do with how you are setting the UI in your onBindViewHolder function. That is why the error appears when you scroll down, and then the error sticks when you scroll back up. Share your Adapter code so we can determine the error.
    • Lumii
      Lumii about 8 years
      Thanks @Knossos you had made a correct prediction :)
    • Eliseo Ocampos
      Eliseo Ocampos about 7 years
      @Cheok Yan Cheng, Thank you for such a detailed question. BTW, it is a very nice app. Is it opensource by any chance?
  • Lumii
    Lumii about 8 years
    If you look at buy_portfolio_card_row_layout.xml (which is layout file for recycler view item) at gist.github.com/yccheok/241a0d38d56305a1be24d09b54eb1600 , you will realize I'm already using wrap_content for height.
  • Patrick W
    Patrick W about 8 years
    Did you set the minHeight or maxHeight or your view item?
  • fillobotto
    fillobotto about 8 years
    I can confirm this was the problem for me too, which I encountered right yesterday
  • Lumii
    Lumii about 8 years
    This is when wrap_content being used - i.imgur.com/VOMgAvv.png it seems it does more harm than good.
  • Lumii
    Lumii about 8 years
    @fillobotto Do you have any screen-shot to describe the problem? As with more problem description, we might soon figure out the cause of the problem. Thanks.
  • Lumii
    Lumii about 8 years
    I had changed both to 30dp. But, it doesn't help much - i.imgur.com/hl4mKGY.png
  • Knossos
    Knossos about 8 years
    @CheokYanCheng It looks to me like you are changing the LayoutParams in your RecyclerView Adapter. Can you check that? Perhaps post the code. If it works at the start, then the XML is fine. The issue is in dynamic changes in your Adapter.
  • Amit
    Amit about 8 years
    I have a same issue as mention top. Using lin 23.2.1 fill all port,I have set item height match_parent to wrap_content. Its working for me
  • Knossos
    Knossos about 8 years
    @CheokYanCheng Can you post your Adapter?
  • Lumii
    Lumii about 8 years
    You are right. Your suggestion trigger me to re-check my adapter code carefully. I do change the LayoutParams dynamically. Thanks.
  • Knossos
    Knossos about 8 years
    Glad to see you found it!
  • Sevle
    Sevle about 8 years
    @Knossos Even though the suggestion in comments was correct, the answer as stands right now is not the correct answer to the OP's question. Would you mind adding your suggestion in the original answer for future readability?
  • Knossos
    Knossos about 8 years
    @Sevle: Absolutely
  • AlvaroSantisteban
    AlvaroSantisteban over 7 years
    This was, indeed, the reason behind my problem.
  • Ammar Mujeeb
    Ammar Mujeeb almost 7 years
    My issue was i was setting the "row" layout parent to match_parent, changing it to wrap_content fix the issue.