StaggeredGridLayoutManager change spanCount dynamically RecyclerView - android

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

Related

Android: How to have a 1 - 2 - 1 list item formation in a GridView or RecyclerView?

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

How can I arrange items on RecyclerView based on condition?

I want to display items on Recyclerview if columns 3 select from back-end RecyclerView first three items on first row and four one in second row, if columns 4 first four item on first row and five in second row.
how can arrange above list in RecyclerView
recyclerViewLineItem.setHasFixedSize(false);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerViewLineItem.setLayoutManager(linearLayoutManager);
Use GridLayoutManager class if number of columns are more than 1
GridLayoutManager gridLayoutManager = new GridLayoutManager(activity, 2/*column count*/);
recyclerView.setLayoutManager(gridLayoutManager);
& for horizontal scrolling, use this
new GridLayoutManager(getActivity(), 1 /*column count*/, GridLayoutManager.HORIZONTAL, false)
This is actually very easy. You need to use GridLayoutManager instead of the usual LinearLayoutManager.
Inside GridLayoutManager, you can choose how many columns you want depending upon the number of items. Let me show you:
int spanCount = 0 ; //This is the number of column you need, set to 0 at first
if(yourItemCount == 3){
spanCount = 3; //set to 3 columns
};
if(yourItemCount == 5){
spanCount = 4; //set to 4 columns, and the 5th item will be at the second row
}
//now pass the spanCount to the GridLayoutManager
GridLayoutManager gridLayoutManager = new GridLayoutManager(activity, spanCount);
recyclerView.setLayoutManager(gridLayoutManager);
Hope it helps.
Cheers!
You can try passing position from your adapter and in your activity/fragment you can create a method which display data according to the values passed.
private void CheckColumn(RecyclerView recyclerView) {
int count = recyclerView.getChildCount();
for (int i = 0; i < count; i++) {
View view = recyclerView.getChildAt(i);
// Your code to show grid items here
}
}

LinearLayout span after rotation

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

How to create the TableView dynamically as metioned below one screen shot in android

I have to create Table Layout dynamically in android. First one header for one row, second one two column for another row in Table Layout.
http://imgur.com/wnuY8aa
Ok. So your main concern about to show header with gridview.
Simplest way to show as per your requirement is to use Recyclerview with GridLayoutManager.
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
GridLayoutManager glm = new GridLayoutManager(this, 2);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
if (position % 4 == 0) {
return 2; // row with single column.
} else {
return 1; // row with two column.
}
}
);
mRecyclerView.setLayoutManager(glm);
mRecyclerView.setAdapter(mAdapter);
You will get following output.
I hope this will you.

StaggeredGridLayoutManager. How to have one item in first row and 2 item other rows

I am using recyclerview with staggeredGridLayoutManager.
What i am trying to do is to show one header item in first row. then i want to show 2 items in each below row. But i am unable to do so.
Here is code that i used.
recyclerView = (RecyclerView)findViewById(R.id.recycler);
staggeredGridLayoutManager = new StaggeredGridLayoutManager(1,1);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
And these are the results
Help me with this. thanks
You could use GridLayoutManager.
GridLayoutManager manager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);
manager.setSpanSizeLookup(
new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
// 2 column size for first row
return (position == 0 ? 2 : 1);
}
});
Here we are creating a GridLayoutManager with 2 grid columns. Then only for the first row, we are setting the span size to 2.

Categories

Resources