9

I have a RecyclerView that's not displaying any items. Initially it's empty, but later I add items and call notifyDatasetChanged(). getItemCount() gets called and returns 25, onBindViewHolder gets called 3 times and sets up the view correctly.

public AlbumListAdapter(FacebookActivity activity, long pageId, OnItemClickListener listener){
    this.listener = listener;
    this.inflater = LayoutInflater.from(activity);
    this.service = new PagedGraphService<Album>(String.format("https://graph.facebook.com/%d/albums", pageId),
            new PagedGraphService.ServiceUpdateListener() {
        @Override
        public void dataUpdated() {
            notifyDataSetChanged();
        }

        @Override
        public void endReached() {

        }
    },
    new TypeToken<PagedGraphService.GraphResponse<Album>>(){}.getType());
}

@Override
public AlbumHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    return new AlbumHolder(inflater.inflate(R.layout.photo_album_list_card, viewGroup, false));
}

@Override
public void onBindViewHolder(AlbumHolder viewHolder, int i) {
    viewHolder.setAlbum(service.get(i));
}

@Override
public int getItemCount() {
    return service.getLoadedCount();
}

The gist of it. The service holds a list of albums and notifies the adapter of changes by calling dataUpdated() and endReached().

More code:

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    RecyclerView view = (RecyclerView) inflater.inflate(R.layout.recycler, container, false);
    view.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
    view.setHasFixedSize(false);
    AlbumListAdapter adapter = new AlbumListAdapter(activity, FacebookActivity.PSM_PAGE_ID, this);
    view.setAdapter(adapter);
    return view;
}

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v7.widget.RecyclerView>

Even more code:

The activity 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="match_parent">

<include layout="@layout/toolbar"/>

<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

List item layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/album_list_title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</android.support.v7.widget.CardView>

List layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

</android.support.v7.widget.RecyclerView>

I've been poking the code for a few hours now and I have no idea why it doesn't work

DariusL
  • 4,007
  • 5
  • 32
  • 44
  • a) You have to show us your code; b) Calling `notifyDatasetChanged()` on the RecyclerView isn't efficient and thus not cool – reVerse Nov 21 '14 at 16:52
  • The adapter's notifyDataSetChanged is called when I receive data. – DariusL Nov 21 '14 at 16:53
  • That's basically what I ment - Quoting the [Docs](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#notifyDataSetChanged%28%29): `If you are writing an adapter it will always be more efficient to use the more specific change events if you can. Rely on notifyDataSetChanged() as a last resort.` – reVerse Nov 21 '14 at 16:56
  • But it should still work, yet no items are drawn – DariusL Nov 21 '14 at 16:56
  • try to set layout bounds from dev options, maybe views are there but sth else is wrong. Also check RV's size, maybe it is 0. I don't see any reason why it would not show up. You can post more code about your layouts which may help. – yigit Nov 21 '14 at 19:37
  • For me the problem was that i explicitly added `android:hardwareAccelerated="true"` in my application which caused the recyclerview to not display, even though everything was implemented. – Sheraz Ahmad Khilji Nov 13 '15 at 07:03

1 Answers1

22

I am an idiot.

<?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="match_parent">

<include layout="@layout/toolbar"/>

<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

The default orientation for LinearLayout is horizontal, and the toolbar's width is match_parent, so my content was off screen. This is probably one of the most popular mistakes on android, which is why you get a warning, but the include must have hidden it.

DariusL
  • 4,007
  • 5
  • 32
  • 44
  • 2
    I love you. I spent an hour on this. I even ended up making myself a cup of tea, Lol. – Achilles Rasquinha Dec 11 '15 at 05:53
  • The default orientation of a LinearLayout is horizontal. It's pretty easy to believe that the layout is vertical, add multiple children to it, and wonder why only the first child is visible (when the subsequent children are off screen to the right). This lint rule helps pinpoint this issue by warning whenever a LinearLayout is used with an implicit orientation and multiple children. It also checks for empty LinearLayouts without an orientation attribute that also defines an id attribute. This catches the scenarios where children will be added to the LinearLayout dynamically. – Francis Bacon Aug 24 '17 at 09:09
  • OMG I can't believe it. I also made a mistake on this realm today. you're a hero – bko Mar 07 '21 at 10:04