• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

RecyclerView doesn't update when wraps content

AsafKov

Newbie
Hi guys, quick question.
I've just finished implementing RecyclerView on my project after an awful struggle - despite updating the Adapter dataset, the view won't update. I've tried doing it in multiple ways - deleting all existing elements and adding the entire list, creating a new ArrayList, creating a new Adapter... nothing worked.

In the end, the problem turned out to be making the RecyclerView wrap its content. I didn't understand how it can cause such a bug so I dug a bit online and all I could find is that it was a bug that was fixed roughly 2 years ago in version 23.2.1.

So I wonder if the problem is somehow in my XML file? I'd just like to know what caused this bug since I've spent so much time on practically nothing, it would be nice to actually take something from it...

Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@color/colorBackground">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/dateHeadline"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@color/colorPrimary"
            android:textColor="@color/headlineTextColor"
            android:gravity="center"
            android:textStyle="bold"
            android:textSize="18sp" />

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

    </LinearLayout>


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabulousFab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="24dp"
        android:src="@android:drawable/ic_input_add"
        android:tint="@android:color/background_light"
        app:backgroundTint="@android:color/background_dark"
        app:layout_anchorGravity="bottom|end" />

</android.support.design.widget.CoordinatorLayout>
 
I'm just near the end of my first RecyclerView project so I don't have a lot of experience yet. I've had plenty of questions and problems but none with showing my RecyclerView.

Looking at your XML, one small thing jumped out at me, something I'd never seen before, namely fitsSystemWindows. The "fits" part of that made me wonder if the space available for you to display your data was somehow being limited. I just looked up the effect of this property and, frankly, can't tell if it had anything to do with your problem. (See https://developer.android.com/reference/android/R.attr.html#fitsSystemWindows - maybe you can figure it out.)

You *could* just omit that parameter and see if it makes any difference....
 
Did you call notifyDataSetChanged() on the adapter?
Yes, of course. Also tried using notifyItemInserted().

I'm just near the end of my first RecyclerView project so I don't have a lot of experience yet. I've had plenty of questions and problems but none with showing my RecyclerView.

Looking at your XML, one small thing jumped out at me, something I'd never seen before, namely fitsSystemWindows. The "fits" part of that made me wonder if the space available for you to display your data was somehow being limited. I just looked up the effect of this property and, frankly, can't tell if it had anything to do with your problem. (See https://developer.android.com/reference/android/R.attr.html#fitsSystemWindows - maybe you can figure it out.)

You *could* just omit that parameter and see if it makes any difference....
fitsSystemWindows doesn't seem to have anything to do with it. It's worth noting that before using ReyclerView I simply used a LinearLayout inside a scrollView, and added children to it, it worked fine with no other changes to the XML.
I examined it a little further and it seems that only wraping the width of the content causes this issue, if the height is set to wrap_content it still works fine.
 
Yes, of course. Also tried using notifyItemInserted().

I'm asking the question because you haven't posted any application code, so we have no way of knowing what exactly that is doing to populate your list.
The scientific approach to solving these problems, rather than second guessing/trial and error, is to systematically trace through your code, to verify that what you think your code is doing, is actually happening. Often those two things don't line up.
 
I'm asking the question because you haven't posted any application code, so we have no way of knowing what exactly that is doing to populate your list.
The scientific approach to solving these problems, rather than second guessing/trial and error, is to systematically trace through your code, to verify that what you think your code is doing, is actually happening. Often those two things don't line up.
I know. I didn't post the code on purpose, because I was confident the problem wasn't there. As I said, the problem was fixed only when I changed the width and height attributes in the XML. Also, I did trace through the code to see if there was a problem when updating the dataset (there wasn't) or when creating and binding the viewHolder (there wasn't).
 
Back
Top Bottom