Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

android - NestedScrollView's fullScroll(View.FOCUS_UP) not working properly

I have a NestedScrollView populated with a vertical LinearLayout, which itself has a bunch of children of various view types: multiple TextViews, two static GridViews, and even a FrameLayout to show a Fragment beneath all of this.

When pressing the back button, if the user has scrolled below a certain point, instead of finishing the Activity, the "scrollToTop" method is called:

public static void scrollToTop(final NestedScrollView scrollview) {
    new Handler().postDelayed(new Runnable() {
        public void run() {
            scrollview.fullScroll(NestedScrollView.FOCUS_UP);
        }
    }, 200);
}

This works in the previous version of my app, which is in the Play Store. But now, after updating my app to target Android Oreo (and updating the support library to version 26.0.2), instead of scrolling to the top, it seems to start scrolling from below the NestedScrollView's original scroll position, and stops where it was. So it just appears as a weird stutter. At some positions, however, it does scroll to the top (albeit very rarely and inconsistently), and others it actually scrolls to the bottom, for what reason I don't understand.

I have been experimenting with view focusability, to no avail. For example, I read that the Static GridViews may interrupt focus while scrolling. I've also tried various different methods to scroll up, such as

scrollview.pageScroll(View.FOCUS_UP);

and

scrollview.smoothScrollTo(0,0);

But nothing seems to work. Is there something wrong with the support library this time around?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try this

add android:descendantFocusability="blocksDescendants" to the LinearLayout inside NestedScrollView and this also

to scroll to top of NestedScrollView use this

NestedScrollView.scrollTo(0, 0);

Edit

Use fling() and smoothScrollTo togather

nestedScrollView.post(new Runnable() {
   @Override
   public void run() {
      nestedScrollView.fling(0);
      nestedScrollView.smoothScrollTo(0, 0);
   }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...