如何在刷卡时停止重新加载Tab片段 [英] How to stop reloading of a Tab Fragment while swiping

查看:79
本文介绍了如何在刷卡时停止重新加载Tab片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行时创建选项卡,根据Json数据类别计数创建片段,甚至列出的产品也取决于类别.我的问题是,当我滑动以前的选项卡片段"时,它会变空.

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.

我有13个TabFragments,它们是根据类别计数自动创建的.

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

从左向右滑动:

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

1st Fragment ---> Having data

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

2nd Fragment ---> Having data

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

3rd Fragment ---> Having Data

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

4th fragment ---> Having Data

从右向左滑动:

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

3rd Fragment ---> No Data

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

2nd Fragment ---> No Data

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

1st Fragment ---> No Data

我尝试使用

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

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

但是什么都没有...我搞砸了.请帮帮我.

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

我的代码在这里:

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

    }

推荐答案

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

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.

默认为OffScreenPageLimit is 2.因此,当滑动下一个片段时.调用OffScreen片段的onStop()方法.

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

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

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

因此,当您返回到那些片段时,这是可能的.您用来在Fragment上设置数据的顺序被弄乱了.

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.

这篇关于如何在刷卡时停止重新加载Tab片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆