Moving from GridView to GridLayoutManager - android

I am trying to move my app from using GridView to using RecyclerView with GridLayoutManager. I am new to RecylcerViews, but have successfully converted my ListViews, now working on my GridView. A couple things I am unsure about:
My current GridView has a certain number of columns, determined at runtime, with each grid column the same, hard-coded width. It is scrollable both horizontally and vertically (I wrap my Gridview in a HorizontalScrollView). So I basically need to have a view with a set number of columns that are a set width, irrespective of screen width.
I have been having problems finding a method to set the column(span) width for GridLayoutManager, so I assume that is not how GridLayoutManager. It almost sounds like it is built to always fit all columns on the screen, rather than letting them spill off the screen? What is the best way to tell GridLayoutManager that I want, for example, 6 columns that are each 150 units wide (either dp or pixels)?
For scrolling in both directions, it sounds like I can use my current approach and just wrap my RecyclerView in a HorizontalScrollView, is that correct?

Make the width of the RecyclerView wrap_content and set the number of spans you want. (Make sure parents of the RecyclerView are also wrap_content.) When you create the item views in the RecyclerView's onCreateViewHolder() make sure that it is the width that you want. The RecyclerView will grow to the width of the view holder layout times the number of spans.
All you need to do now is to wrap everything in a HorizontalScrollView.

Related

How to set different widths for RecyclerView cells?

I want a layout where the first cell is the full width of the RecyclerView, and the rest are half the width. There doesn't seem to be a built in function that lets me set this.
You can achieve this with GridLayoutManager.
By default, each item occupies 1 span. You can change it by providing a custom GridLayoutManager.SpanSizeLookup instance via setSpanSizeLookup(SpanSizeLookup).
https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager

Android GridLayout different span count based on row width

I have a recyclerview with GridLayoutManager as layout managar and what I want to achieve is like image below
As you can see every row may have multiple item just based on item width and each item is a TextView with wrap_content width but all item have same height.
I know it is possible to set row span by SpanSizeLookup but for doing that, I should know the count of spans before while my list rows will fill just by items width
Can anyone help me?
You might be better off with a different Layout manager like FlexboxLayoutManager https://github.com/google/flexbox-layout
It has lots of flexibility of on controlling when the wrap the row (automatic or manual) and you can specify lots of controls on how individual cells can grow/shrink to fill a line.
There are lots of examples on the github page.
But as a starting point you could manually wrap before cells 3 and 7 to give you the 3 on the first row and 4 on the second.
Or setting FlexWrap will do it automatically based on size

Android Horizontal RecyclerView with dynamic amount of rows and columns

I need to create a Horizontal RecyclerView with dynamic rows and columns, and every cell has the same width, but different height. Which means column width should be consistent and row height is dynamic.
I've tried implementing this behavior with FlexBoxLayoutManager, StaggeredGridLayoutManager, GridLayoutManager. Also attempted with GridView but then realized it's not supported with horizontal scroll.
I'm trying to find something that is similar to UICollectionView in iOS.
As seen in the photo, columns are unlimited, and number of cells per column depends on the height of the cells. So each column has a different number of cells.
FlexBoxLayoutManager supports what you're trying to achieve.
You want to use it within a RecyclerView have a look at FlexDirection
https://github.com/google/flexbox-layout#flexboxlayoutmanager-within-recyclerview

RecyclerView incorrect manual positioning

I'm trying to align some items inside a RecyclerView with others outside it. However, seems like all my tries are failing.
What I'm trying to do specifically: I have a couple of views as a header which are aligned using various methods* then a RecyclerView just below it, in its same parent, stretching the whole width, that inflates views.
The problem: Items inside the RecyclerView do not align perfectly to the items outside of it. Knowing that I'm using the same layout for both the parent and the items! And knowing that whatever method I used for horizontal alignment for the header items, is exactly used the same way for each item inflated for the RecyclerView. AND knowing that both items have same properties (they're TextViews, same size, width, height, etc...)
What methods have I tried? FOR BOTH SIDES (up and down), I tried the following:
Using ConstraintLayout for both parent and item layouts, and stretching the items between both start and end of parent, then using the constraint ratio to position it exactly where I want. Outcome for header is different from that of RecyclerView holders.
Using a TableLayout with children stretched across the whole width, same number of columns, same stretching, still different outcome, not pixel perfect.
Placing the header views exactly how I want, but not using any Android placement 'methodology' at all, but instead getX() and setX() later inside the adapter (using .post() so I ensure the position is accurate after inflation) and STILL the same wrong placement.
What am I missing? Shouldn't a RecyclerView inflating children that span the whole width when I specify they should match_parent? I tried debugging the X values for header and view holders, THEY'RE THE SAME but my eyes see different things. I delayed it for some milliseconds but this didn't change a thing.
So the problem turned out that whatever method I used, if the TextViews widths were wrap_content it'll always fit the word inside it. The solution was setting the width to 0dp and let the parent ViewGroup balance everything evenly. In my case I used a TableLayout.

How to set row height of GridView fit to Screen in Android

I want to stretch the row of a GridView to match the height of the screen in Android.
How do I do this?
You are in control over your row heights, by virtue of what you put in
them. Since your cells appear to have more than one widget, they are
presumably wrapped in a LinearLayout or something. Set your
LinearLayouts to be some specific height, and the rows will all be
that height.
Personally, I think you should be resizing your images if you are
going to have text above and below each image on a per-cell basis.
With the Reference commonsware link

Categories

Resources