NestedScrollView的fullScroll(View.FOCUS_UP)无法正常工作 [英] NestedScrollView's fullScroll(View.FOCUS_UP) not working properly

查看:887
本文介绍了NestedScrollView的fullScroll(View.FOCUS_UP)无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NestedScrollView,其中装有垂直的LinearLayout,它本身具有一堆具有各种视图类型的子级:多个TextView,两个静态GridView,甚至还有一个FrameLayout,用于在所有这些视图下方显示一个Fragment.

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.

按下返回按钮时,如果用户滚动到某个点以下,而不是完成活动",则将调用"scrollToTop"方法:

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);
}

此功能适用于我在Play商店中的应用的先前版本.但是现在,在将我的应用程序更新为Android Oreo(并将支持库更新为26.0.2版)之后,它似乎没有从顶部滚动,而是从NestedScrollView的原始滚动位置开始滚动,并停止在原来的位置.因此,它看起来像是一个奇怪的结结巴巴.但是,在某些位置,它确实会滚动到顶部(尽管很少且不一致),而在其他位置,它实际上会滚动到底部,这是出于我不明白的原因.

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.

我一直在尝试查看焦点,但无济于事.例如,我读到静态GridViews可能会在滚动时中断焦点.我还尝试了各种向上滚动的方法,例如

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);

scrollview.smoothScrollTo(0,0);

但是似乎没有任何效果.这次支持库有什么问题吗?

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

推荐答案

尝试一下

android:descendantFocusability="blocksDescendants"添加到NestedScrollView内的LinearLayout中,这也是

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

要滚动到NestedScrollView的顶部,请使用此

to scroll to top of NestedScrollView use this

NestedScrollView.scrollTo(0, 0);

修改

使用 fling() smoothScrollTo 聚在一起

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

这篇关于NestedScrollView的fullScroll(View.FOCUS_UP)无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆