I have a RecyclerView using a GridLayout. The RecyclerView has heterogeneous layouts (I followed this guide link) with the difference that I have only on top of the recyclerView a textView showing some data. It works fine but when I rotate the screen, and the gridlayout is set with two or more spans, the textView is shown on the first span only but I want that the span will take the entire width of the screen regardless the number of spans.
Should I use a tableLayout for that?
All you need to do is set a custom SpanSizeLookup on your GridLayoutManager.
final int numSpans = ...
// Create a grid layout with numSpans columns
GridLayoutManager layoutManager = new GridLayoutManager(this, numSpans);
// Create a custom SpanSizeLookup where the first item spans all columns
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
return position == 0 ? numSpans : 1;
}
});
Related
I'm trying to achieve the following list item formation within a recycler or grid view:
1
1 1
1
1 1
1
where 1 is a single view holder (list item). My list items all have the same width and height.
I've attempted to use a recyclerview with a grid layout manager as seen here:
recyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));
But this will strictly display 2 columns.
I've tried using a StaggeredGridLayoutManager also with my recyclerView as seen below:
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
But this only stretches my list items to fit a 2 column list, similar to the GridLayoutManager.
I've also tried to change the width of 1 in 3 list views in my adapter, but the width of one view does not effect the position of another.
The current setup looks like this:
adapterPre = new CategoryListAdapter(this, getContext(),GeneralUtils.isLoggedIn());
RecyclerView premadeLists = view.findViewById(R.id.practise_recycler);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);
gridLayoutManager.setSpanSizeLookup(new CustomSpanSizeLookup());
premadeLists.setLayoutManager(gridLayoutManager);
premadeLists.setHasFixedSize(false);
premadeLists.setAdapter(adapterPre);
CustomSpanSizeLookup:
class CustomSpanSizeLookup extends GridLayoutManager.SpanSizeLookup {
#Override
public int getSpanSize(int position) {
return position % 2 == 0 ? 2 : 1;
}
}
You have to use a GridLayoutManager with 2 spans and create a custom GridLayoutManager.SpanSizeLookup:
class CustomSpanSizeLookup extends GridLayoutManager.SpanSizeLookup {
#Override
public int getSpanSize(int position) {
/* one in three items will occupy the whole row,
* two in three items will take up half a row and can
* therefore be placed next to each other.
*/
return position % 3 == 0 ? 2 : 1;
}
}
You can set it on the GridLayoutManager with the method:
layoutManager.setSpanSizeLookup(new CustomSpanSizeLookup());
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 use GridLayoutManager to place items in RecyclerView vertically.
Each item oocupies one span.
Now I want some items to end current row, so next item is placed on new row below it. Each item must can occupy only one span, so that they're all same width.
Example:
The item[4] would be placed on next row, not after item[3]. Space after item[3] shall be left unused.
I tried to use SpanSizeLookup, but its span index doesn't make any sense to me and any value doesn't seem to do anything.
Can someone suggest solution?
GridLayoutManager manager = new GridLayoutManager(getContext(), 3, LinearLayoutManager.VERTICAL, false);
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
return position == 3 ? 3 : 1;
}
});
Span size is 1 per default for each element, just return the desired span size for the position in custom span lookup.
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);