当用户通过按“后退"按钮返回活动时,如何保持列表视图的位置? [英] How can I maintain the position of listview when user comes back to the activity by pressing back button?
问题描述
我有一个 listview
,当我转到另一个活动(通过启动另一个意图)然后返回时如何保持我的 ListView
在我的活动中的位置(按返回按钮)?当用户单击 listview
中的任何项目时,另一个活动开始,并且在该活动中,当按下上一个 listview
活动时,我有一个后退按钮,其状态恢复为点击了吗?我已经搜索了,但是没有找到有用的东西,并且有什么办法可以在清单中做到这一点,因为我在 listview
活动清单中使用了以下代码来恢复其状态,并且我知道它不是恢复状态的好选择方向改变后,但对我而言很有用.
I have a listview
and How can I maintain the position of my ListView
in my activity when I go to another activity (by launching another intent) and then come back (press the back button)? when user click on any item in the listview
another activity started and in that activity I have a back button when it pressed the previous listview
activity restores its state with item position that was clicked ? I have searched but nothing useful is found and Is there any way to do that in manifest because I used below code in the listview
activity manifest to restore its state and I know its not a good choice to restore state after orientation changes but in my case that was useful.
android:configChanges="orientation|screenSize"
推荐答案
kotlin方式
val index: Int = recyclerView.scrollState
recyclerView.scrollToPosition(index)
这篇关于当用户通过按“后退"按钮返回活动时,如何保持列表视图的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!