Is it possible to have a nested RecyclerViews with following criterias?
The outer RecyclerView is attached programmatically near the top of the view hierarchy of the Activity
The outer RecyclerView is scrollable
The inner RecyclerView lives inside a custom view which is also attached programatically
The inner RecyclerView has a vertical GridLayout with ImageViews
The inner RecyclerView has width match_parent and height wrap_content
The inner RecyclerView is not scrollable
The inner RecyclerView can displays all it's elements by scrolling the outer RecyclerView
The items inside the RecyclerView are recycled correctly
I am trying to build this but the last two criterias are not satisfied. If I have more than 30 Images (GridLayout has 3 columens, so I have minimum 10 rows) further images are not displayed. And if I open the HierarchyViewer I see that the RecyclerView contains all the inner ViewHolders so nothing is recycled, even if i scroll down.
Furthermore I have questions towards the Images within the grid of the RecyclerView. want to calculate their width dynamically and the images are loaded with Glide Library. I make a request to a server which gives me the images in the right width, but the surrounding ImageViewa have 2 dp less than the images themselves. I have a grid spacing of 8dp. I calculate the width of the images(to be requested) with this formula:
imageWidth = (screenWidth - 2 * horizontalActivityPadding - 2 * spacing) / 3;
This should work for a GridLayout with 3 columns. But then the imageWidth has 90dp and the ImageView's width becomes 88dp. This is because I get a spacing after the 3rd column. But I only calculated twice the spacing so I expect to have this spacing only
between the 1st and the 2nd column
and between the 2nd and the 3rd column
but not after the 3rd column
What may be the reasons for
my RecyclerView problem?
my image spacing problem?
Related
I have a problem with recyclerview items width:
i already looked through android docs and stack so i think, there is no solution to this problem.
i have an ordinary recyclerview with a horizontal gridlayout
sorry, but i dont have enough reputation to embed pictures
Picture of recyclerview
and i want the items of it being shown next to each other, but I want to make the width of the items depend on the width of the parent (in this case, the recyclerview width used in the other fragment).
I can show you what i want by using a fixed value (in this example 200dp)
Picture of the solution I want, but here a fixed value for width is used
However if my constraint of the item is set to "match parent" like here:
Picture constraint set to match parent
The result is, that the width of the items seen in the phone always depends of the width of the recyclerview. in my case its cut nearly in half:
recyclerview cut in half
also android studio only allows one view as a top level layout, so i cannot use a guideline that is set to any %
possible solution to that?
If you have width of recycler view (in pixel not dp), just divide it by number of items in a row, and in your adapter in "onBindViewHolder" method set item width. this solution is when all of your item has same width.
In addition for height of every item use WRAP_CONTENT.
I am creating a recycler view and I have 2 types of itemviews in it. Each item view would have different widths(55dp for one and MatchParent for the another). If I create a recycler view with first 4 rows having width 55dp and the next 2 rows with width MatchParent, do you think my entire recycler view would have same width throughout? Or will it be 55 width for first 4 rows and MatchParent for the last 2 rows?
Please note I have set the width and height as wrapContent for the recycler view and using staggeredGridLayoutManager with single column and vertical scroll.
I only want a single column vertical scrolling recycler view with different widths at each row.
If you can imagine, it should look like an L - shaped recycler view.
Yes, it is possible to have items in RecyclerView to have different height and width.
You should give wrap content for both list item and RecyclerView
We have horizontal recycler view. Each item view of recycler view contains a textView.
Width of each item view is constant, 100dp. Height is dynamic & we have to set it as per the max data.
List of string is coming from server & hence any data can come. So, e.g. 1st cell can have text of 1 line whereas 2nd cell can have text of 4 lines.
Requirement:
We have to calculate maximum height of the cell (i.e cell consuming max height due to biggest text) and set that maximum height to all cells of recycler view.
This is required because we want height of all item views to be same & also making biggest text visible to user without getting cut (ellipse).
One of the ways that I know:
Before setting adapter, we can iterate the list containing Strings. We can create a dummy textview (invisible to user & width 100 dp). We can set each of the text in the textview & then get height of the textview. We will save the maximum height to adapter while calling setAdapter.
Is this good approach?
Please help.
I have two scroll views in a vertical linear layout.
I want them to be relative to each other so that they fill the entire linear layout and compensate if one cant cover half the screen.
Lets call that scroll views TOP and BOT.
If the screen can display 4 rows and both scroll views have infinite rows, each scroll views should display 2 rows and be able to scroll down to se the rest rows.
If TOP has 1 and BOT infinite rows, BOT should be resized to 3/4 of the linear layout.
If TOP have infinite and Bot has 1 row TOP should still just display 2, i.e. it should never pass the linear layouts vertical center.
Here are some pictures for reference:
my setup with weight set to 0.5/0.5.
result of 0.5/0.5 weight. Notice the gray bar above the BOT title bar. This empty space should be filled by the BOT bar.
if Using fixed size or wrap content the TOP will push the Bot out of view.
How can I have them hugging each other and still set TOP to a maximal height?
Preferable in XML.
Its better to set the weight dynamically. Count the number of items in both views. Set the weight of each view according to the ratios of their number of items. You can refer set weight dynamically for setting weights at run time
I am trying to use GridLayout with ImageViews. My problem is that the images I have are all different sizes. I have set the row count and column count, but somehow I need to set a fixed cell size (somehow uniform - as in cell width = total / num of columns, and cell height = total / num of rows). I want to do this so that when I set height and width of imageview to match_parent, the image view doesn't expand beyond the cell size and the layout looks uniform.
I need 5 rows and 3 columns. Can someone suggest how to do this ?
I answered a similar question here. The theory is to use a wrapper layout for your cell and provide the margins that it needs to it. Then use the imageview inside this layout. Thus the outer layout acts as a container for your imageview and it will never go out of the bound of its parent.