Question

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.

Was it helpful?

Solution

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 :)

OTHER TIPS

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();
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top