【问题标题】:How to maintain the position of ListView [duplicate]如何保持ListView的位置[重复]
【发布时间】:2011-02-13 01:52:44
【问题描述】:

可能重复:
Maintain/Save/Restore scroll position when returning to a ListView

当我转到另一个活动(通过启动另一个意图)然后返回(按返回按​​钮)时,如何保持我的 ListView 在活动中的位置?

谢谢。

【问题讨论】:

    标签: android


    【解决方案1】:
    @Override
    protected void onPause()
    {
        // Save scroll position
        SharedPreferences preferences = context.getSharedPreferences("SCROLL", 0);
        SharedPreferences.Editor editor = preferences.edit();
        int scroll = mListView.getScrollY();
        editor.put("ScrollValue", scroll);
        editor.commit();
    }
    
    @Override
    protected void onResume()
    {
        // Get the scroll position
        SharedPreferences preferences = context.getSharedPreferences("SCROLL", 0);
        int scroll = preferences.getInt("ScrollView", 0);
        mListView.scrollTo(0, scroll);
    }
    

    【讨论】:

    • 如果我使用'getSharedPreferences',是否意味着它会持续到磁盘上?即它会在我重新启动手机后保留该值?如果我不想要那个怎么办?当我重启手机时,它会从 0 开始?
    • 它会在关机后持续存在。如果您不希望这样,那么您可以将值写入成员变量并希望它持续存在。如果应用程序没有被垃圾收集,它应该。你只需要决定你想在哪一边犯错。
    • 我认为 onPause 中的第 6 行应该是 editor.putInt("ScrollValue", scroll);
    • mListView.getScrollY() 总是返回 0 。在这里阅读:stackoverflow.com/questions/2132370/…
    【解决方案2】:

    您应该使用onSaveInstanceState 来存储滚动位置,然后使用onCreateonRestoreInstanceState 来恢复它。

    http://developer.android.com...#onSaveInstanceState...

    【讨论】:

    • 我试过你的方法。 mListView.getSelectedItemPosition();给我一个 -1 和 mListView.getScrollY() 给我一个 0。当我进行测试时,我清楚地向下滚动到我的 ListView。知道为什么它不起作用吗?
    • 你做了什么来尝试我的方式?
    • 我做了:protected void onSaveInstanceState (Bundle outState) { super.onSaveInstanceState(outState); int 滚动 = mListView.getScrollY(); System.out.println("scrollY" + scroll); }
    • 啊,我不知道。我还没有使用列表视图。我刚开始使用 android。
    • 从onPause调用时是否有效?
    【解决方案3】:

    声明全局变量:

    int index = 0;
    ListView list;
    

    并在onCreate() 中引用您的ListView

    list = (ListView) findViewById(R.id.my_list);
    

    接下来,在onResume()的末尾添加这一行:

    list.setSelectionFromTop(index, 0);
    

    最后,在onPause 的末尾添加以下行:

    index = list.getFirstVisiblePosition();
    

    【讨论】:

    • 此外,我会将索引保存在 sharedPreferences 对象中,这样当应用程序超出范围时,它仍然保留索引
    • @mray190 这就是保存实例状态的目的。您可以在方向更改时将此值保存在捆绑包中,然后在 Activity 或 Fragment 级别重新创建视图时恢复滚动状态。
    【解决方案4】:

    请注意,使用 ListView.getScrollY() 不能很好地恢复滚动位置。

    Android: ListView.getScrollY() - does it work?

    指的是整个view的滚动量,所以几乎都是0。

    我也经常遇到这个值是 0。 ListView.getFirstVisiblePosition() 与 ListView.setSelection() 更可靠。

    【讨论】:

      【解决方案5】:

      做简单的......

      @Override
      protected void onPause()
      {
         index = listView.getFirstVisiblePosition();
         // store index using shared preferences   
      }
      

      和..

      @Override
      public void onResume() {
      super.onResume();
      // get index from shared preferences
      
      if(listView != null){
          if(listView.getCount() > index)
              listView.setSelectionFromTop(index, 0);
          else
              listView.setSelectionFromTop(0, 0);
      }
      

      【讨论】:

      • 对代码的解释可能有所帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 1970-01-01
      • 2012-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多