java [Android]返回ListView 2时保存滚动位置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java [Android]返回ListView 2时保存滚动位置相关的知识,希望对你有一定的参考价值。

Parcelable state;

@Override
public void onPause() {    
    // Save ListView state @ onPause
    Log.d(TAG, "saving listview state @ onPause");
    state = listView.onSaveInstanceState();
    super.onPause();
}
...

@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    // Set new items
    listView.setAdapter(adapter);
    ...
    // Restore previous state (including selected item index and scroll position)
    if(state != null) {
        Log.d(TAG, "trying to restore listview state..");
        listView.onRestoreInstanceState(state);
    }
}

/*
I think this is the best answer as this method restores the exact scroll position and not only the first visible item. –  max.weller Jul 2 '12 at 10:52 

Great answer but won't work when dynamically loading content and you want to save state in the onPause() method. –  Gio Sep 18 '12 at 12:47

great solution. Just one Problem. If the items are big and you scroll, but the first item is still visible the list jumps back to top. if you scroll to the second item or anywhere else everything works –  passsy Jul 26 '13 at 3:34
*/

以上是关于java [Android]返回ListView 2时保存滚动位置的主要内容,如果未能解决你的问题,请参考以下文章

ArrayList 对象正在返回 java android 中 ListView 上的一行

Android ListView显示访问WebServices返回的JSON结果

Android ListView 返回 Null

Android 自定义 ListView onClickListener 返回 null

返回活动后,Android ListView 不显示传入消息

Android ListView.count vs ListView.size