How to modify the position of a single View inside a RecyclerView - android

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);

Related

How can I implement layout like this with recyclerview?

I use GridlayoutManager and override getSpanSize method. I was able to implement three and one column in the same recyclerview. But I was not able to implement five columns in the same recyclerview.
If you know which row will have five column and which has three column so you can use below code
GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
if (position > 3)
return 5;
else
return 3;
}
});

How to arrange multiple images horizontally on single activity

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.

dynamic child width in vertical GridLayoutManager

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.

Android custom LayoutManager for first element in RecycleView

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);

Different card size

How can I make it so that in the recycleView there are differents size cards? (1x1, 1x2 and 2x1 where 1 is the card length)
enter image description here
You can create two view holders. One of them holds the two cards that are in the same row, the other holds the full row one. It would definitely look like the image you posted. For implementing the recycler view with multiple view holders check out this.
You can use GridLayoutManager with different span count.
Here is some example.
In activity:
//Initialize recyclerView and adapter before
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
if (adapter.isHeader(position)) {
//Returns span count 2 if method isHeader() returns true.
//You can use your own logic here.
return mLayoutManager.getSpanCount()
} else {
return 1;
}
}
}
});
And add this method to your adapter class:
public boolean isHeader(int position) {
return position == 0;//you can use some other logic in here
}

Categories

Resources