Set Span for Items in Gridlayoutmanager Using Spansizelookup

Set span for items in GridLayoutManager using SpanSizeLookup

The problem was that header should have span size of 2, and regular item should have span size of 1.
So correct implementations is:

mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(mAdapter.getItemViewType(position)){
case MyAdapter.TYPE_HEADER:
return 2;
case MyAdapter.TYPE_ITEM:
return 1;
default:
return -1;
}
}
});

set different span for a every row with GridLayoutManager

There seems to be issue with your logic. Try with below logic:

    gridLayoutManager.setSpanSizeLookup(new GgridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {

Log.e("SPAN",position%3+" <=="+position);
switch (position % 6) {
case 0:
return 3;
break;
case 1:
case 2:
case 3:
case 4:
return 1;
break;
case 5:
return 2;
break;
default:return 3;
}
}
});

Android GridLayoutManager span size

If I understand your requirements correctly, there is no way to accomplish what you want with RecyclerView + GridLayoutManager. Even if you implement a custom GridLayoutManager.SpanSizeLookup, you will only be able to have your extra one or two items stretch to fill the row... you can't center them.

You can, however, accomplish what you want by using RecyclerView with a FlexboxLayoutManager, which is a part of Google's FlexboxLayout project: https://github.com/google/flexbox-layout

When you create your FlexboxLayoutManager, you need to set the FlexDirection to "row" and the JustifyContent to "center":

FlexboxLayoutManager manager = new FlexboxLayoutManager(this, FlexDirection.ROW);
manager.setJustifyContent(JustifyContent.CENTER);

Then, when you create your ViewHolders, you need to size them to one third of the RecyclerView's width:

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.item_view, parent, false);

ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) itemView.getLayoutParams();
layoutParams.width = (parent.getWidth() / 3) - layoutParams.leftMargin - layoutParams.rightMargin;
itemView.setLayoutParams(layoutParams);

return new MyViewHolder(itemView);
}

Here's a link to a gist for a small app that demonstrates this: https://gist.github.com/zizibaloob/0c44bfe59b371b5ae0bd2edcb4a7e592

Set span size in Paging Library with GridLayoutManager

I solved it. this is my code

In your Activity

val gridLayoutManager = GridLayoutManager(activity, 2)
gridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
val viewType = wallpaperAdapter.getItemViewType(position)
return if(viewType == WALLPAPER_VIEW_TYPE) 1
else 2
}
}

binding.recyclerView.apply {
this.layoutManager = gridLayoutManager
this.setHasFixedSize(true)
this.adapter = wallpaperAdapter
}

In your PagingDataAdapter

override fun getItemViewType(position: Int): Int {
if (position == itemCount){
return NETWORK_VIEW_TYPE
}else {
return WALLPAPER_VIEW_TYPE
}
}

Hope it will work for you.

GridLayoutManager spansizelookup not working

Here's the important part of your method:

if(position == 0)
num = 2;
else if(position == 1)
num = 1;
else if (position % 4 == 0)
num = 1;
else
num = 2;

So the span size will be 1 for positions 1 and 4, 8, 12, 16... and will be 2 for everything else. This means that there are never two items next to each other with span size 1, and since your grid is only two spans wide, everything needs to be on its own row. I used your SpanSizeLookup but with a simple layout and I see this:

enter image description here

So, if you want to sometimes see two images next to each other and sometimes only see one, you'll need a different algorithm for your span size lookup. For example:

return (position % 3) == 0 ? 2 : 1;

enter image description here



Related Topics



Leave a reply



Submit