简体   繁体   English

当多个加载程序与加载程序管理器一起使用时,android无法在方向更改时保持正确的加载程序状态

[英]android not maintaining the correct loader state on orientation change when multiple loaders are used with loader manager

I am using 4 loaders in my application to load data into a listview. 我在应用程序中使用4个加载程序将数据加载到列表视图中。 When I change the orientation the loader manager always tries to load the first loader. 当我更改方向时,加载程序管理器始终尝试加载第一个加载程序。 To solve this issue I am restarting the loader in "onResume" of the application. 为了解决此问题,我在应用程序的“ onResume”中重新启动加载程序。 Now the listview is populated with the data but the position of the list before orientation change in not maintained. 现在,在列表视图中填充了数据,但未保持方向更改之前列表的位置。 Is there any way to maintain the loader state on orientation change ? 有什么办法可以在方向改变时保持装载机状态?

If you can't prevent the re-loading of the list, you can at least restore the position: 如果您不能阻止重新加载列表,则至少可以恢复该位置:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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