以编程方式滚动到 NestedScrollView 的顶部

Posted

技术标签:

【中文标题】以编程方式滚动到 NestedScrollView 的顶部【英文标题】:Programmatically scroll to the top of a NestedScrollView 【发布时间】:2015-09-09 22:31:34 【问题描述】:

有没有办法通过触发父级的滚动事件以编程方式滚动到NestedScrollView 的顶部? smoothScrollTo(x, y) 不会将滚动事件委托给 NestedScrollingParent

【问题讨论】:

再澄清一下你的问题,你的意思是说NestedScrollView什么时候在coordinator layout里面? 以编程方式滚动到任意点,检查***.com/questions/52083678/… 【参考方案1】:
NestedScrollView.scrollTo(0, 0);

【讨论】:

我告诉您使用NestedScrollView.scrollTo(0, 0); 滚动到NestedScrollView 的顶部。不客气。 这更普遍地适用于 ScrollView 或 NestedScrollView。 @Override public void onStart() super.onStart(); scroll.post(() -> scroll.scrollTo(0, 0)); 在 C# Xamarin 中工作,如 MyScroller.ScrollTo(0, 0);【参考方案2】:

我设法做到了(动画滚动):

CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
if (behavior != null) 
    behavior.onNestedFling(coordinatorLayout, appBarLayout, null, 0, 10000, true);

其中10000 是 y 方向的速度。

【讨论】:

如果您只想滚动隐藏 appbarlayout,就像我的情况一样,您可以使用 behavior.onNestedFling(coordinator, appbar, null, 0, -params.height, true); 来反转它 behavior.onNestedFling(coordinator, appbar, null, 0, params.height, true); 谢谢。关于如何滚动到嵌套滚动视图底部的任何想法? AppBarLayout 展开时,模拟嵌套的 fling 会折叠(很棒),但不会让 NestedScrollView 同时向下滚动... 另外,当 AppBarLayout 完全折叠时,使用负 y 速度值不会展开它。【参考方案3】:

另一种平滑滚动NestedScrollView 到一直向上或向下的方法是使用fullScroll(direct) 函数

nestedScrollView.fullScroll(View.FOCUS_DOWN);
nestedScrollView.fullScroll(View.FOCUS_UP);

【讨论】:

fullScroll(View.FOCUS_UP) 隐藏的应用栏不出来。任何想法 @Sabbir Dhangot 您是否将NestedScrollView 布局在应用栏下方? 我有一个问题,我可以看到 fullScroll 接收视图。如果我想滚动到某个 textview、editext 或 recyclerView,它会滚动到它吗?【参考方案4】:

没有什么对我有用,我真的不知道为什么会这样,但这是我的解决方案和问题。

将回收器视图添加到嵌套滚动视图时,当进入该活动时,它会在屏幕上显示回收器视图。为了一直向上滚动,我不得不使用这个:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.details_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setItemAnimator(new DefaultItemAnimator());
ProductDescriptionAdapter adapter = new ProductDescriptionAdapter(this);
adapter.setData(mProduct.getDetails());
recyclerView.setAdapter(adapter);
NestedScrollView scrollView = (NestedScrollView) findViewById(R.id.scroll);
scrollView.getParent().requestChildFocus(scrollView, scrollView);

【讨论】:

Wooooow,这是最好的,damet Garm 如果有人的问题是嵌套滚动视图内的recyclerview在进入活动时已经滚动到一半,请尝试将XML根目录下最顶层的元素设置为具有以下属性android:focusableInTouchMode="真” 此解决方案在 scrollView.scrollTo(0,0); 根本不起作用的情况下完美运行。 这工作:scrollView.getParent().requestChildFocus(scrollView, scrollView); 工作就像一个魅力。谢谢【参考方案5】:

我也遇到过类似的情况。在我的情况下,当我向下滚动到结尾时,应该会出现 FAB 按钮,并且当用户点击该 FAB 按钮时,应该会转到页面顶部。 为此,我添加了@SilentKnight 答案NestedScrollView.scrollTo(0, 0); 转到顶部,但这对于向上滚动的平滑动画来说还不够。 对于流畅的动画,我使用了@Sharj 答案,即NestedScrollView.fullScroll(View.FOCUS_UP); 但是我的 AppBar 在那里不可见,因此我必须按照 appBarLayout1.setExpanded(true) 扩展 AppBar。 所以使用这三个我可以顺利进入页面顶部。

nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.scrollTo(0,0);
appBarLayout1.setExpanded(true);

【讨论】:

appBarLayout1.setExpanded(true); - 为我工作!谢谢:) 谢谢!这是最正确的答案,尤其是在使用 appbarlayout 时【参考方案6】:

您可以使用它来平滑滚动。

nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.smoothScrollTo(0,0);

OR 用于正常滚动

nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.scrollTo(0,0);

【讨论】:

Gr8 工作。你节省了我的时间【参考方案7】:

有些时候NestedScrollView.scrollTo(0, 0);scrollview.pageScroll(View.FOCUS_UP); 不起作用

此时您需要同时使用 fling()smoothScrollTo()

示例代码

nestedScrollView.post 
   nestedScrollView.fling(0)
   nestedScrollView.smoothScrollTo(0, 0)

【讨论】:

【参考方案8】:

您可以轻松地伪造 NestedScrollView 级别的滚动。您基本上告诉 coordinatorLayout 您刚刚滚动了工具栏的高度。

    NestedScrollView nestedScrollView = (NestedScrollView) getActivity().findViewById(R.id.your_nested_scroll_view);

    int toolbarHeight = getActivity().findViewById(R.id.toolbar).getHeight();

    nestedScrollView.startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
    nestedScrollView.dispatchNestedPreScroll(0, toolbarHeight, null, null);
    nestedScrollView.dispatchNestedScroll(0, 0, 0, 0, new int[]0, -toolbarHeight);
    nestedScrollView.scrollTo(0, nestedScrollView.getBottom());

【讨论】:

唯一对我有用的答案!我需要在协调器布局中向上滚动(所以 nestedScrollView 和 collapsibleToolbar )。我使用了现有代码,但 toolbarHeight 更改为 Integer.MIN_VALUE 和 -toolbarHeight 更改为 Integer.MAX_VALUE (因为我需要相反的方向)。然后我调用 appBarLayout.setExpanded(true, true);谢谢!【参考方案9】:

我尝试了上述所有响应,似乎没有任何效果,但我只需要一个 postDelayed。

    scrollview.postDelayed(new Runnable() 
        @Override
        public void run() 
            listener.setAppBarExpanded(false, true); //appbar.setExpanded(expanded, animated);
            scrollview.fullScroll(View.FOCUS_DOWN);
        
    , 400);

【讨论】:

【参考方案10】:

添加两次。为我工作

nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.fullScroll(View.FOCUS_UP);

【讨论】:

【参考方案11】:

使用 View.scollTo(int x, int y) 代替滚动到顶部。

findViewById(your NestedScrollView resource id).scrollTo(0, 0);

【讨论】:

【参考方案12】:

当我使用 NestedScrollView 时,我遇到了问题,而不是 RecyclerView 。 这段代码对我有用: nestedScrollView!!.getParent().requestChildFocus(nestedScrollView,nestedScrollView);

    val recyclerViewSearch = activity?.findViewById<RecyclerView>(R.id.recycler)
    recyclerViewSearch.setAdapter(setAdapterSearch())

    val nestedScrollView = activity?.findViewById<NestedScrollView>(R.id.bottom_sheet_Search)
    nestedScrollView!!.getParent().requestChildFocus(nestedScrollView, nestedScrollView);

【讨论】:

【参考方案13】:

当 RecyclerView 位于 NestedScrollView 中时,这是我如何滚动到 RecyclerView 的一项

首先,获取item height,然后滚动到您想要的position

val itemHeight =
    binding.calendarRecyclerView.findViewHolderForAdapterPosition(0)!!.itemView.height
binding.nestedScrollView2.smoothScrollTo(0, position * itemHeight)

【讨论】:

以上是关于以编程方式滚动到 NestedScrollView 的顶部的主要内容,如果未能解决你的问题,请参考以下文章

无法滚动到 NestedScrollView 内的 RecyclerView 中的项目

如何使用 NestedScrollView 滚动以在 SliverAppBar 上使用堆栈圆形头像?

LinearLayout 和 NestedScrollView 内的 RecyclerView - 如何滚动到某个位置的项目顶部?

NestedScrollView/ ScrollView 在某些布局包含到 ScrollView 的子项时不滚动

NestedScrollView、LinearLayout 超出约束过度滚动

当nestedscrollview 中的recyclerview 滚动时,对讲焦点转到工具栏项