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.
Related
Hi I'm trying to make a activity where the size of each grid it 1*1 and occasionally an 2*2 grid appears.
Here's What I'm trying to achieve:
I am using recycler view to populate my layout.
here is my staggered grid StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(3, LinearLayoutManager.VERTICAL);
I tried using different layouts but I didn't get what I wanted.thanks in advance.
If you all items have the same size (as a grid) you can use a GridLayoutManager and you can use the method setSpanSizeLookup(SpanSizeLookup) to change the default span (=1).
Something like:
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
return // your implementation ...... ;
}
});
You can use FlexboxLayoutMananger
Here you can find the usage:
https://github.com/google/flexbox-layout/blob/master/README.md#flexboxlayoutmanager-within-recyclerview
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);
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;
}
});