- How to implement NestedScrolling on Android?
- Android — NestedScrollView doesn’t scroll
- 4 Answers 4
- Android: ScrollView vs NestedScrollView
- 6 Answers 6
- Programmatically scroll to the top of a NestedScrollView
- 13 Answers 13
- Tutorialwing
- Output
- Getting Started
- Attributes of Android NestedScrollView Widget
- Example of Android NestedScrollView Widget
- 1. Creating New Project
- 2. Modify Values folder
- 3. Download Drawable Resources Needed
- 4. Use NestedScrollView Widget in xml file
- 5. Access NestedScrollView Widget in java file
- AndroidManifest.xml
How to implement NestedScrolling on Android?
With support-v4 library 22.1.0 android supports nested scrolling (pre android 5.0). Unfortunately, this feature is not really documented. There are two interfaces ( NestedScrollingParent and NestedScrollingChild ) as well as two helper delegate classes ( NestedScrollingChildHelper and NestedScrollingParentHelper ).
Has anyone worked with NestedScrolling on Android?
I tried to setup a little example, where I use NestedScrollView which implements both NestedScrollingParent and NestedScrollingChild .
My layout looks like this:
I want to display a header view and another NestedScrollView (id = child) in a NestedScrollView (id = parent).
The idea was, to adjust the height of the child scroll view at runtime by using a OnPredrawListener :
So the header view will be scrolled away partially, 40 pixels will remain visible since I set the height of the nested child scroll view to parentScroll.getHeight() — 40 . Alright, setting the height at runtime and scrolling the parent scroll view works like expected (header scrolls out, 40 pixels remain visible and then the child scrollview fills the rest of the screen below the header).
I would expect that «NestedScrolling» means that I can make a scroll gesture anywhere on the screen (touch event caught by parent scroll view) and if the parent scroll view has reached the end the nested child scroll view beginns to scroll. However that seems not to be the case (neither for simple scroll gestures nor for fling gestures).
The touch event is always handled by nested child scrollview if the touch event begins in its boundaries, otherwise by the parent scrollview.
Is that the expected behaviour of «nested scrolling» or is there an option to change that behaviour?
I also tried to replace the nested child scroll view with a NestedRecyclerView . I subclassed RecyclerView and implemented NestedScrollingChild where I delegate all methods to NestedScrollingChildHelper :
but the NestedRecyclerView doesn’t scroll at all. All touch events are caught by the parent scroll view.
Источник
Android — NestedScrollView doesn’t scroll
I have a problem with xml. When I create a NestedScrollView in android studio, it doesn’t scroll in the xml preview. It is so important because if I don’t see preview I can’t see my work. Also when I start the application, I see the latest version that should no longer be there because it was replaced by this code.
Here is my xml code:
4 Answers 4
just change this code in your xml The height of the NestedScrollView Always match_parent
The height of the NestedScrollView should be match_parent . And the child of it ( RelativeLayout ) should have height wrap_content .
ScrollViews usually don’t scroll in the XML preview. You can only see them on the device. That’s why its always better to have an android phone connected to your system for live testing.
That way, you can easily correct any errors
This is not issue of height of NestedScrollView
New Android Studio has issues with scrolling ScrollView in Layout Editor. In older versions ScrollView had smooth scrolling behavior in Editor. This is a bug of Android Studio.
Solution that i usually do
- You can see Design & Text at bottom of Layout Editor. Click on Design, there you will be able to scroll. It works perfectly all times i do.
- If you want Text Editor scrollable, then restart your IDE ( File>Invalidate Cache/ Restart>Just Restart ). My Text Editor start working by this trick.
- Just a work around : You can make your upper views invisible temporarily to see your bottom views.
Источник
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.
6 Answers 6
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.
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.
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
Источник
Programmatically scroll to the top of a NestedScrollView
Is there a way to programmatically scroll to the top of a NestedScrollView by also triggering the scroll events for the parent? smoothScrollTo(x, y) doesn’t delegate the scroll events to the NestedScrollingParent .
13 Answers 13
I managed to do it (animated scrolling):
where 10000 is the velocity in y-direction.
Another way to smooth scroll NestedScrollView to all the way up or down is using fullScroll(direct) function
Nothing worked for me, and I don’t really know why this worked, but here is my solution and problem.
When adding recycler view to a nested scroll view, it showed recycler view on screen when getting to that activity. In order to scroll all the way up, I had to use this:
I also faced similar kind scenario. In my case when I scroll down to end, FAB button should be appears and when user tap on that FAB button should go to the top of the page. For that I added @SilentKnight answer NestedScrollView.scrollTo(0, 0); For go to the top but which is not enough for smooth animation for scroll up.
For smooth animation I have used @Sharj answer which is NestedScrollView.fullScroll(View.FOCUS_UP); But then my AppBar is not visible there fore I have to expanded the AppBar as following appBarLayout1.setExpanded(true) . So using these three I can able smoothly go to top of the page.
You can use this for smooth scroll.
Источник
Tutorialwing
Hello Readers! In this post, we are going to learn about how to use android nestedScrollView widget in any android application. We will also go through different attributes of nestedScrollView widget that can be used to customise it.
Output
Tutorialwing Android NestedScrollView Output
Tutorialwing Android NestedScrollView Output
Getting Started
NestedScrollView widget can be defined as below –
As the Name suggests, NestedScrollView is a widget that are used when we want to implement scrollable view inside another scrollable view.
Normally, it is difficult to implement scrollable view inside another scrollable view because system would be unable to decide which view to scroll. This is where NestedScrollView comes in.
Attributes of Android NestedScrollView Widget
Attributes of nestedScrollView is same as scrollView. It is a support v4 widget that runs on old version of android as well. It is added in api 22.
Example of Android NestedScrollView Widget
At first, we will create android application. Then, we will use nestedScrollView widget in this application.
1. Creating New Project
Follow steps below to create new project. Please ignore the steps if you have already created a new application.
Step | Description |
---|---|
1. | Open Android Studio. |
2. | Go to File => New => New Project. Write application name as NestedScrollView. Then, click next button. |
3. | Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button |
4. | Then, select Empty Activity => click next => click finish. |
5. | If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail. |
Now, we will modify xml and java file to use nestedScrollView widget in the application.
2. Modify Values folder
Open res/values/strings.xml file. Then, add below code into it.
3. Download Drawable Resources Needed
You will need some images, stored in res/drawable folder, to be used in the application. These drawable images will be used by views inside nestedScrollView widget in the application.
4. Use NestedScrollView Widget in xml file
Open res/layout/activity_main.xml file. Then, add below code into it.
In activity_main.xml file, we have defined nestedScrollView inside scrollView. Using nestedScrollView, we have defined scrollable view inside another scrollable view. It is used same as scrollView i.e. It can contain only one direct child. Note that we have defined linearLayout inside nestedScrollView as a direct child. Then, we have defined view to be scrolled inside it. All the imageViews have been defined inside scrollView.
5. Access NestedScrollView Widget in java file
Open src/main/java/com.tutorialwing.nestedscrollview/MainActivity.java file. Then, add below code into it.
We have changed nothing in MainActivity. So, it is as it was after creating project.
Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.
AndroidManifest.xml
Code inside src/main/AndroidManifest.xml file is as below –
When we run the program, we will get output as shown above.
That’s end of tutorial on Android NestedScrollView widget.
Источник