良好的解决方案,以保留列表视图项目时,用户旋转手机,并保持在ArrayAdapter中的所有数据视图、良好、解决方案、项目

2023-09-12 23:55:19 作者:啭捔╮笕啈冨

我使用的是碎片与ListView控件。我填这个列表视图相关ArrayAdapter,通过自定义加载程序(来自互联网)接收到的数据。自定义ArrayAdapter支持无限滚动(分页)。

I'm using Fragment with listView. I fill ArrayAdapter associated with this listview, by data received in custom Loader(from internet). Custom ArrayAdapter supports infinite scrolling(paging).

什么是存放物品的ArrayAdapter当用户旋转设备,并保持在ListView的滚动位置?最好的办法

What is the best way to store items in ArrayAdapter when user rotate device and keep scroll position in ListView?

我正在考虑创建非可视片段,ArrayAdapter,并使用setRetainInstance方法保存的值。

I'm thinking about creation of non-visual Fragment with ArrayAdapter, and using setRetainInstance method to save values.

有更好的解决方案有什么建议?

Any suggestions for better solution?

推荐答案

要与Android框架和片段生命周期的工作,你应该实现在片段中的的onSaveInstanceState 方法。为简单起见,我假定你有字符串值,你可以得到的一个数组(我一般延长ArrayAdaptor封装视图建设,提供了一个方便的方法来访问整个基础数据集):

To work with the Android framework and Fragment lifecycle you should implement the onSaveInstanceState method in your Fragment. For simplicity I've assumed that you have an array of String values that you can get to (I generally extend ArrayAdaptor to encapsulate view construction and to provide a convenience method to access the entire underlying dataset):

public void onSaveInstanceState(Bundle savedState) {

    super.onSaveInstanceState(savedState);

    // Note: getValues() is a method in your ArrayAdaptor subclass
    String[] values = mAdapter.getValues(); 
    savedState.putStringArray("myKey", values);

}

您可以再检索数据在onCreate方法(或onCreateView或onActivityCreated - 看到的片段的JavaDoc )是这样的:

You can then retrieve the data in your onCreate method (or onCreateView or onActivityCreated - see the Fragment JavaDoc) like this:

public void onCreate (Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        String[] values = savedInstanceState.getStringArray("myKey");
        if (values != null) {
           mAdaptor = new MyAdaptor(values);
        }
    }

    ...

}

这可以确保所有的生命周期事件会得到妥善处理,不会丢失数据,包括设备旋转和用户切换到其他应用程序。不使用的onSaveInstanceState和使用内存的危险是Android系统回收内存的危险。保存的状态不会受到影响这一点,但使用实例变量或隐藏的碎片会导致数据丢失。

This ensures that all lifecycle events will be handled properly, without loss of data, including device rotation and the user switching to other applications. The danger of not using onSaveInstanceState and using memory is the danger of Android reclaiming that memory. Saved state would not be affected by this but using instance variables or hidden fragments would result in loss of data.

如果savedStateInstance为null,则没有状态恢复。

If savedStateInstance is null then there is no state to restore.

如果(值!= NULL)仅仅是为了防止没有数组保存的可能性,但如果你code您ArrayAdaptor处理空数据集,你不需要这个。

The if (values != null) is simply to guard against the possibility that no array was saved, but if you code your ArrayAdaptor to handle a null data set you won't need this.

的最终解决方案,如果你的行是自己的一个类的实例,并没有单一的数据项,是实现该类中的那个Parcelable接口,那么你可以使用 savedState.putParcelableArray(的myKey myArray的)。你会惊奇地发现它是多么有用知道如何实现Parcelable - 它可以让你周围的传递你的类里的意图,并允许你写干净多了code

The ultimate solution, if your rows are instances of one of your own classes and not single data items, is to implement the Parcelable interface on that class, then you can use savedState.putParcelableArray("myKey", myArray). You'd be surprised how useful it is to know how to implement Parcelable - it allows you to pass your classes around inside intents and allows you to write much cleaner code.

 
精彩推荐
图片推荐