android – 如何避免重新创建片段到ViewPager?

前端之家收集整理的这篇文章主要介绍了android – 如何避免重新创建片段到ViewPager?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用一个包含5页的FragmentStatePagerAdapter的ViewPager. @H_301_2@在第一页中,我有一个带有项目的ListView.当我在ViewPager的最后一页更改,然后我回到第一页时,我的ListView为空,因为已重新创建了适配器.

@H_301_2@Android总是调用下一个方法:onAttach(),onCreate(),onCreateView(),onViewCreated(),onActivityCreated(),onStart()和onResume().

@H_301_2@我想不要使用ViewPager.setOffscreenPageLimit()方法来避免我的页面被销毁.还有其他解决方案吗?

@H_301_2@我想要的行为与Facebook相同.

@H_301_2@我做错了什么?如何将数据保存在ListView中?

@H_301_2@我的FragmentStatePageAdapter的代码

public class AdapterMain extends FragmentStatePagerAdapter {
    List<ChicfyFragment> fragments = new ArrayList<ChicfyFragment>();

    public AdapterMain(FragmentManager fm) {
        super(fm);

        fragments.add(new FragmentShop());
        fragments.add(new FragmentSearch());
        fragments.add(new FragmentSell());
        fragments.add(new FragmentNotification());
        fragments.add(new FragmentProfile());
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }
}
@H_301_2@我的ViewPager第一页的代码

public class FragmentShop extends ChicfyFragment implements SwipeRefreshLayout.OnRefreshListener {

    ListView mListView;
    AdapterProductList mAdapter;
    SwipeRefreshLayout mSwipeRefreshLayout;

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

        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh);
        mListView = (ListView) view.findViewById(R.id.fragment_main_shop_listview);

        return view;
    }

    @Override
    public void onViewCreated(View view,Bundle savedInstanceState) {
        super.onViewCreated(view,savedInstanceState);

        // setup adapter
        mAdapter = new AdapterProductList((FragmentActivity) getActivity(),new ArrayList());

        // setup listview
        mListView.setAdapter(mAdapter);
        mListView.setDivider(null);

        // setup swipe
        mSwipeRefreshLayout.setColorScheme(R.color.red,R.color.white,R.color.blue,R.color.white);
        mSwipeRefreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onRefresh() {
        new AsyncTask<Void,Void,Void>() {
            List<String> list = new ArrayList<String>();

            @Override
            protected Void doInBackground(Void... voids) {
                try {
                    Thread.sleep(1000);
                    for(int i=0; i<10; i++) list.add("" + i);
                } catch (Exception e) {}
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);

                mAdapter.clear();
                mAdapter.addAll(list);
                mAdapter.notifyDataSetChanged();

                mSwipeRefreshLayout.setRefreshing(false);
            }
        }.execute();
    }
}

解决方法

您可以将此行用于X片段的计数 @H_301_2@mViewPager.setOffscreenPageLimit(X);

原文链接:https://www.f2er.com/android/309872.html

猜你在找的Android相关文章