我试图让一个标签布局使用一个公共片段,它传递一个名为category的参数字符串,用于过滤要在活动中显示的查询,具体取决于标签位置。这是我的ClothingSectionsPagerAdapter
类来处理这个问题。
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];
}
}
传递字符串参数的片段应该在屏幕上加载此类别的服装项目。这是我的片段。
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();
}
}
当我在标签之间切换时,查询结果没有变化,这意味着不会调用loadClothing
方法。我猜测片段已创建,onCreateView
仅针对每个片段调用一次。但是,一旦我使用刷新布局刷新片段,就会显示正确的结果,这意味着正确的类别字符串用于查询。 我的问题是如何在切换标签时让片段使用正确的类别字符串调用loadClothing?