简体   繁体   中英

ViewPager, same Fragment with RecyclerView for multiple tabs

I am working on a app, which having a category list. If we click on category then I have to show different subcategories in tabs, each tab having a list of products.

Everything is working fine tabs and fragment are loaded with correct data, only recyclerview onClick gives incorrect item (items from adjacent fragment's recyclerview). Mostly happens on viewpager swipe.

code in 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) {

        }
    });

code in 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;
}

}

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

I don't know whats going wrong here.

Please help, Thank You.

Well fixed the problem,

Removed static field from RecyclerView Adapter.

Now working fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM