18

I have a strange issue with NestedScrollView fling on Nexus 5x (7.1.2) and Google Pixel (7.1.1). On other OS versions it works OK.

Fling animation sometimes stops right after lifting up a finger. It stucks and the next few flings may be stopping as well. In order to reproduce it, you need to fling several times up and down.

In logs these flings look pretty much the same in terms of velocity, direction, etc, so I can't find a real cause of this bug.

Also NestedScrollView doesn't necessarily need to be inside of CoordinatorLayout, it also can have no NestedScrollingChild at all.

For example, this bug is reproducible with one of the following NestedScrollView children:

1) LinearLayout populated with TextViews

2) WebView

3) LinearLayout populated with RecyclerViews.

I know about possible issues with RecyclerView and Behaviours inside of CoordinatorLayout, but it's not related. So please don't mention any

recyclerView.getLayoutManager().setAutoMeasureEnabled(true);
recyclerView.setNestedScrollingEnabled(false);

or things like that.

Code sample:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:text="Put a super long text here"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:text="Put a super long text here"/>

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>
Dimezis
  • 1,541
  • 11
  • 24
  • Have you tried adding different scrolling behaviours? – Nick Cardoso Mar 19 '17 at 18:53
  • 1
    seems like there's a bug in support libraries https://code.google.com/p/android/issues/detail?id=183738 the link comes from this response where they say you can "simulate" the behaviour http://stackoverflow.com/questions/31795483/collapsingtoolbarlayout-doesnt-recognize-scroll-fling – eduyayo Mar 20 '17 at 16:24
  • @NickCardoso as I said, it doesn't depend on CoordinatorLayout at all – Dimezis Mar 20 '17 at 20:32
  • Post gif image of problem if possible. Post code for animation. – Pravin Divraniya Mar 21 '17 at 10:06
  • 1
    I can confirm this issue as well. Its very simple to reproduce. just create a nestedscrollview with any content in it. See the behavior difference with a normal scrollview. when you quickly flink up and down it often stops scrolling instantly. I am going to create a repo where you can quickly compare both scenarios. – Gillis Haasnoot Apr 10 '17 at 00:32
  • 1
    I have created a repo where you can quickly test the different behavior of the nestedscrollview and the normal scrollview. https://github.com/holoduke/nestedscrollviewtest – Gillis Haasnoot Apr 10 '17 at 15:28
  • @GillisHaasnoot thanks a lot. if you don't mind, I will add to the corresponding google tracker issue. Edit: ah, you already did it :) – Dimezis Apr 11 '17 at 16:30
  • @GillisHaasnoot if you are interested, I have created a workaround while we are waiting for a proper fix: https://github.com/Dimezis/FlingableNestedScrollView/ – Dimezis Apr 12 '17 at 10:07
  • 1
    Should be fixed now: https://chris.banes.me/2017/06/09/carry-on-scrolling/ – granko87 Jun 26 '17 at 07:35
  • @granko87 it's fixed indeed, just checked – Dimezis Jun 26 '17 at 10:53

2 Answers2

7

So it's clearly a bug in NestedScrollView. I have made a workaround for this, but still waiting for a proper fix from Google side.

https://github.com/Dimezis/FlingableNestedScrollView/

Edit:

Looks like the issue is fixed in support lib 26.0.0-beta2

https://chris.banes.me/2017/06/09/carry-on-scrolling/

Edit 2: Although the scrolling works fine now, in my app I can constantly reproduce this bug:

https://issuetracker.google.com/issues/37051723

If someone encounters it as well, you can find a workaround in a thread mentioned.

Dimezis
  • 1,541
  • 11
  • 24
  • This helped me a lot. Thanks :) – sauvik May 31 '17 at 10:17
  • There are still issues open with support lib 26. It's enough to see that the android project created by Android Studio "ScrollingActivity" and the Google IO App itself are presentig errors while scrolling. – David Sep 08 '17 at 12:07
0

according to Animating a Scroll Gesture training guide, while overriding computeScroll(), after using mScroller.computeScrollOffset() to calculate proper offset to scroll view, we need use:

ViewCompat.postInvalidateOnAnimation(this);

to animate next scroll. However, in the NestedScrollView, the computeScroll() looks like this:

public void computeScroll() {
    if (mScroller.computeScrollOffset()) {
    ...     
    }
}

It doesn't request next scroll animation! Which means that after using mScroller.fling(...), the computeScroll() method will sometimes only get called one time, and view doesn't keep fling.

To fix this problem, I have tried to replace the computeScroll in this way:

public void computeScroll(){
    if(mScroller.computeScrollOffset()){
       ...
       ViewCompat.postInvalidateOnAnimation(this); 
    }
}

It may not sound a good solution, but it just works fine for now.

Recent version of NestedScrollView has added the ViewCompat.postInvalidateOnAnimation(this).

Xiang Zhang
  • 43
  • 1
  • 6