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
}
Related
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;
}
});
I want to use an expandable recyclerview with grid layout manager, but with specific expanding behaviour which expand to the full width of the screen.
Is there any way or maybe libraries to achieve this? Thank you.
You need to add a transformation in your ViewHolder. Check this post: link
I donĀ“t know if Christopher's link allows for the expandable to occupy the whole 2 grids. There might be a way using a combination of getItemViewType(), and setSpanSizeLookup().
#Override
public int getItemViewType(int position) {
if (position == positionOfClickedItem + 2){
return TYPE_EXPANDABLE;
} else {
return TYPE_CAR_ITEM;
}
}
Bind the data according to its position, and carry the position to your activity to set SpanSizeLookup. You would then have to notify the adapter of a change:
// Create a SpanSizeLookup which returns 2 grids span if its the expandable or 1 otherwise.
gridLayout.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
if (position == positionOfClickedItem) {
return 2;
} else {
return 1;
}
}
});
I think the transformation answer works better but just in case the Span size is important.
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 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.