本文介绍了片段中的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不能保存滚动位置
发布评论