NestedScrollView
顶部并触发父级滚动事件的方法?smoothScrollTo(x, y)
不会委托滚动事件给NestedScrollingParent
。NestedScrollView
顶部并触发父级滚动事件的方法?smoothScrollTo(x, y)
不会委托滚动事件给NestedScrollingParent
。NestedScrollView.scrollTo(0, 0);
NestedScrollView.scrollTo(0, 0);
将NestedScrollView
滚动到顶部。不客气。 - SilentKnightMyScroller.ScrollTo(0, 0);
这样的语句。 - cherry我成功地做到了(动画滚动):
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方向上的速度。
behavior.onNestedFling(coordinator, appbar, null, 0,-params.height, true);
。要取消隐藏,请执行behavior.onNestedFling(coordinator, appbar, null, 0, params.height, true);
。 - Kosh另一种平滑滚动 NestedScrollView
到顶部或底部的方法是使用 fullScroll(direct)
函数。
nestedScrollView.fullScroll(View.FOCUS_DOWN);
nestedScrollView.fullScroll(View.FOCUS_UP);
NestedScrollView
布局放在应用栏下面? - MeLean对我而言,一切都行不通,我真的不知道为什么这个方法管用,但这是我的解决方案和问题。
当将RecyclerView添加到嵌套的ScrollView中时,在进入该活动时会在屏幕上显示RecyclerView。为了滚动到顶部,我必须使用以下代码:
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);
scrollView.getParent().requestChildFocus(scrollView, scrollView);
。 - Rajeev Sahu我也遇到过类似的情况。在我的情况下,当我向下滚动到底部时,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);
有时候 NestedScrollView.scrollTo(0, 0);
和 scrollview.pageScroll(View.FOCUS_UP);
不起作用。
这时候需要同时使用 fling()
和 smoothScrollTo()
。
示例代码
nestedScrollView.post {
nestedScrollView.fling(0)
nestedScrollView.smoothScrollTo(0, 0)
}
您可以使用此功能实现平滑滚动。
nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.smoothScrollTo(0,0);
普通滚动的OR
nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.scrollTo(0,0);
您可以轻松地在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());
我尝试了以上所有方法,似乎都不起作用,但我只需要一个postDelayed。
scrollview.postDelayed(new Runnable() {
@Override
public void run() {
listener.setAppBarExpanded(false, true); //appbar.setExpanded(expanded, animated);
scrollview.fullScroll(View.FOCUS_DOWN);
}
}, 400);
将这行代码重复两次。 对我有用。
nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.fullScroll(View.FOCUS_UP);