How to stop reloading of a Tab Fragment while swiping

我与影子孤独终老i 提交于 2019-12-11 13:23:57

问题


I am creating Tabs during RunTime.Depends on the Json data category count, fragment is created.Even products are listed depends on Category. My issue is that while I swipe the previous Tab Fragment gets empty.

I am having 13 TabFragments which is created automatically depends on Category count.

Left to Right Swipe:

1st Fragment ---> Having data

2nd Fragment ---> Having data

3rd Fragment ---> Having Data

4th fragment ---> Having Data

Right To Left Swipe:

3rd Fragment ---> No Data

2nd Fragment ---> No Data

1st Fragment ---> No Data

I have tried to use

  1. viewPager.setOffscreenPageLimit(jSonTab.size()); ---In Activity

  2. setRetainInstance(true); ---In Fragment

But nothing worked...I mess up.Please help me.

My Code is here:

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

    }

回答1:


First of all, using viewPager.setOffscreenPageLimit(jSonTab.size()); is not good. Suppose jSonTab.size() returns 8 or more. Then 8+ fragments will remain in your memory. This will increase your memeory Consumption or may get OurOfMemoryError.

By Default OffScreenPageLimit is 2. so when swipe for next fragment. it onStop() method of OffScreen fragments is called.

When you get back to those fragments. all lifecycle is repeated. and OnActivityCreated() is called before onCreateView().

So it is possible that when you return back to those fragments. The sequence you are using to set up data on you Fragment is messed up.

So confirm it once. It may help you.



来源:https://stackoverflow.com/questions/36661183/how-to-stop-reloading-of-a-tab-fragment-while-swiping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!