0

我在我的应用程序中使用 4 个加载器将数据加载到列表视图中。当我改变方向时,加载器管理器总是尝试加载第一个加载器。为了解决这个问题,我在应用程序的“onResume”中重新启动加载程序。现在列表视图填充了数据,但未维护方向更改之前列表的位置。有没有办法在方向改变时保持装载机状态?

4

1 回答 1

1

如果你不能阻止列表的重新加载,你至少可以恢复位置:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (null != savedInstanceState) {
        mViewPosition = savedInstanceState.getInt(KEY_LIST_CUR_POS);
        mViewOffset = savedInstanceState.getInt(KEY_LIST_VIEW_OFFSET);
    } 
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.status_list, container, false);
    return(view);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // Restore list position.
    getListView().setSelectionFromTop(mViewPosition, mViewOffset);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    // Save current view in list.
    outState.putInt(KEY_LIST_CUR_POS, mViewPosition);
    outState.putInt(KEY_LIST_VIEW_OFFSET, mViewOffset);
}

@Override
public void onPause() {
    super.onPause();

    mViewPosition = getListView().getFirstVisiblePosition();
    View v = getListView().getChildAt(0);
    mViewOffset = (v == null) ? 0 : v.getTop();
}
于 2012-08-15T15:36:55.280 回答