Android: ScrollView vs NestedScrollView
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.
What is the difference b/t Default Android Scrolling functionality VS ScrollView / Nested Scrollview?
A ScrollView
is like a little window you can scroll up and down so you can see all its content - it can be any size on the screen. Like @Marrows says, by default nothing scrolls - if a widget is off the screen, it's off the screen!
So if your layout requires stuff that might not all fit - say because there's a variable amount of it, or things can expand (like a large text field), or because on smaller screens (or in landscape) there's potentially not enough room - you need to explicitly allow for some or all of your UI to scroll by putting it in a scroll view.
One typical way to do this is to have fixed items at the top and bottom of the screen, and put a scrollview in the rest of the space. That way, if there's room to display all its contents at once, great! Otherwise if it gets squished, the user can still scroll its display and access everything.
The layout inside the ScrollView
needs to be big enough to hold all its content (usually wrap content
) otherwise stuff will get cut off as usual. The scroll view itself defines how big the window into that layout is. That larger layout just moves up and down behind it.
A RecyclerView
is a type of ScrollingView
which is why you might have thought it was typical behaviour (and TextView
s allow you to scroll inside them too, if they're smaller than their contents)
NestedScrollView scrolls on top when content changes
This is happening because the recyclerView's container is set to wrap_content , so when the height get smaller than the NestedScrollView, it gets scrolled to top.
you can fix it by providing a height larger than NestedScrollView :
So add minHeight :
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nested_scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<TextView
android:id="@+id/description_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="Very long random text..." />
<Button
android:id="@+id/filter_1_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Filter 1" />
<Button
android:id="@+id/filter_2_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Filter 2" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="1000dp">
<!-- Add min height to support scrolling-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp" />
</FrameLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
NestedScrollView/ ScrollView is not scrolling when some layout are included to the child of ScrollView
So, this is the dumbest mistake that took my whole day to figure out.
The first child of my constraint layout is constrained to bottom of tv1 which is not even present in the whole layout. (Copy pasting mistake)
<TextView
android:id="@+id/tv2"
style="@style/RegularBlack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_8sdp"
android:text="My support will include assistance with :"
android:textAlignment="viewStart"
app:layout_constraintTop_toBottomOf="@+id/tv1" />
changed it to
app:layout_constraintTop_toTopOf="parent"
and the scroll started working. :)
NestedScrollView inside a ScrollView is not scrolling for API17
Try changing it into this format and see if it works:
<NestetScrollView>
<Other Views/>
<Other Views/>
<ScrollView>
<CustomTextView/>
</ScrollView>
</NestetScrollView>
NestedScrollView requestRectangleOnScreen not scrolling view to center
view.top
returns the top position of a view relative to its direct parent, in your case it's the top position in relation to a greater parent is required for that you can use a method like the following to aggregate the top position of a view in relation to the scrollView and scroll to it
private fun ScrollView.scrollTo(target: View) {
var topPosition = 0
var view = target
while (view !== this) {
topPosition += view.top
view = view.parent as View
}
smoothScrollTo(0, topPosition)
}
so to scroll to any view you just call scrollView.scrollTo(view)
Related Topics
Scan Android Sd Card for New Files
Android Studio: Gradle: Error: Cannot Find Symbol Variable
Using Camera in the Android Emulator
Run Task Before Compilation Using Android Gradle Plugin
How Does One Use Basic Authentication with Volley on Android
Change Keyboard Input Language
How to Implement My Own Disk Cache with Picasso Library - Android
Java.Lang.Noclassdeffounderror: Com.Google.Android.Gms.Internal.Zzmp
How to Set Alarm in Android Programmatically
Suppress/Block Broadcastreceiver in Another App
Android: Fragments Overlapping Issue
Android Studio 3.0 Manifest Error: Unknown Element <Action> Found
Android: Gles20: Called Unimplemented Opengl Es API
How to Add Android Support V7 Libraries in Eclipse
How to Default to Numeric Keyboard on Edittext Without Forcing Numeric Input