gpt4 book ai didi

android - ViewPager,与多个选项卡的 RecyclerView 相同的 fragment

转载 作者:太空宇宙 更新时间:2023-11-03 12:16:04 41 4
gpt4 key购买 nike

我正在开发一个应用程序,它有一个类别列表。如果我们点击类别,那么我必须在选项卡中显示不同的子类别,每个选项卡都有一个产品列表。

一切正常,选项卡和 fragment 都加载了正确的数据,只有 recyclerview onClick 给出了不正确的项目(来自相邻 fragment 的 recyclerview 的项目) .主要发生在 viewpager 滑动时。

Activity 中的代码:

    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();
}
}

我不知道这里出了什么问题。

请帮忙,谢谢。

最佳答案

很好地解决了问题,

从 RecyclerView 适配器中删除了静态字段

现在工作正常。

关于android - ViewPager,与多个选项卡的 RecyclerView 相同的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35415767/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com