Nested Horizontal view inside RecyclerView - android

I want Horizontal scrollable items in vertical recyclerView. horizontal items will not be more than 10 in any case.
I thought of two approaches
1) Horizontal Recyclerview as item in vertical RecyclerView
2) Custom horizontalScrollView as an item in vertical RecyclerView
I have implemented first one facing some issues in scrolling but I think I will manage that.
My question is, why not second approach. I have seen all related question on SO and most of them trying to do with first approach. Why is it so?
What is disadvantage? specially I you have limited number in horizontal view.
Any pointer would be great help.

Why do we use RecyclerView instead of LinearLayout?
Because if we use a LinearLayout consisting of (let's say 50 items) they will be all rendered on screen (50 Views) and this will lead to very very bad performance issues and scalability issues. RecyclerView does initialize the views that appears on screen only ,let's say 7 views, and they will be recycled in every new row that comes.
Why do we use Horizontal Recyclerview instead of Horizontal ScrollView?
This is not usually the case. If you have multiple items in every row that needs to be recycled then you should use Horizontal RecyclerView otherwise it won't matter at all.
To summarize,
The views in the vertical RecyclerView will got recycled whether it has another horizonal RecyclerView or Horizontal ScrollView, but the views inside the Horizontal ScrollView will not get recycled and the views inside the Horizontal RecyclerView will got recycled as it's a RecyclerView.
If you don't have multiple items in your Horizontal RecyclerView, you shall use Horizontal ScrollView instead.

Related

Impelement two recyclerview in one screen

How can I implement this kind of UI, where we have two recyclerViews. One scrolls horizontally and the second one vertically. when the second one scrolls first one also scrolls top together.
I tried to implement using NestedScrollView, but I had to make second recyclerView height wrap content which causes recyclerView not recycle.
The second way that I tried was having one recyclerView. And adding horizontal recyclerview as a header. The problem was to save header recyclerview scroll state when navigation. And there had been crashes when loading next page (paging 3) in header recyclerView.
The question is: Is there any optimal solution for this kind of ui?
In cases, Like this, you don't have to use 2 RecyclerView and you also have to avoid using RecyclerView insideScrollView. instead of this you have to use one vertical RecyclerView with multitype view Adapter.
in this way, you are going to have 2 different ViewHolder one of them is a horizontal recyclerView (your top item) and the other one is your other items.
for learning multitype adapter you can see this:
How to create RecyclerView with multiple view types
and for a horizontal recyclerView inside a vertical RecyclerView you can see this :
https://medium.com/#ashishkudale/android-list-inside-list-using-recyclerview-73cff2c4ea95
you have to combine these 2.
I could not understand the meaning of "header" where you said "adding horizontal recyclerview as a header" but if you did what I told and the problem is the state of inner Horizontal recyclerView, I think probably you are calling setAdapter method of horizontal RecyclerView in OnBind() method of your vertical recycler view, it is a common mistake that I have seen in many tutorials.
if you have done this mistake , try to call setAdapter of your inner recyclerView in the constructor of its viewHolder and just update the list using yourHorizontalAdapter.notifyDataSetChanged() in onBind() method of VerticalRecylerView,
and if its not the case and your recyclerView is completely destroying see this link :How to save RecyclerView's scroll position using RecyclerView.State?

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.

Horizontal Recyclerview inside a Vertical Scrollview

I have a situation where I have a Recyclerview which slides horizontally inside a ScrollView. Now the situation is when I do a horizontal swipe on the Recyclerview, instead of scrolling the cardviews inside Recyclerview, it scrolls the screen up, which disturbs the user experience.
Any solution or approach to avoid vertical scrolling when the person is doing a horizontal swipe on Recyclerview?
The solution is actually quite simple.
put the horizontal RecycleView as an item in a RecycleView, instead of using a scrollview.
then when you scroll it behaves as you want it to.
Thats what I did and it works perfectly

Is it possible to have a ScrollView inside another ScrollView?

I want to learn how to solve this problem. I want to have a Horizontal scrollview with the scroll blocked (the user should not be able to scroll it) and inside that horizontal scrollview i want to have another horizontal scroll view, and this scrollview must be able to be scrolled by the user (it haves more content that the width of the screen).
Is it possible to do it?
I tried using this on the parent horizontal scroll view:
((HorizontalScrollView) view).setHorizontalScrollBarEnabled(false);
((HorizontalScrollView) view).setEnabled(false);
((HorizontalScrollView) view).setFocusable(false);
((HorizontalScrollView) view).setFocusableInTouchMode(false);
and this on the child horizontal scroll view:
((HorizontalScrollView) view).requestFocus();
It is not working, the child appears to have a scroll bar, but it cannot be scrolled.
How can this be solved?
PD: I know that this is not a good practice, but I want to learn how to achieve this goal.
You should never use a
HorizontalScrollView with a ListView,
since ListView takes care of its own
scrolling. Most importantly, doing
this defeats all of the important
optimizations in ListView for dealing
with large lists, since it effectively
forces the ListView to display its
entire list of items to fill up the
infinite container supplied by
HorizontalScrollView.
http://developer.android.com/reference/android/widget/HorizontalScrollView.html
UPDATE:
Since you may be forced to use a two dimensional scrollview, you may consider using this:
http://blog.gorges.us/2010/06/android-two-dimensional-scrollview/
I haven't used this but it may be a reasonable approach.
you can do it. But you have to handle child layouts in scrollview i.e ScrollView can host only one direct child.

Categories

Resources