luk*_*994 68 android android-layout android-nestedscrollview
有没有办法以编程方式滚动到顶部NestedScrollView还通过触发父级的滚动事件?smoothScrollTo(x, y)不会将滚动事件委托给NestedScrollingParent.
Sil*_*ght 91
NestedScrollView.scrollTo(0, 0);
Run Code Online (Sandbox Code Playgroud)
luk*_*994 42
我设法做到了(动画滚动):
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);
}
Run Code Online (Sandbox Code Playgroud)
10000y方向的速度在哪里.
Sha*_*arj 30
平滑滚动NestedScrollView到上下的另一种方法是使用fullScroll(direct)函数
nestedScrollView.fullScroll(View.FOCUS_DOWN);
nestedScrollView.fullScroll(View.FOCUS_UP);
Run Code Online (Sandbox Code Playgroud)
Vul*_*sin 21
没有什么对我有用,我不知道为什么会这样,但这是我的解决方案和问题.
将循环器视图添加到嵌套滚动视图时,它会在进入该活动时在屏幕上显示回收器视图.为了一直向上滚动,我不得不使用这个:
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);
Run Code Online (Sandbox Code Playgroud)
小智 16
我也遇到了类似的情况.在我的情况下,当我向下滚动到结尾时,应该出现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);
Run Code Online (Sandbox Code Playgroud)
您可以轻松伪造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());
Run Code Online (Sandbox Code Playgroud)
您可以使用它进行平滑滚动。
nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.smoothScrollTo(0,0);
Run Code Online (Sandbox Code Playgroud)
或正常滚动
nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.scrollTo(0,0);
Run Code Online (Sandbox Code Playgroud)
有的时候NestedScrollView.scrollTo(0, 0);并scrollview.pageScroll(View.FOCUS_UP);不能正常工作
在那你需要使用 fling()和smoothScrollTo()一起
示例代码
nestedScrollView.post {
nestedScrollView.fling(0)
nestedScrollView.smoothScrollTo(0, 0)
}
Run Code Online (Sandbox Code Playgroud)
我尝试了上述所有响应,但似乎没什么用,但我只需要一个postDelayed。
scrollview.postDelayed(new Runnable() {
@Override
public void run() {
listener.setAppBarExpanded(false, true); //appbar.setExpanded(expanded, animated);
scrollview.fullScroll(View.FOCUS_DOWN);
}
}, 400);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
50859 次 |
| 最近记录: |