片段中的ListView不能保存滚动位置 [英] ListView within fragment cant save scroll position

查看:124
本文介绍了片段中的ListView不能保存滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何保存的ListView 的ListView 位于一个<$ C $内的滚动位置C> 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不能保存滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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