如何保存具有listview的Fragment状态

时间:2015-11-06 10:00:36

标签: android

我使用一些片段在选项卡式应用中显示一些列表,但是当我更改选项卡的位置时,片段中的listviews突然清除,并且项目删除了!我如何在每个碎片中保存每个listview的状态?请帮帮我,谢谢。这是我的onCreateViewof其中一个片段的代码。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_two, container, false);
    loadtoken();
    items = new ArrayList<item>();
    lv = (ListView)rootView.findViewById(R.id.listView);
    adapter_common adapter = new adapter_common(getContext(), items);
    swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout);
    swipeRefreshLayout.setColorSchemeColors(R.color.asabani_cat, R.color.asheghame_cat, R.color.asanyevazi_cat);
    swipeRefreshLayout.setOnRefreshListener(this);
    lv.setAdapter(adapter);
    lv.setOnScrollListener(new AbsListView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (firstVisibleItem == 0) {
                // check if we reached the top or bottom of the list
                View v = lv.getChildAt(0);
                int offset = (v == null) ? 0 : v.getBottom();
                if (offset == 0) {

                    return;
                }
            } else if (totalItemCount - visibleItemCount == firstVisibleItem) {
                View v = lv.getChildAt(totalItemCount - 1);
                int offset = (v == null) ? 0 : v.getTop();
                final int lastItem = firstVisibleItem + visibleItemCount;
                if (lastItem == totalItemCount) {
                    if (preLast != lastItem) {
                        preLast = lastItem;
                        f = f + 6;
                        j = 1;
                        new PostDataAsyncTask().execute();
                    }
                }
            }
        }
    });
    return rootView;
}

0 个答案:

没有答案