防止NestedScrollView自动滚动到顶部 [英] Prevent NestedScrollView scrolls to top automatically

查看:1427
本文介绍了防止NestedScrollView自动滚动到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的片段中的 NestedScrollView (不想使用RecycleListView)中有 ListView 。列表适配器在 onCreate 中初始化,并且在应用程序从其他片段回到该片段时不会重新创建。但是我的NestedScrollView然后滚动到顶部(我可以防止这种情况吗?)。如果没有 NestedScrollView ,我的ListView将保持在同一位置。

I have ListView inside NestedScrollView (do not want use RecycleListView) in my fragment. List adapter inits in onCreate and not recreated when app back to the fragment from other fragment. But my NestedScrollView then scrolls to the top (can I prevent this?). Without NestedScrollView my ListView stay at the same position.

推荐答案

在片段的 onPause()方法中保留列表状态(将其保存到捆绑包中),然后在 onResume()中读取并设置它code>。只需创建一些常量键,保存然后阅读:-)

You should retain the list status in onPause() method on the Fragment (save it to the Bundle) and then read and set it in onResume(). Just create some constant key, save then read :-)

您要保存的值是 getScrollY()很有可能。然后,您将要使用 scrollTo(int x,int y)还原它,其中x = 0和y = savedValue。

The value you will want to save is getScrollY() most probably. Then you will want to restore it with scrollTo(int x, int y) where x=0 and y=savedValue.

此外,您要做要使用 RecyclerView :-)这真的很酷,并且可以与 NestedScrollView一起很好地工作 CoordinatorLayout 。而且,一旦掌握了这些功能,它比 ListView 更容易,更快捷地实现。它也很轻巧。我曾经见过它可以救一只小狗免于火灾。 RecyclerView是达人! :-D

Also, you do want to use RecyclerView :-) It is really cool and works really well with NestedScrollView and CoordinatorLayout. Also, it is much easier and quicker to implement than ListView once you get the hang of it. It is also very lightweight. And I had once seen it save a puppy from a fire. RecyclerView is da man! :-D

这篇关于防止NestedScrollView自动滚动到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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