我在视图分页器中有7个片段,我在所有分页器项目中使用相同的片段ProductListingFragment
。并且public void setUserVisibleHint(boolean isVisibleToUser)
上的填充数据被调用。现在第一个片段工作正常,但是当我移动到第二个片段时,它不会填充适配器中的数据,它会在第三个片段中显示第二个片段的数据。
以下是我在Viewpager中添加Child片段的代码:
viewPager = (ViewPager) view.findViewById(R.id.tiresViewpager);
viewPager.setOffscreenPageLimit(0);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
pagePosition = position;
viewPager.setCurrentItem(pagePosition);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
return view;
}
@Override
public void getVolleyRequestResponse(VolleyRequestResponse volleyResponse) {
switch (volleyResponse.type) {
case 0:
try {
JSONObject objJson = new JSONObject(volleyResponse.output);
if (objJson.optInt(Param.KEY_STATUS_CODE) == Param.SUCCESS_CODE) {
adapter = new ViewPagerAdapter(getChildFragmentManager());
JSONArray jsonArray = objJson.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject categoryObject = jsonArray.getJSONObject(i);
adapter.addFragment(new ProductListingFragment(), categoryObject.getString("name"));
categoryId.add(categoryObject.getString("id"));
}
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Hear是在片段
中加载数据的代码@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_productlisting, container, false);
setHasOptionsMenu(true);
rv_Products = (RecyclerView) view.findViewById(R.id.rv_product);
return view;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
handler = new Handler();
list = new ArrayList<>();
getProductList();
}
}
public static void setGrid(Activity activity) {
isList = false;
GridLayoutManager lm = new GridLayoutManager(activity, 2);
rv_Products.setLayoutManager(lm);
adapter = new ProductFeatureAdapter(list, rv_Products, "grid");
rv_Products.setAdapter(adapter);
}
public void getProductList() {
new VolleyRequest(getActivity(), VolleyRequest.Method.POST, Url.getProductList, Param.productList(TiresFragment.categoryId.get(TiresFragment.pagePosition), page_count + ""), 0, true, this);
}
@Override
public void getVolleyRequestResponse(VolleyRequestResponse volleyResponse) {
switch (volleyResponse.type) {
case 0:
try {
JSONObject objJson = new JSONObject(volleyResponse.output);
if (objJson.optInt(Param.KEY_STATUS_CODE) == Param.SUCCESS_CODE) {
JSONArray featureProductArray = objJson.getJSONArray("response");
total_page = objJson.getInt("totalPages");
if (!isLoading) {
for (int i = 0; i < featureProductArray.length(); i++) {
JSONObject object = featureProductArray.getJSONObject(i);
list.add(new Product(object.getString("id"),
object.getString("productname"),
"",
object.getString("imageurl"),
object.getString("price"),
"",
false,
false,
false,
0,
0));
}
} else {
ArrayList<Product> tmp_list = new ArrayList<>();
for (int i = 0; i < featureProductArray.length(); i++) {
JSONObject object = featureProductArray.getJSONObject(i);
tmp_list.add(new Product(object.getString("id"),
object.getString("name"),
object.getString("category"),
object.getString("imageurl"),
object.getString("price"),
object.getString("type"),
false,
false,
false,
0,
0));
}
list.addAll(tmp_list);
}
set_recycler();
}
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
private void set_recycler() {
Log.e("AAKASH", "IF LOAD " + isLoading + "");
Log.e("AAKASH", "IF PAGE COUNT " + page_count + "");
Log.e("AAKASH", "IF TOTAL PAGE " + total_page + "");
isList = false;
GridLayoutManager lm = new GridLayoutManager(getActivity(), 2);
rv_Products.setLayoutManager(lm);
Log.i("niral", "FRAGMENT CALLLED SET ADAPTER " + TiresFragment.pagePosition);
adapter = new ProductFeatureAdapter(list, rv_Products, "grid");
rv_Products.setAdapter(adapter);
adapter.notifyDataSetChanged();