Recycling views in a non-scrollable grid view inside ScrollView - android

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?

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).

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.

Related

Android. RecyclerView track viewed items

I have two RecyclerView inside the NestedScrollView and I need track viewed items of my RecyclerViews. But problem that using nestedScrollView the recycler pattern doesn’t work and all the views will be loaded at once because wrap_content needs the height of complete RecyclerView so it will draw all child Views at once.
Is there any way to track of the viewed items with this approach? Please help me.
never use RecyclerView inside NestedScrollView because your ViewHolders will not be recycled and you will be in danger of OutOfMemoeryExceptions, it may work for lists with small sizes but as the list size increases it becomes worse,
instead of using one ScrollView and 2 RecyclerView as child
you have to completely remove ScrollView and instead of that, use one RecyclerView with multitype ViewHolders, in other words you need to combine those 2 RecyclerView in on RecyclerView .
To achieving this you can combine those adapters manually or you can use ConcatAdapter that depends on what you want, this link may also help you if you want to use ConcatAdapter but that is not necessary.
then you can have different approaches to track the visibility of viewHolders like layoutManager.findFirstVisibleItemPosition() and etc.

Which Recyclerview or ScrollView should I choose?

I created an activity with a ListView. That is a Friend ListView.
I wanna let it choose to add it to another View.
I don't know which View to choose is the best. Recyclerview or ScrollView?
Like this
Basic difference between RecyclerView and ListView are the below.
ListView is simple to implement. Default Adapters available.
But ViewHolder pattern and finding views using ID's used to give slow performance.
Now in RecyclerView , the above problems are attended using RecyclerView.ViewHolder.
For RecyclerView adapter, this ViewHolder is mandatory. While comparing to list view this is kinda complex, but solves performance issues in ListView.
Difference LayoutManagers are available while using RecyclerView.
LinearLayoutManager
StaggeredGridLayoutManager
GridLayoutManager
Major improvement of RecyclerView performance while comparing to ListView would be this according to developer.android.com
The RecyclerView creates only as many view holders as are needed to
display the on-screen portion of the dynamic content, plus a few
extra. As the user scrolls through the list, the RecyclerView takes
the off-screen views and rebinds them to the data which is scrolling
onto the screen
To sumup, RecyclerView is preferable than ListView (when UI is having same widgets repeating according to your data)
Now when to use ScrollView:
Your UI elements may not show completely in small device screens. But in bigger screen sizes it may!
Elements may not be necessarily only list / grid. It can have combinations of any UI widgets.
Eg:- TextViews vertically , with RadioButton and Button at last for user action.
This cannot be included in ListView/ RecyclerView , now you can add ScrollView which will have a LinearLayout/RelativeLayout . Inside which all other elements can be added.
Now you can
I recommend to user always RecyclerView and never a ListView. Use RecyclerView for element list and scrollview for static views.
Seeing your image Scrollview inside RecyclerView or ListView have problems with drag.
Use a vertical RecyclerView in all page and horizontal RecyclerView each row.
I suggest you to use RecyclerView because you will load lots of images and if you use ScrollView eventually you will use so much memory. Also it is recommended to use RecyclerView when you have dynamic data. You can look the definition of the RecyclerView in the Android Documentation as follow
RecyclerView is a view for efficiently displaying large data sets by
providing a limited window of data items.
You can use nested RecyclerView for creating such hierarchal view.
You can also checkout this example for nested RecyclerView usage.
Also you can further read:
Android Recyclerview vs ListView with Viewholder
RecyclerView vs. ListView
Should we use RecyclerView to replace ListView?
I hope this helps.

RecyclerView + NestedScrollView + BottomSheetBehavior = bad performance?

Using Support library 27.1.1. I have a NestedScrollView with BottomSheetBehavior. Inside the bottom sheet layout I have LinearLayout with a header view at the top, then a RecyclerView at the bottom. The RecyclerView is populated with 10-20 custom views with heavy onDraw methods.
The whole idea of having a RecyclerView was to avoid inflating these views, as they are quite heavy to render. I want these views to render as soon as they are scrolled into view (dragging the bottom sheet header). The problem is that all children of the RecyclerView are inflated/rendered immediately. I thought I could stop this from happening using a custom LinearLayoutManager but haven't succeeded thus far.
There are a few sources on the net, discussing this particular problem. However, in my case there are a few things that change the conditions.
The RecyclerView is used along with the BottomSheetBehavior. With a height of 0dp the sheet cannot be opened!
The height of the custom views is known to be half the view width.
Support Library 27.1.1 or later is targeted (most discussions on the net consider v23.2).
Q: Is it possible to get the RecyclerView to recycle its views when put inside a NestedScrollView with BottomSheetBehavior? Or would it be easier to somehow prevent the heavy onDraw in the child views?

How to make RecyclerView inflate all items at once?

I was using a ScrollView for displaying some (5-7) heterogeneous information card (Each of them has its different layout and can contain sub RecyclerViews and images which is complex). However for tablet support I'd like to use RecyclerView's existing LayoutManagers for linear/grid layout switching.
When I was using ScrollView, all views are inflated at once, and the lag only comes at loading which is unnoticeable. However when switched to a RecyclerView, the views are inflated upon scrolling and the scroll lags noticeably.
So how can I force RecyclerView to inflate all items at once, and do not recycle them, just like a ScrollView? I've taken a glance at Recyclers and LayoutManagers but couldn't get a clue. Any help is appreciated.
try to add android:fillViewport="true" for your ScrollView in the xml file.

Recycler View as part of a Scroll View

I'm developing an app that has an UI pretty similar to Play Store. It is organized as a multiple panels one above another. First it has a panel containing a photo. Under that it has another panel containing some text and a custom background color. Under that it has another photo and finally it has a Linear Layout with vertical orientation containing a pretty long list of little views filled dynamically at runtime. I have all this inside a Scroll View, naturally.
The problem? That dynamic fill of the linear layout takes a long processor time and makes my app unresponsive during those inner views inflation. So I thought to replace the linear layout by a Recycler View. And the performance is awesome!
So? Well... Not everything is so awesome. I can't scroll the Recycler View because it's inside the Scroll View. And if I remove the Scroll View then I can't scroll the entire view (some things doesn't fit on the screen).
What's the best approach for fixing this?
It's not recommended to use a RecyclerView or ListView inside of a ScrollView precisely due to the double scrolling issues. RecyclerView is very robust and is prepared to receive headers, footers, etc. I see no reason why the entire layout could not be inside of a RecyclerView instead of a ScrollView
The ViewHolder implementation can include logic to inflate different layouts depending on what section should be next.
Pseudocode:
i.e.
if(currentAdapterItem == sectionA){
useLayoutA();
} else{
useLayoutB();
}
Just use a NestedScrollView instead of a normal ScrollView. It handles the nested scrolling quite well.

Categories

Resources