I'm trying out RecyclerViews to build out something like Pinterest staggered layout. However, while LinearLayoutMnager and GridLayoutManager both work well in the same code, replacing that with StaggeredGridLayoutManager doesn't show any items. Putting breakpoints in the code shows that it gets the item count and items, but rendered nothing. Any idea on what I am missing?
final RecyclerView rView = (RecyclerView) findViewById(R.id.app_list);
rView.setClickable(true);
rView.setHasFixedSize(true);
rView.setLayoutManager(new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.HORIZONTAL));
rView.setAdapter(new ItemListAdapter(this, getItems()));
Replacing the StaggeredGridLayoutManager with just the GridLayoutManger everything works.
rView.setLayoutManager(new GridLayoutManager(this, 5));
The following is the adapter:
private static class ItemListAdapter extends RecyclerView.Adapter<ItemViewHolder> {
private final LayoutInflater mInflater;
private final ArrayList<ItemDetail> mList;
public ItemListAdapter(Context context, ArrayList<ItemDetail> list) {
mInflater = LayoutInflater.from(context);
mList = list;
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int position) {
final View view = mInflater.inflate(R.layout.item_blob, parent, false);
return new ItemViewHolder(view);
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
holder.hive.showDrawable(mList.get(position).icon);
}
@Override
public int getItemCount() {
return mList.size();
}
}
And the following is the ViewHolder
private static class ItemViewHolder extends RecyclerView.ViewHolder {
public final HiveView hive;
public ItemViewHolder(View view) {
super(view);
this.hive = (HiveView) view;
}
}
And finally the item layout
<?xml version="1.0" encoding="utf-8"?>
<com.bidyut.app.hivelauncher.widget.HiveView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/app_blob_size"
android:layout_height="@dimen/app_blob_size"
android:background="@android:color/holo_red_light"/>
In your samples, you are setting StaggeredGrid horizontal but Grid vertical. If your item views cannot properly measure themselves when given specified HEIGHT and variable WIDTH, this may turn into a problem.
If you can add the code for your adapter & item views, we can figure out what the problem is.
Edit for updated question
StaggeredGridLM (when horizontal) currently measures the child with exact height and infinite width. The layout params you are setting are not helping here since they are ignored. In a future release, it will start reading the value for scroll direction but other direction will stay the same until we introduce gravity support.
You can fix the bug by fixing the onMeasure method in your HiveView.