Просмотр списка пейджеров не обновляется при возобновлении - PullRequest
0 голосов
/ 17 сентября 2018

На странице «Мой фрагмент» я добавил 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();
    }
}
...