【问题标题】:How to stop reloading of a Tab Fragment while swiping如何在滑动时停止重新加载选项卡片段
【发布时间】:2016-04-16 07:00:34
【问题描述】:

我在运行时创建选项卡。取决于 Json 数据类别计数,创建片段。甚至列出的产品取决于类别。我的问题是,当我滑动之前的 Tab 片段时,会变空。

我有 13 个 TabFragment,它是根据类别计数自动创建的。

从左向右滑动:

第一个片段 ---> 有数据

第二个片段 ---> 有数据

第三个片段--->有数据

第 4 个片段 ---> 有数据

从右向左滑动:

第三个片段 ---> 没有数据

第二个片段 ---> 没有数据

第一个片段 ---> 没有数据

我试过用

  1. viewPager.setOffscreenPageLimit(jSonTab.size()); ---在活动中

  2. setRetainInstance(true); ---在片段中

但是没有任何效果...我搞砸了。请帮助我。

我的代码在这里:

 public class TabFragmentSearch extends Fragment {

        GridLayoutManager mLayoutManager;
        Context context;
        RecyclerView recyclerView;
        String currentTab;
        boolean swipeCheck;
        Config config;
        DottedProgressBar progressBar;
        TextView noData;
        CartRes filterBean = new CartRes();
        CartRes filter = new CartRes();
        CommonUtil commonUtil;
        List<CartRes> cartRestaurant;
        private List<CartRes> RestaurantDataSet;
        private RecyclerView.Adapter adapter;

        public static TabFragmentSearch newInstance(int categoryId, String categoryName) {

            Bundle args = new Bundle();
            args.putInt(CommonUtil.CATEGORY_ID, categoryId);
            args.putString(CommonUtil.CATEGORY_NAME, categoryName);

            TabFragmentSearch fragment = new TabFragmentSearch();
            fragment.setArguments(args);
            return fragment;
        }


        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setRetainInstance(true);
            config = new Config(getActivity());
            getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
            CommonUtil.pref = getActivity().getSharedPreferences(CommonUtil.MyPREFERENCES, Context.MODE_PRIVATE);
        }

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

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            config = new Config(getActivity());
            context = getActivity();
            CommonUtil.pref = context.getSharedPreferences(CommonUtil.MyPREFERENCES, Context.MODE_PRIVATE);

            commonUtil.dbUtil = new DbUtil(context);
            commonUtil.dbUtil.open();
            commonUtil.dbHelper = new DbHelper(context);

            currentTab = CommonUtil.pref.getString("FIRST_TAB", "tab1");

            recyclerView = (RecyclerView) view.findViewById(R.id.fragment_list_rv);
            edtSearch = (SearchView) view.findViewById(R.id.edtSearch);
            noData = (TextView) view.findViewById(R.id.noData);
            noData.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);

            mLayoutManager = new GridLayoutManager(context, 2);
            recyclerView.setLayoutManager(mLayoutManager);
            recyclerView.setHasFixedSize(true);

            RestaurantDataSet = new ArrayList<>();


            swipeCheck = false;

            List<CartRes> RestaurantFilter = new ArrayList<>();
            cartRestaurant = new ArrayList<>();
            if (Config.startFilterClicked == true) {     /*---Check whether the ToolBar Filter icon is clicked or not*/

                SharedPreferences.Editor editor = CommonUtil.pref.edit();
                editor.putBoolean("startFilter", Config.startFilterClicked);
                editor.commit();
                ArrayList<String> L2H;

                List<CartRes> RestaurantFilter_CF = new ArrayList<>();
                String BeanCatName = getArguments().getString(CommonUtil.CATEGORY_NAME);
                RestaurantFilter_CF = MainActivity.cartRestaurant;
                System.out.println("Android Studio" + " Condition Check" + ChatListAdapter.filterCategory + "  " + BeanCatName);

                if (Config.L2HFilterClicked == "L2H") {
                    commonUtil.dbUtil.open();
                    commonUtil.dbUtil.resetAddNew();
                    commonUtil.dbUtil.open();

                    Cursor L2HCur = commonUtil.dbUtil.getLowToHigh();
                    System.out.println("Marysudha  " + " Cursor Test" + "" + L2HCur.getCount());
                    System.out.println("" + L2HCur.getCount());
                    if (L2HCur != null && L2HCur.moveToFirst()) {


                        cartRestaurant.add(new CartRes(L2HCur.getInt(L2HCur.getColumnIndex(DbHelper.JSON_CATEGORY_ID)), L2HCur.getString(L2HCur.getColumnIndex(DbHelper.JSON_CATEGORY_NAME)),
                                L2HCur.getInt(L2HCur.getColumnIndex(DbHelper.JSON_PRODUCT_ID)),
                                L2HCur.getString(L2HCur.getColumnIndex(DbHelper.JSON_PRODUCT_NAME)),
                                L2HCur.getInt(L2HCur.getColumnIndex(DbHelper.JSON_SALES_PRICE)), L2HCur.getString(L2HCur.getColumnIndex(DbHelper.JSON_IMAGE_ID)),
                                L2HCur.getString(L2HCur.getColumnIndex(DbHelper.JSON_SHOP_ID)), L2HCur.getString(L2HCur.getColumnIndex(DbHelper.JSON_DELIVERY_TIME)),
                                L2HCur.getInt(L2HCur.getColumnIndex(DbHelper.JSON_LIKECOUNT)), L2HCur.getString(L2HCur.getColumnIndex(DbHelper.JSON_VOUCHER_ID)),
                                L2HCur.getString(L2HCur.getColumnIndex(DbHelper.JSON_VOUCHER_OFFER))));

                        adapter = new CardAdapter(cartRestaurant, context);
                        recyclerView.setAdapter(adapter);
                       adapter.notifyDataSetChanged();
                    }
                } else if ((ChatListAdapter.filterCategory != null) && ChatListAdapter.filterCategory.equalsIgnoreCase(BeanCatName)) {

                    CartRes cartRes_CF2 = new CartRes();
                    CartRes cartRes_CF = new CartRes();

                    for (int i = 0; i < MainActivity.cartRestaurant.size(); i++) {

                        cartRes_CF = MainActivity.cartRestaurant.get(i);
                        cartRes_CF2 = new CartRes(cartRes_CF.JSON_CATEGORY_ID, cartRes_CF.JSON_CATEGORY_NAME, cartRes_CF.JSON_PRODUCT_ID, cartRes_CF.JSON_PRODUCT_NAME,
                                cartRes_CF.JSON_SALES_PRICE, cartRes_CF.JSON_IMAGE_ID, cartRes_CF.JSON_SHOP_ID, cartRes_CF.JSON_DELIVERY_TIME, cartRes_CF.JSON_LIKECOUNT,
                                cartRes_CF.JSON_VOUCHER_ID, cartRes_CF.JSON_VOUCHER_OFFER);
                        RestaurantFilter_CF.add(cartRes_CF2);

                        adapter = new CardAdapter(RestaurantFilter_CF, context);
                        recyclerView.setAdapter(adapter);
                   adapter.notifyDataSetChanged();
                    }
                }
            } else {

                Config.startFilterClicked = false;
                SharedPreferences.Editor editor = CommonUtil.pref.edit();
                editor.putBoolean("startFilter", Config.startFilterClicked);
                editor.commit();

                int BeanCatId = getArguments().getInt(CommonUtil.CATEGORY_ID);

                Cursor curCAT_PDT = commonUtil.dbUtil.getCAT_PDT(String.valueOf(BeanCatId));
                System.out.println("Marysudha  " + " CategoryPdt" + "" + curCAT_PDT.getCount() + " Bean Id" + BeanCatId);
                System.out.println("" + curCAT_PDT.getCount());
                if (curCAT_PDT != null && curCAT_PDT.moveToFirst()) {
                    curCAT_PDT.moveToFirst();
                    while (curCAT_PDT.isAfterLast() == false) {

                        cartRestaurant.add(new CartRes(curCAT_PDT.getInt(curCAT_PDT.getColumnIndex(DbHelper.JSON_CATEGORY_ID)), curCAT_PDT.getString(curCAT_PDT.getColumnIndex(DbHelper.JSON_CATEGORY_NAME)),
                                curCAT_PDT.getInt(curCAT_PDT.getColumnIndex(DbHelper.JSON_PRODUCT_ID)),
                                curCAT_PDT.getString(curCAT_PDT.getColumnIndex(DbHelper.JSON_PRODUCT_NAME)),
                                curCAT_PDT.getInt(curCAT_PDT.getColumnIndex(DbHelper.JSON_SALES_PRICE)), curCAT_PDT.getString(curCAT_PDT.getColumnIndex(DbHelper.JSON_IMAGE_ID)),
                                curCAT_PDT.getString(curCAT_PDT.getColumnIndex(DbHelper.JSON_SHOP_ID)), curCAT_PDT.getString(curCAT_PDT.getColumnIndex(DbHelper.JSON_DELIVERY_TIME)),
                                curCAT_PDT.getInt(curCAT_PDT.getColumnIndex(DbHelper.JSON_LIKECOUNT)), curCAT_PDT.getString(curCAT_PDT.getColumnIndex(DbHelper.JSON_VOUCHER_ID)),
                                curCAT_PDT.getString(curCAT_PDT.getColumnIndex(DbHelper.JSON_VOUCHER_OFFER))));
                        curCAT_PDT.moveToNext();
                    }
                }

                adapter = new CardAdapter(cartRestaurant, context);
                recyclerView.setAdapter(adapter);
              adapter.notifyDataSetChanged();
            }

    }

【问题讨论】:

    标签: android tabs fragment swipe


    【解决方案1】:

    首先,使用viewPager.setOffscreenPageLimit(jSonTab.size()); 不好。假设jSonTab.size() 返回8 or more。然后8+ fragments 将留在您的记忆中。这会增加你的内存消耗或者可能会得到OurOfMemoryError.

    默认为OffScreenPageLimit is 2。所以当刷下一个片段时。它调用了 OffScreen 片段的 onStop() 方法。

    当你回到那些片段时。所有的生命周期都是重复的。并且OnActivityCreated()onCreateView() 之前被调用。

    所以有可能当你回到那些片段时。你用来设置数据的顺序Fragment 搞砸了。

    所以确认一次。它可能会帮助你。

    【讨论】:

      猜你喜欢
      • 2016-04-22
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 2016-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多