简体   繁体   中英

ListView onRestoreInstanceState

I've a ListView (VL) with HListView (HL) as items (bookshelf style). I'm saving the VL state and it's working, but now what I want is to save it's items state because I want the HLs to restore their state when they are rendered again.
So I'm doing something like this in my BookShelfBaseAdapter :

private List<Book> bookList;
private Parcelable[] bookListStates;

@Override
public View getView(int position, View view, ViewGroup parent) {
    final int fPosition = position;
    ...

    holder.booksHListView.setOnScrollListener(new ContentListOnScrollListener(holder) {
       @Override
       public void onScrollStateChanged(AbsHListView view, int scrollState) {}

       @Override
       public void onScroll(AbsHListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
           bookListStates[fPosition] = holder.booksHListView.onSaveInstanceState();
       }
    });

    if (bookListStates[fPosition] != null) {
        holder.booksHListView.onRestoreInstanceState(bookListStates[fPosition]);
    }
    ...
}



When the list items are rendered again they aren't in the state they were before... What I'm I doing wrong here?
Thanks for your time.

I've found the problem, I have to switch the "saving coding" with the "restore code", like this:

@Override
public View getView(int position, View view, ViewGroup parent) {
final int fPosition = position;
...

if (bookListStates[fPosition] != null) {
    holder.booksHListView.onRestoreInstanceState(bookListStates[fPosition]);
}

holder.booksHListView.setOnScrollListener(new ContentListOnScrollListener(holder) {
   @Override
   public void onScrollStateChanged(AbsHListView view, int scrollState) {}

   @Override
   public void onScroll(AbsHListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
       bookListStates[fPosition] = holder.booksHListView.onSaveInstanceState();
   }
});   
...

}

It's working now :)

if (bookListStates[fPosition] != null) {
    holder.booksHListView.onRestoreInstanceState(bookListStates[fPosition]);
}

holder.booksHListView.setOnScrollListener(new ContentListOnScrollListener(holder) {
   @Override
   public void onScrollStateChanged(AbsHListView view, int scrollState) {}

   @Override
   public void onScroll(AbsHListView view, int firstVisibleItem, int visibleItemCount, int      totalItemCount) {
       bookListStates[fPosition] = holder.booksHListView.onSaveInstanceState();
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM