I HAVE ALREADY GOOGLED IT , AND I AM GOOGLING AND DOING RANDOM THINGS SINCE YESTERDAY
In fragment A, I have a tab layout which is connected to ViewPager2.
And the first tab have a fragment with recyclerview. When i open fragment A. i can't see any items in recyclerview. But when i change the tab and comes back to first tab, Item appears.
what i tried
- I have done similar thing on another activity and it works like charm.
- I have printed the arraylist after adding data. It works.
- I tried to open this fragment standalone in an activity and it works perfectly fine.
- It just seems to occur problem when it is in viewpager.
code of view pager and tablayout
ViewPager2 viewPager2 = view.findViewById(R.id.shopVP);
viewPager2.setAdapter(new ShopMenuTabAdapter(getActivity()));
TabLayout tabLayout = view.findViewById(R.id.shopTab);
TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> {
switch (position){
case 0:
tab.setText("Services Categories");
break;
case 1:
tab.setText("Reviews");
break;
case 2:
tab.setText("About");
break;
}
});
tabLayoutMediator.attach();
code of tab adapter
List<Fragment> mData = Arrays.asList( //as you can see i have 3 tabs.
new ShopServicesCategories(), // recyclerview is in this tab.
new ShopReview(),
new ShopAbout()
);
public ShopMenuTabAdapter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
@NonNull
@Override
public Fragment createFragment(int position) {
return mData.get(position);
}
@Override
public int getItemCount() {
return mData.size();
}
this is how i am setting up recyclerview and its adapter
// as "service categories" and "sub section" have same variables. i didn't bothered to create a model specially for categories
// in class
RecyclerView rv;
List<SubSectionModel> mData = new ArrayList<>();
SubSectionAdapter mAdapter;
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
// in onCreateView
rv = view.findViewById(R.id.shop_sservice_cat_rv);
rv.setLayoutManager(llm);
// in class
// i call this method after adding data to ArrayList.
private void setupRecyclerView(List<SubSectionModel> mData) {
mAdapter = new SubSectionAdapter(getActivity(), mData, new SubSectionAdapter.OnItemClickListener() {
@Override
public void onItemClick(SubSectionModel subSectionModel) {
Log.i("click",subSectionModel.getSub()+" "+subSectionModel.getHave_sub_sec());
openSubSec(subSectionModel.getSub());
}
});
rv.setAdapter(mAdapter);
}