Set logic fill data in RecyclerView - android

I have Recycler View to show my grid.
My data is int[] list = {1, 2, 3, 4, 5, 6, 7};
And I use code
mRv.setLayoutManager(new GridLayoutManager(mRvIcon.getContext(), 5, GridLayoutManager.VERTICAL, false));
mRv.setAdapter(myAdapter);
and I have result:
But I want fill data same
How I can do it?

try to add layout manager like this
new GridLayoutManager(this, 5, GridLayoutManager.HORIZONTAL, false)

You can use horizontal mode with 4 column:
mRv.setLayoutManager(new GridLayoutManager(mRvIcon.getContext(), 4, GridLayoutManager.HORIZONTAL, false));
mRv.setAdapter(myAdapter);

Thank you every one. I did it.
mRvIcon.setLayoutManager(new GridLayoutManager(mRvIcon.getContext(), 2, GridLayoutManager.HORIZONTAL, false));
But I need custom params of item view with width = 1/5 screen width in onBinViewHolder.
Thank you so much for your time!

Related

Android grid layout with circular item in random position

I am trying to implement different grid layout structure but unfortunately not getting success. So please look on below screenshot and tell me is it possible to implement gridview or recyclerview like below screenshot. Thanks in advance :)
Use GridLayoutManager with spancount 3
Now set the custom top margin to your root layout for the second position.
Apply this logic to your RecyclerViewAdapter's onBindViewHolder
if (position % 3 == 1) {
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeParams.setMargins(0, 50, 0, 0); // left, top, right, bottom
holder.rel_root.setLayoutParams(relativeParams);
}
not sure if it's correct way but works :)

How to create rtl GridLayoutManager in Android? [duplicate]

This question already has an answer here:
How can I fill RecyclerView with GridLayoutManager from right to left
5 answers
I'm creating a recyclerView in android project and i want to set a gridLayout manager in my recyclerView .
here is my Code :
workHourRecycler = view.findViewById(R.id.market_hours_recycler);
workHourRecycler.setLayoutManager(new GridLayoutManager(getContext(),4));
But GridLayout must be 'rtl direction' anyone have idea ?
Thanks.
You need to programmatically change the layout direction of the RecycleView
workHourRecycler = view.findViewById(R.id.market_hours_recycler);
workHourRecycler.setLayoutManager(new GridLayoutManager(getContext(),4));
//Programtically set the direction
workHourRecycler.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 2);
rvProductlist.setLayoutManager(mLayoutManager);
/**
* #param context Current context, will be used to access resources.
* #param spanCount The number of columns or rows in the grid
* #param orientation Layout orientation. Should be {#link #HORIZONTAL} or {#link
* #VERTICAL}.
* #param reverseLayout When set to true, layouts from end to start.
*/
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 4, GridLayoutManager.HORIZONTAL, true);
rv_days.setLayoutManager(mLayoutManager);
rv_days.setItemAnimator(new DefaultItemAnimator());
Try this
GridLayoutManager layoutManager = new GridLayoutManager(this, numberOfRows, GridLayoutManager.HORIZONTAL, true);

RecyclerView with CardView as Dashboard or like a Windows Tiles

I spent many time looking for the solution How to do the Windows tiles liked interface, also knowing as Dashboard. I want use for it RecyclerView with CardView. I found old solution but there used LinearLayout with Buttons: How to create layout with 6 buttons like windows tiles
What I have at this moment:
What I want:
I have six CardViews, they should occupy the entire area of the parent RelativeLayout and be the same size regardless of the width of the screen.
In portrait mode I have 2 columns and in landscape - 3 cols.
recyclerView.setHasFixedSize(true);
int columns = resources.getConfiguration()
.orientation == Configuration.ORIENTATION_LANDSCAPE ? 3 : 2;
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(), columns);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(columns,
Utils.dpToPx(resources, 3), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setLayoutManager(mLayoutManager);
How can I do that?
As always, after I write here a question, after a while the solution comes by itself. Here's how I solved this
In your RecyclerView Adapter:
#Override
public HomeTilesAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.home_tile_item, parent, false);
int h = parent.getHeight();
// margin - activity_vertical_margin
// rows - number of rows in diffrent display modes
h = (h - Math.round(margin*2))/rows;
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) itemView.getLayoutParams();
params.height = h;
itemView.setLayoutParams(params);
return new MyViewHolder(itemView);
}
This decision solves the problem, but in the process I faced another problem. On the tablets the icon looks very small, it looks not beautiful. How I solved this issue you can read this post: ImageView size for phones and tablets
Result

Add vertical columns in horizontal RecyclerView

I created a RecyclerView horizontal on my project. I want to have horizontal rows with different sizes and multiple vertical columns and each contains a different number of rows, like this image:
The number of horizontal rows is variable. Column number one can have four rows,column two can have three,four...Number of columns is also variable.
The result would be like a tv guide, the column is the channel and rows the emission.Only this is not a guide to tv xD
I've done the horizontal rows in this way:
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(layoutManager);
public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.VerticalItemHolder>{
#Override
public void onBindViewHolder(VerticalItemHolder itemHolder, int position) {
Item item = arrayItems.get(position);
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, item.getSizeRow(), root.getResources().getDisplayMetrics());
ViewGroup.LayoutParams params = root.getLayoutParams();
if (params == null) {
params = new ViewGroup.LayoutParams(heigth, width);
} else {
params.width = width;
params.height = heigth;
}
root.setLayoutParams(params);
}
The result is like this image:
But I can’t get certain items are displayed in the vertical column. How is it done? thanks
Answered from here for using Variable span size in RecyclerView
Use GridLayoutManager instead of LinearLayoutManager as below:
GridLayoutManager manager = new GridLayoutManager(this, 3);
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
return (3 - position % 3);
}
});
recyclerView.setLayoutManager(manager);
Here, I have to explain few things. see below image-
In above code, the span size is 3. It means that the maximum number of columns you want in any row is 3.
In above code, the method getSpanSize returns the span size of the position.
Span sizes for Above code:
if position is 0, span size is 3
if position is 1, span size is 2
if position is 2, span size is 1
if position is 3, span size is 3
if position is 4, span size is 2
if position is 5, span size is 1
... and so on.
If you understood this, it might help you.

StaggeredGridLayoutManager change spanCount dynamically RecylerView

How can we change the spanCount of RecylerView 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.
enter code here 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;
}
});

Resources