0

I am trying to get a tab layout working with a common fragment, which gets passed an argument string called category, which is used for filtering a query to be displayed in the activity, depending on the tab position. This is my ClothingSectionsPagerAdapter class which handles this.

public class ClothingSectionsPagerAdapter extends FragmentStatePagerAdapter {

    private String[] mCategories;

    public ClothingSectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public void setContext(Context context) {
        mCategories = context.getResources().getStringArray(R.array.category);
    }

    @Override
    public Fragment getItem(int position) {
        return ClothingCategoryFragment.newInstance(mCategories[position]);
    }

    @Override
    public int getCount() {
        return mCategories.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mCategories[position];
    }
}

The fragment which gets passed the string argument should when on screen, load the clothing items with this category. Here is my fragment.

public class ClothingCategoryFragment extends Fragment {

    @BindView(R.id.catalog_swipe_refresh_layout)
    SwipeRefreshLayout mCatalogRefreshLayout;

    @BindView(R.id.catalog_recycler_view)
    RecyclerView mCatalogRecyclerView;

    private CatalogAdapter mCatalogAdapter;
    private List<Clothing> mClothing;
    private String mCategory;

    private ClothingRepositoryActions mCatalogRepository;

    public ClothingCategoryFragment() {
        // Required empty public constructor
    }

    public static ClothingCategoryFragment newInstance(String category) {
        ClothingCategoryFragment clothingCategoryFragment =
                new ClothingCategoryFragment();
        Bundle args = new Bundle();
        args.putString(CATEGORY_FRAGMENT_ARGUMENT_KEY, category);
        clothingCategoryFragment.setArguments(args);
        return clothingCategoryFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_clothing_category,
                container, false);
        ButterKnife.bind(this, rootView);

        setAdapter();

        mCatalogRepository = new ClothingRepository();
        mCategory = getArguments().getString(CATEGORY_FRAGMENT_ARGUMENT_KEY, mCategory);
        loadClothing();

        mCatalogRefreshLayout.setColorSchemeResources(R.color.refresh_progress_1);
        mCatalogRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadClothing();
            }
        });

        return rootView;
    }

    // Irrelevent methods omitted
    ...

    public void setAdapter() {
        mClothing = new ArrayList<>();
        mCatalogRecyclerView.setHasFixedSize(true);
        mCatalogAdapter = new CatalogAdapter(getActivity(), mClothing);

        final GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);

        mCatalogRecyclerView.setLayoutManager(layoutManager);
        mCatalogRecyclerView.setAdapter(mCatalogAdapter);
    }

    public void loadClothing() {
        if (isNetworkAvailable()) {
            mCatalogRepository.loadClothing(mCategory);
        } else {
            mCatalogRepository.loadClothingFromRealm(mCategory);
        }
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
}

When I switch between tabs, there is no change in query results, meaning that the loadClothing method is not called. I'm guessing the fragments are created and onCreateView is only called once for each fragment. However, once I refresh the fragment using the refresh layout, the correct results are shown meaning the correct category string was used for the query. My question is how do I get the fragments to call loadClothing with the correct category string when switching tabs?

Tom Finet
  • 2,056
  • 6
  • 30
  • 54

0 Answers0