skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    I just changed the height of recyclerview to wrap content instead of 0dp(match constraint).


  2. First, get the data and then set up your tabs.

    OR

    It takes too much time to get data from the server then you can simply get this data into your main activity and store it globally in your application and then pass this data into your tabs.

    Passing arguments into fragment

    adapter.addFragment(AboutFragment.newInstance(data), "About");
    adapter.addFragment(SkillsFragment.newInstance(data), "Skills");
    

    Main App class:

    public class App extends Application {
        public static List<UserInfo> userInfo = new ArrayList<>();
        public static List<Skills> skillsList = new ArrayList<>();
    
        public static Users getUserInfo() {
           return userInfo;
        }
    
        public static void setUserInfo(UserInfo userInfo) {
           App.userInfo = userInfo;
        }
    
        public static Users getSkillsList() {
           return skillsList;
        }
    
        public static void setSkillsList(Skills skillsList) {
           App.skillsList = skillsList;
        }
    
    }
    

    And then add this into your manifest:

    <application
        android:name=".App"
        ....>
        ....
        ....
    </application>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search