currently I'm exploring more on RecyclerView and GridLayoutManager and trying to achieve a nice looking UI for gallery display but I'm having some trouble with the span size of the GridLayoutManager. As documentation said: By default, each item occupies 1 span. You can change it by providing a custom GridLayoutManager.SpanSizeLookup instance via setSpanSizeLookup(SpanSizeLookup).
With this I expect that if I set the span to 2 of the item in position 0 it will set the colspan and the rowspan to 2 as well but it seems that it doesn't work that way. I set my column count into 2 for my GridLayoutManager for portrait and 4 for landscape. It works perfectly for portrait but not in landscape as it creates 2 empty cells below the 2nd and 3rd item in which I guess that the 1st item doesn't get the rowspan of 2 or else it really leaves an empty cells.
Here's my code:
gallery = new ArrayList<HashMap<String, String>>();
adapter = new GalleryAdapter(getBaseContext(), gallery);
// using grid layout manager
GridLayoutManager mLayoutManager = new GridLayoutManager(this, getResources().getInteger(R.integer.col_count));
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(adapter.getItemViewType(position)){
case 2:
return 2;
case 1:
return 1;
default:
return -1;
}
}
});
list_news.setLayoutManager(mLayoutManager);
list_news.setAdapter(adapter);
The Adapter:
public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.ViewHolder> {
Context context;
ArrayList<HashMap<String, String>> article;
ImageLoader mImageLoader;
@Override
public int getItemCount() {
return article.size();
}
public GalleryAdapter(Context context, ArrayList<HashMap<String, String>> article){
this.context = context;
this.article = article;
}
// Create new views (invoked by the layout manager)
@Override
public GalleryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.articles_item, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public int getItemViewType(int position) {
// Just as an example, return 0 or 2 depending on position
// Note that unlike in ListView adapters, types don't have to be contiguous
if(position==0){
return 2;
}else{
return 1;
}
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
mImageLoader = VolleyQueue.getInstance(context).getImageLoader();
holder.image.setImageUrl(article.get(position).get("image"), mImageLoader);
holder.image.setDefaultImageResId(R.drawable.test);
}
static class ViewHolder extends RecyclerView.ViewHolder {
@Bind(R.id.image) NetworkImageView image;
public ViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
And here's the result screenshot for landscape and portrait:
is there's any way I can avoid having blank cells for this?
UPDATE:
to better visualize on what I'm trying to achieve here's an image of what I would want the landscape mode to be. The numbers are the items based on the expected position.
Change your SpanSizeLookup
code into
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return adapter.getItemViewType(position);
}
});
And your adapter's getItemViewType
code into
@Override
public int getItemViewType(int position) {
if (position == 0) {
return context.getResources().getInteger(R.integer.col_count);
} else {
return 1;
}
}