片段中的ListView不能保存滚动位置

编程入门 行业动态 更新时间:2024-10-18 08:35:31
本文介绍了片段中的ListView不能保存滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我如何保存的ListView 在的ListView 位于一个 ListFragment ?

How can I save the ListView's scroll position when the ListView is situated within a ListFragment?

推荐答案

最后,我解决了这个问题,所以我决定后给别人解决方案:

Finally I solved the problem, so I decided to post the solution for others:

在我的ListFragment子类我声明两个int变量来保存滚动条的位置

Within my ListFragment sub class I declared two int variables to hold the scroll position

public static class MyListFragment extends ListFragment { ...................... ...................... private int index = -1; private int top = 0; ......................

然后重写的onPause()和onResume()来保存和恢复的ListView 的滚动位置如下:

@Override public void onResume() { super.onResume(); ...................... ...................... setListAdapter(mAdapter); if(index!=-1){ this.getListView().setSelectionFromTop(index, top); } ...................... ...................... } @Override public void onPause() { super.onPause(); try{ index = this.getListView().getFirstVisiblePosition(); View v = this.getListView().getChildAt(0); top = (v == null) ? 0 : v.getTop(); } catch(Throwable t){ t.printStackTrace(); } ...................... ...................... }

这就是它!我希望这将帮助一些之一。 :)

That's it!! I hope this will help some one. :)

更多推荐

片段中的ListView不能保存滚动位置

本文发布于:2023-10-08 01:25:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471107.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:片段   位置   ListView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!