在“我的片段”页面上,我添加了一个ViewPager
,并在该ViewPager
中加载了列表片段。第一次,它会正确加载列表,一旦导航到另一个片段,然后返回ViewPager
内容(列表片段)不显示内容,它只会加载ViewPager
标题,一旦更改标签会加载内容,这是我的代码,
首页片段
public class HomeFragment extends Fragment {
@BindView(R.id.progressLayout)
LinearLayout progressLayout;
@BindView(R.id.tabLayout)
TabLayout tabLayout;
@BindView(R.id.viewpager)
ViewPager viewpager;
private static int pagePos = 0;
ArrayList<Category> categories;
CategoryPagerAdapter catrgoryAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
ButterKnife.bind(this, rootView);
categories = getProductCategories();
setupViewPager();
tabLayout.setupWithViewPager(viewpager);
return rootView;
}
@Override
public void onResume() {
super.onResume();
getActivity().setTitle(null);
catrgoryAdapter.notifyDataSetChanged();
}
private ArrayList<Category> getProductCategories() {
categories = new ArrayList<Category>();
categories.add(new Category(1, "Evening Dresses"));
categories.add(new Category(2, "Abaya"));
categories.add(new Category(3, "Night Dresses"));
categories.add(new Category(4, "Shirt"));
return categories;
}
private void setupViewPager() {
catrgoryAdapter = new CategoryPagerAdapter(getActivity(), getActivity().getSupportFragmentManager(), categories);
viewpager.invalidate();
viewpager.setAdapter(catrgoryAdapter);
if (catrgoryAdapter.getCount() > pagePos)
viewpager.setCurrentItem(pagePos);
catrgoryAdapter.notifyDataSetChanged();
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
pagePos = position;
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
页面视图适配器
public class CategoryPagerAdapter extends FragmentPagerAdapter {
Context context;
private ArrayList<Category> categories = new ArrayList<>();
public CategoryPagerAdapter(Context context, FragmentManager fm, ArrayList<Category> categories) {
super(fm);
this.context = context;
this.categories = categories;
}
@Override
public Fragment getItem(int position) {
return new ProductListingFragment(categories.get(position));
}
@Override
public int getCount() {
return categories.size();
}
@Override
public CharSequence getPageTitle(int position) {
return categories.get(position).getCategoryName();
}
public View getTabView(int position) {
View tab = LayoutInflater.from(context).inflate(R.layout.category_tab_title_view, null);
TextView txtTitle = (TextView) tab.findViewById(R.id.txtTitle);
txtTitle.setText(getPageTitle(position));
return tab;
}
}
ProductListingFragment页面
public class ProductListingFragment extends Fragment implements ProductActionListener {
@BindView(R.id.progressLayout)
LinearLayout progressLayout;
@BindView(R.id.recyclerView)
RecyclerView recyclerView;
@BindView(R.id.noResultTextView)
TextView noResultTextView;
Category category;
ArrayList<Product> products;
ProductListAdapter productsAdapter;
GridLayoutManager layoutManager;
public ProductListingFragment() {}
@SuppressLint("ValidFragment")
public ProductListingFragment(Category category) {
this.category = category;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_product_listing, container, false);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
ButterKnife.bind(this, rootView);
layoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(layoutManager);
products = getCategoryProducts();
populateProductList();
return rootView;
}
@Override
public void onResume() {
super.onResume();
products = getCategoryProducts();
populateProductList();
}
private ArrayList<Product> getCategoryProducts() {
products = new ArrayList<>();
products.add(new Product(Integer.parseInt(category.getCategoryId() + "" + 1)));
products.add(new Product(Integer.parseInt(category.getCategoryId() + "" + 2)));
products.add(new Product(Integer.parseInt(category.getCategoryId() + "" + 3)));
products.add(new Product(Integer.parseInt(category.getCategoryId() + "" + 4)));
products.add(new Product(Integer.parseInt(category.getCategoryId() + "" + 5)));
products.add(new Product(Integer.parseInt(category.getCategoryId() + "" + 6)));
products.add(new Product(Integer.parseInt(category.getCategoryId() + "" + 7)));
products.add(new Product(Integer.parseInt(category.getCategoryId() + "" + 8)));
products.add(new Product(Integer.parseInt(category.getCategoryId() + "" + 9)));
return products;
}
private void populateProductList() {
int scrollTo = 0;
if (layoutManager != null)
scrollTo = layoutManager.findFirstVisibleItemPosition();
noResultTextView.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
if (products != null && products.size() > 0) {
productsAdapter = new ProductListAdapter(getActivity(), this, products);
recyclerView.invalidate();
recyclerView.setAdapter(productsAdapter);
if (scrollTo > 0 && products.size() > scrollTo)
recyclerView.scrollToPosition(scrollTo);
} else {
recyclerView.setVisibility(View.GONE);
noResultTextView.setVisibility(View.VISIBLE);
}
}
@Override
public void ProductSelected(Product product) {
toProductDetailsFragment();
}
@Override
public void ProductAddedToFavourite(Product product) {
AppController.getInstance().addRemoveFavourites(product);
populateProductList();
}
@Override
public void ProductAddedToCart(Product product) {
}
private void toProductDetailsFragment() {
ProductDetailsFragment fragment = new ProductDetailsFragment();
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.contentView, fragment).addToBackStack(null).commitAllowingStateLoss();
}
}