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

时间:2015-06-23 22:10:26

标签: android android-layout android-nestedscrollview

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

13 个答案:

答案 0 :(得分:83)

NestedScrollView.scrollTo(0, 0);

答案 1 :(得分:41)

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

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方向的速度。

答案 2 :(得分:29)

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

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

答案 3 :(得分:19)

对我来说没有任何作用,我也不知道为什么会这样,但这是我的解决方案和问题。

将循环播放器视图添加到嵌套滚动视图时,它会在到达该活动时在屏幕上显示回收器视图。为了一直向上滚动,我不得不使用它:

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

答案 4 :(得分:12)

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

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

答案 5 :(得分: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());

答案 6 :(得分:5)

您可以使用它进行平滑滚动。

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

或正常滚动

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

答案 7 :(得分:2)

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

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

示例代码

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

答案 8 :(得分:1)

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

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

答案 9 :(得分:1)

我尝试了上述所有答复,但似乎没有什么用,但我只需要一个postDelayed。

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

答案 10 :(得分:1)

添加该行两次。为我工作

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

答案 11 :(得分:0)

与RecyclerView相比,我使用NestedScrollView时遇到问题。 这段代码对我有用: 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);

答案 12 :(得分:0)

这是当RecyclerView位于NestedScrollView内时如何滚动到RecyclerView的项目

首先,获取一个item height,然后滚动到所需的position

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