1

I have a ScrollView that contains several other views and I would like for one of these views to be a grid of other views having the same layout (e.g. ImageView).

Since having one scrollable view inside another is not recommended, I would like this grid view not to be scrollable, otherwise I would have used GridView or RecyclerView.

Surely I can place the grid views inside one of the standard layouts (e.g. TableLayout) but this may cause memory issues when many grid items exist. Is there any standard approach or a library that allows to recycle views for a non scrollbale view inside ScrollView?

HaimS
  • 824
  • 8
  • 18

2 Answers2

2

If you try to force GridView or RecyclerView to be non-scrollable (so basically you would have to force the dimensions of the view to display all the elements) you will end up in the same situation as if you used TableLayout (so you would need to watch out for memory issues).

If you disable the scrolling of scrollable (recycling) elements like GridView/RecyclerView you disable the most important part that makes those things work efficiently (that makes those things reuse their views).

The way you should solve your issue is to implement your other Views of your ScrollView as a part of the RecyclerView. Your RecyclerView should be equipped with the adapter that can inflate multiple types of Views (you can read about it for example here).

Community
  • 1
  • 1
Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
  • This is an interesting idea and theoretically it almost solves my problem. The issue that remains is that this special view should be visualized as grid whereas the other views have width=match_parent. So I guess a custom layout manager for the recycler view is required to support this. Do you know if there is one available anywhere? – HaimS Jul 27 '15 at 22:07
  • It's easier than you think. Just go for a regular `GridLayoutManager`, pass the column count (of your "grid part" of your screen) as a `spanCount` in the constructor. Then use `setSpanSizeLookup` method of `GridLayoutManager` to specify for which positions of your adapter you want it to act as a `LinearLayoutManager` (`setSpanSizeLookup` should return `1`), and for which positions you want it to be grid (`setSpanSizeLookup` should return previously mentioned column count). – Bartek Lipinski Jul 28 '15 at 08:29
  • 2
    Oh wow, I wasn't aware that GridLayoutManager provides this functionality. Perhaps it shouldn't have been called Grid given this functionality... Anyhow, thanks a lot for your help! – HaimS Jul 29 '15 at 05:00
0

Since you are using RecyclerView you could use NestedScrollView instead of ScrollView They should play more nicely since RecyclerView extends from NestedScrollingChild and NestedScrollView extends from NestedScrollingParent.

Other views you can use are VerticalGridView or HorizontalGridView but as you said you are worried about performance issues and you can provide a GridLayoutManager to the RecyclerView I would stick with that.

pablisco
  • 14,027
  • 4
  • 48
  • 70