我正在开发一个有类别列表的应用。 如果我们点击类别,那么我必须在标签中显示不同的子类别,每个标签都有一个产品列表。
一切工作正常,标签和片段都加载了正确的数据,只有 recyclerview onClick 会提供错误的商品(来自相邻片段的商品&#39) ; s recyclerview)。主要发生在viewpager滑动。
活动中的代码:
PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount(), response);
// response is a list of subcategories and products
viewPager.setOffscreenPageLimit(1);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
PagerAdapter中的代码:
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
SubCategoryResponse response;
public PagerAdapter(FragmentManager fm, int NumOfTabs, SubCategoryResponse response) {
super(fm);
this.mNumOfTabs = NumOfTabs;
this.response = response;
}
@Override
public Fragment getItem(int position) {
L.m(position +" => "+response.getList().get(position).getProducts().get(0).getName());
TabFragment fragmentDummy = TabFragment.getInstance(position, response.getList().get(position).getProducts());
return fragmentDummy;
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
TabFragment中的代码:
public class TabFragment extends Fragment implements ProductAdapter.mClickListener {
@Bind(R.id.list_container_fragment)
LinearLayout listContainerFragment;
@Bind(R.id.list_products)
RecyclerView productListView;
private ProductAdapter productsAdapter;
private Dialog myDialog;
private LinearLayoutManager linearLayoutManager;
private ArrayList<Products> responseProducts = null;
public TabFragment() {
// Required empty public constructor
}
public static TabFragment getInstance(int position, ArrayList<Products> response) {
TabFragment fragmentDummy = new TabFragment();
Bundle args = new Bundle();
args.putParcelableArrayList("PRODUCTS", response);
args.putInt("position", position);
fragmentDummy.setArguments(args);
return fragmentDummy;
}
@Override
public void setArguments(Bundle args) {
super.setArguments(args);
this.responseProducts = args.getParcelableArrayList("PRODUCTS");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tab, container, false);
ButterKnife.bind(this, view);
linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
productListView.setLayoutManager(linearLayoutManager);
productListView.setItemAnimator(new DefaultItemAnimator());
populateList();
return view;
}
public void populateList() {
L.m("populate list");
if (responseProducts.size() > 0) {
L.m("Inside populate list => " + responseProducts.get(0).getName());
productsAdapter = new ProductAdapter(responseProducts);
productsAdapter.setListner(getActivity(), this);
productListView.setAdapter(productsAdapter);
productListView.setHasFixedSize(true);
} else {
showError("No Products to Show");
}
}
public void showError(String msg) {
SnackbarManager.show(
Snackbar.with(getActivity()) // context
.text(msg) // text to be displayed
.textColor(Color.WHITE) // change the text color
// .textTypeface(myTypeface) // change the text font
.color(getResources().getColor(R.color.colorPrimary)) // change the background color
.duration(Snackbar.SnackbarDuration.LENGTH_LONG)
, getActivity());
}
@Override
public void mClickDetails(View view, int pos) {
startActivity(new Intent(getActivity(), ProductDetailsActivity.class)
.putExtra("PRODUCT", responseProducts.get(pos).getName()));
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
//populateList();
}
}
我不知道这里出了什么问题。
请帮助,谢谢。
答案 0 :(得分:7)
很好地解决了这个问题,
从RecyclerView Adapter中删除静态字段。
现在工作正常。