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);
Related
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'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 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;
}
});
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.
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.