I have a recyclerview with GridLayoutManager and have its spancount as 3. I have two viewtypes - one for the recyclerview items and one for ads(admob). Following is the result when my spancount is 3
You can see that the ad is not visible, now when i change the spancount to 1 it becomes visible like below
i think this is because the admob ads don't support these dimensions, is there a work around for this?
the ad is not showing up even if i try to hardcode the adsize
My question is that is there a way to fit the adview into my recyclerview with GridLayoutManager and spancount as 3
Yes, this is possible. All you have to do is to call setSpanSizeLookup on your layout manager like below
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
if (position % MainActivity.ITEMS_PER_AD == 0) {//your condition for showing ad
return 3;//replace 3 with the number of items in each row
}
return 1;
}
});
And then set the layout manager to your recyclerview.
Related
I am new to android i have developed an application in which i have to display images horizontal view on single activity i have done using staggered recycleview but am getting like this.
But i want to design like this as part of the activity.
You can easily do that with GridLayoutManager. Use SpanSizeLookup to control your row/column . For your case you have to use HORIZONTAL orientation. SpanSizeLookup will help you to control your rows in each column in HORIZONTAL GridLayoutManager.
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), NUM_OF_ROW, LinearLayoutManager.HORIZONTAL, reverseOrder);
GridLayoutManager.SpanSizeLookup spanSizeLookup = new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
// for position 0 use only one row. for this position it will take all rows
if (position == 0) {
return NUM_OF_ROW;
}
return 1;
}
};
Here is blog post about different Layout Manager implementation.
I have uploaded a repo on Github about different LayoutManager usage like LinearLayoutManager, GridLayoutManager, StaggeredGridLayoutManager and some advance RecyclerView usage like swipe, Drag and Drop. You can also check that
here is what i use for arranging items horizontally provided you're using RecyclerView:
int numberOfColumns = 3; //edit as you want pls
final GridLayoutManager gridLayoutManager = new GridLayoutManager(this.getContext(), numberOfColumns);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(adapter);
I might be off your solution, perdon if so, but pasting some of your code might help better.
I have a table to be displayed in RecyclerView. Currently i'm using GridLayoutManager with vertical orientation. Since all my logic depends on the vertical orientation of GridLayoutManager, i'm unable to shift to horizontal orientation or StaggeredLayoutManager. Is there a way to alter RecyclerView children measure so that the first cell of each row can have a different width?
I suggest you to use SpanSizeLookup.
Something like this:
GridLayoutManager manager = new GridLayoutManager(context, 2);
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
switch(adapter.getItemViewType(position)) {
// first cell type
case 1:
return 1;
default:
return 2;
}
}
});
And in you adapter you should override getItemViewType and return different types for your cells.
After some source digging, i have found that method GridLayoutManager.calculateItemBorders() is responsible for item width (cachedBorder). But since change that method is private, we cannot override it.
Hope this helps anyone who is facing a similar situation.
I've implemented a RecyclerView that based on a Preference value, displays a variable number of buttons. This is the situation:
What I would like to have
What I currently have
I am using a GridLayoutManager to display the buttons but I have many doubts about how to get the result I want. I've tryed to work with the span size and other things but with no results. Should I override onLayoutChildren() or is there an easier solution for this?
You can try this :
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
return 3 - position % 3;
}
});
recyclerView.setLayoutManager(gridLayoutManager);
I would like to implement RecycleView with layout that looks like the left image on this link
http://www.corelangs.com/html/tables/img/colspan-rowspan.png
I only want first element to have 100% width of the row, other rows should be seperated into two columns.
Any idea/tutorial how to do this?
use View Type for each row and make layout accordingly.
I solved it this way
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup()
{
#Override
public int getSpanSize(int position)
{
return (position == 0 ? 2:1);
}
});
recyclerView.setLayoutManager(gridLayoutManager);
How can we change the spanCount of RecyclerView dynamically?
My code create 3 column of cards in RecyclerView:
StaggeredGridLayoutManager mFavouratesLayoutManager;
mFavouratesLayoutManager = new StaggeredGridLayoutManager(3,
StaggeredGridLayoutManager.VERTICAL);
I'm trying to make the adapter like:
one element at top row
2 elements in second row
3 items below that
StaggeredGridLayoutManager does not support that. You can change the span count but it will effect every row. Your only option is to have full rows w/ SGLM.
If you use GridLayoutManager, you can achieve that by setting span count to 6, then providing a SpanSizeLookup that returns
6 for first item
3 for items 1 and 2
2 for items 3 4 5
Etc.
You can try this as StaggeredGridLayoutManager does not support that so use Gridlayoutmanager.
GridLayoutManager layoutManager = new GridLayoutManager(this, 4);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
if (position == 1)
return 6;
else
return 2;
}
});