我在RunTime期间创建Tabs。在Json数据类别计数上创建片段,创建片段。列出的产品依赖于类别。我的问题是,当我滑动前面的Tab片段时,它会变空。
我有13个TabFragments会自动创建,具体取决于类别数。
从左到右滑动:
第1片段--->有数据
第二片段--->有数据
第3片段--->有数据
第4片段--->有数据
从右向左滑动:
第3片段--->没有数据
第二片段--->没有数据
第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();
}
}
答案 0 :(得分:0)
首先,使用viewPager.setOffscreenPageLimit(jSonTab.size());
并不好。假设jSonTab.size()
返回8 or more
。然后8+ fragments
将留在你的记忆中。这会增加您的记忆消耗量,也可能会OurOfMemoryError.
默认OffScreenPageLimit is 2
。所以当滑动下一个片段时。调用OffScreen片段的onStop()
方法。
当你回到那些碎片时。所有生命周期都在重复。在OnActivityCreated()
之前调用onCreateView()
。
所以有可能当你回到那些片段时。用于在Fragment
上设置数据的序列搞砸了。
所以确认一次。它可能对你有帮助。