Android nestedScrollView - how to make it horizontal? - android

I cant seem to find an option for nestedScrollView to make it horizontal instead of vertical. i have been using HorizontalScrollView but i'd like to switch to nested. Is there anyway to do this ? searching the docs i dont see it yet it does say its the same as a scrollview ?

No
I do not believe NestedScrollView can be scrolled horizontally. According to docs:
For vertical scrolling, consider NestedScrollView instead of scroll view which offers greater user interface flexibility and support for the material design scrolling patterns.
As per the text in bold, I believe NestedScrollViews were only made to scroll vertically.
While NestedScrollViews are "just like ScrollView" (link), the docs do also state that:
Scroll view supports vertical scrolling only. For horizontal scrolling, use HorizontalScrollView instead.
(Both blockquotes come from here)

There is no support to enable the Horizantal nested scrollview in android.
Support is only limited to vertical scrollview.

NestedScrollViews is not a good approach . Better approach is to use recyclerview iniside another recyclerview.

Related

How to disable internal scrolling of a recyclerview but maintain outer list scrolling? (as shown in the material design guidelines)

Here is what I am trying to achieve:
Link to google's material design guide
In my case I have multiple cards in a scrollView, which contains a linear layout. I did not use a recycler view because the number of cards is always about the same (below 10). One of the cards contains two recyclerviews showing a list of for example comments. The problem is that I can't find a way to disable scrolling of the internal recyclerview without causing it to not scroll at all and show incomplete data.
Thanks for your help!
Use a NestedScrollView and put your RecyclerView inside it.
If you wish the RecyclerView to become a scrolling part of your NestedScrollView, set nested scrolling to false.
NestedScrollView nestedScrollView = findViewById(R.id.myNestedScrollView);
nestedScrollView.setNestedScrollingEnabled(false);
If you want an independent scroll you don't need to do anything since nested scrolling is set to true by default.

Layout of 2 RecyclerViews tip

I am trying to create the ui depicted in the design below. Basically it will be an horizontal RecyclerView and below that a vertical one. What I want is that when i scroll vertically, the horizontal one should move too instead of only scrolling the vertical one.
Now I thought of putting the horizontal one in the first cell of the vertical with a different viewholder, but isn't that a bit expensive performance-wise (as i would have to reinitialize and populate the horizontal one after the cell gets recycled and bind() is called)?
So I am asking is there any better approach? Or should I go on with this one?
For the record: minimum API: 17
You can put the 2 RecyclerView inside a NestedScrollView here official documentation.
Hope it helps.

Android: ScrollView vs NestedScrollView

What is the difference between ScrollView and NestedScrollView? Both of them, extend FrameLayout. I want to know in depth pros and cons of both of them.
NestedScrollView as the name suggests is used when there is a need for a scrolling view inside another scrolling view. Normally this would be difficult to accomplish since the system would be unable to decide which view to scroll.
This is where NestedScrollView comes in.
In addition to the nested scrolling NestedScrollView added one major functionality, which could even make it interesting outside of nested contexts: It has build in support for OnScrollChangeListener. Adding a OnScrollChangeListener to the original ScrollView below API 23 required subclassing ScrollView or messing around with the ViewTreeObserver of the ScrollView which often means even more work than subclassing. With NestedScrollView it can be done using the build-in setter.
Other than the advantages listed in the answers given, one more advantage of NestedScrollView over ScrollView is its compatibility with CoordinatorLayout. The ScrollView does not cooperate with the CoordinatorLayout. You have to use NestedScrollView to get "scroll off-screen" behaviour for the toolbar.
Toolbar will not collapse with Scrollview as child of CoordinatorLayout
NestedScrollView
NestedScrollView is just like ScrollView, but it supports acting as
both a nested scrolling parent and child on both new and old versions
of Android. Nested scrolling is enabled by default.
https://developer.android.com/reference/android/support/v4/widget/NestedScrollView.html
ScrollView
Layout container for a view hierarchy that can be scrolled by the
user, allowing it to be larger than the physical display. A
ScrollView is a FrameLayout, meaning you should place one child in it
containing the entire contents to scroll; this child may itself be a
layout manager with a complex hierarchy of objects
https://developer.android.com/reference/android/widget/ScrollView.html
NestedScrollView is just like ScrollView, but in NestedScrollView we can put other scrolling views as child of it, e.g. RecyclerView.
But if we put RecyclerView inside NestedScrollView, RecyclerView's smooth scrolling is disturbed. So to bring back smooth scrolling there's trick:
ViewCompat.setNestedScrollingEnabled(recyclerView, false);
put above line after setting adapter for recyclerView.
I think one Benefit of using Nested Scroll view is that the cooridinator layout
only listens for nested scroll events. So if for ex. you want the toolbar to scroll down when you scroll you content of activity, it will only scroll down when you are using nested scroll view in your layout. If you use a normal scroll view in your layout, the toolbar wont scroll when the user scrolls the content.
<androidx.core.widget.NestedScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
// your Layout xml code
</androidx.core.widget.NestedScrollView>

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.

Scrollbars in android

Can any one tell me how i can set my scroll view with both vertical and horizontal scroll ?
You can't. ScrollView is vertical scroll. HorizontalScrollView is horizontal scroll.
The only View (in the SDK) that I know of that has both vertical and horizontal scroll is the WebView. See if you can work with that. If not, you'll have to create your own View.
If you need both horizontal and vertical scrolling, you shouldn't be using ScrollViews. Instead, as this question points out, you can use a combination of FrameLayouts and RelativeLayouts to achieve the desired effect.

Categories

Resources