ListView onRestoreInstanceState

时间:2014-03-13 11:54:52

标签: android listview android-adapter

我有一个带有HListView(HL)的ListView(VL)作为项目(书架样式)。我正在保存VL状态并且它正在工作,但现在我想要的是保存它的项目状态,因为我希望HL在再次渲染时恢复它们的状态。
所以我在 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]);
    }
    ...
}



当列表项再次呈现时,它们不处于以前的状态......我在这里做错了什么? 谢谢你的时间。

2 个答案:

答案 0 :(得分:0)

我发现了问题,我必须切换&#34;保存编码&#34;使用&#34;恢复代码&#34;,像这样:

@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();
   }
});   
...

}

它现在正在工作:)

答案 1 :(得分:0)

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();
   }