How to make RecyclerView inflate all items at once? - android

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.

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.

Vertical RecyclerView containing nested RecyclerView

Structure of my Android app is forcing me to create Vertical Nested RecyclerView inside Vertical Nested RecyclerView because of the pre-drawing all nested items inside every RootRecyclerView. The View of RootRecyclerView has approximately 5 - 10 views and the View of InsideRecyclerView consists also from 5-10 views. There is not necessarity for scrolling this ChildRecyclerView, because all scrolling is handled by RootRecyclerView (same term as Nested RecyclerView)
I have two options:
1. ChildRecyclerView, that will predraw all child items when the root item will reached the screen (it lags, but the result looks good under the 30 child rows)
2. Join the Root+Child View, remove Child RecyclerView and the all logic will be handled in the Root RecyclerView. - i tried this, also used ConstraintLayout, so the whole layout was only 1-LEVEL, but it was still laggy, even if I set Visibility of half of the views in this layout. I also setup setHasFixedSize(true), tried to use Cache methods of RecyclerView, initialCacheItemSize and so on.
So do you have any ideas, how to solve this problem? Did somebody of you met with this kind of problem? You could also imagine the case of ChildRecyclerView inside another ChildRecyclerView that is inside RootRecyclerView.

Recycling views in a non-scrollable grid view inside ScrollView

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.

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.

Linear layout with nested listview scrolling

I have a LinearLayout with a nested listview which looks like this:
<LinearLayout ... >
<LinearLayout>
</LinearLayout>
<ListView>
</ListView>
</LinearLayout>
The problem is that the listview owns the scrollbar (so only content in the listview is scrollable) but i actually want the parent LinearLayout to own the scrollbar (so making the entire content scrollable).
Wrapping parent ListView in a ScrollView hasn't been successful because the ScrollView doesn't recognize ListView height (which looks like is rendered at running time)
Thanks
Edit: SOLVED My perfect solution was using a MergeAdapter, as advised by Barak
You can use CommonWares MergeAdapter which allows you to define views and list adapters, pour them into the MergeAdapter and get a single list adapter out, containing everything you poured in, and it scrolls as one list.
A previous answer about MergeAdapter I gave with some instructions is here
You could replace the Listview with a Tableview instead, as long as the listview does not have too many items in it. You can still use a childview with the tableview in much the same way as the listview, you just won't be able to databind it in the same way as you can with a listview, and the items won't recycle either.
Since the listview is designed to contain more items than can be displayed the height will never exceed the screen size (best case) which is the intent of the control, though I suppose you could force it to be larger that seems generally like a bad idea for a lot of reasons.
I suspect what you should do is either create a custom listview adapter, and based on position in the list create the view you want, this would allow all items to scroll like you want.
Listview with different view types per row
This may or may not work depending on what exactly you are trying to do, otherwise you might just want to add views to a linear view inside a scroll view san's the listview.
It just depends on the use case (how many items in the list view, memory issues etc) and what the purpose of the other views are.

Categories

Resources