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
RecyclerView. Adapter base class for presenting paged data from PagingData s in a RecyclerView . Adapters provide a binding from an app-specific data set to views that are displayed within a RecyclerView . A class that extends ViewHolder that will be used by the adapter.
It takes the data set that needs to be shown to the user in RecyclerView. It acts as the main responsible class for binding the views and displaying them. The majority of the work is done within the RecyclerView's adapter class.
RecyclerView makes it easy to efficiently display large sets of data. You supply the data and define how each item looks, and the RecyclerView library dynamically creates the elements when they're needed. As the name implies, RecyclerView recycles those individual elements.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With