问题描述
我正在运行时创建选项卡.依赖于JSON数据类别计数,创建片段.将产品列出的列出取决于类别.我的问题是,当我滑动前面的标签片段时.
我有13个ta片,自动创建的ta片取决于类别计数.
从左到右滑动:
第一片段--->有数据
第二片--->有数据
3rd fragment --->有数据
第四片--->有数据
右至左滑动:
第三片--->无数据
第二片--->无数据
第1片--->无数据
我尝试使用
-
viewPager.SetOffScreenPageLimit(jsontab.size()); ---在活动中
-
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(); } }
推荐答案
首先,使用viewPager.setOffscreenPageLimit(jSonTab.size());不好.假设jSonTab.size()返回8 or more.然后8+ fragments将保留在您的内存中.这将增加您的模因消费,或者可能获得OurOfMemoryError.
默认情况下OffScreenPageLimit is 2.因此,当滑动下一个片段时.它onStop()被称为屏幕外片段的方法.
回到这些片段时.所有生命周期都重复. OnActivityCreated()在onCreateView()之前被调用.
因此,当您返回到这些片段时,有可能.您用来在您上设置数据的序列被弄乱了.
所以一次确认一次.它可能会帮助您.
问题描述
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
viewPager.setOffscreenPageLimit(jSonTab.size()); ---In Activity
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(); } }
推荐答案
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.