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
Related
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
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
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.
I want to achieve something similar to the attached image
I was thinking of using TableLayout with 2 columns for each row.
There will be padding in order to be apart of each other. Then I will put white background for the cell. Lastly, I'll just add the ImageView and TextView.
The images and text are dynamically generated. I will get the image URL and display them.
So, are there any better or more efficient way for implementing what I want to achieve? TableLayout doesn't seem to be that efficient.
Your diagram looks pretty much like a grid - for which you can use a GridLayout in Android: https://developer.android.com/reference/android/widget/GridLayout.html
A layout that places its children in a rectangular grid.
The grid is composed of a set of infinitely thin lines that separate the viewing area into cells. Throughout the API, grid lines are referenced by grid indices. A grid with N columns has N + 1 grid indices that run from 0 through N inclusive. Regardless of how GridLayout is configured, grid index 0 is fixed to the leading edge of the container and grid index N is fixed to its trailing edge (after padding is taken into account).
Every one of the items in the grid can be a Cardview (https://developer.android.com/training/material/lists-cards.html) - and that way you will also benefit of a consistent look and feel with Android, without much effort.
Take into account that the cards (every item in the grid) will have the same height, tho: How to make a grid layout of CardViews with variable height?.
If the height of the elements will be variable, you should better take a look to the StaggeredGridLayoutManager: https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html
What you have to do is use a RecyclerView with a GridLayoutManager.
For a full working implementation: https://inducesmile.com/android/android-gridlayoutmanager-with-recyclerview-in-material-design/
Is there a way to determine if a list view needs to scroll to display all of its contents?
In other words, are the combined heights of all the children greater than the height of the list view itself.
I have not tried using it this way, but in principle if getLastVisiblePosition() equals getCount()-1, all items should be visible. It's possible that there is still a bit of scrolling needed, less than the height of a row, and I don't know if that matters in your case or not.
If it does, you could always iterate over the visible children and sum their heights.
I tried some ways a while ago and ended with simply calculating and comparing the height of the list view with the sum of the heights of all items and the sum of all separators.