Home > OS >  Change Viewpager2 fragment from child fragment
Change Viewpager2 fragment from child fragment

Time:02-06

I have a Main fragment/ViewPager fragment inside the Main Activity and the Main Fragment has the Viewpager2 where I add child fragments into ViewPager via Adapter. Earlier I had a method in Main Activity (Which also had view pager)

public void ChangeFragment_ViewPager(int position, boolean outside) {
        if (viewPager!=null){
            viewPager.setCurrentItem(position);
        }    
    }

This method easily changes the fragment when I call from any child fragment of viewpager but since I shifted the viewpager to the Main fragment, my viewpager always comes out null from child.

 MainFragment.newInstance().ChangeFragment_ViewPager(0, false);

The Main Fragment

public class MainFragment extends Fragment {
    ViewPager2 viewPager;
    public MainFragment() {
        // Required empty public constructor
    }


    // TODO: Rename and change types and number of parameters
    public static MainFragment newInstance() {
        MainFragment fragment = new MainFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.main_fragment, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPager = view.findViewById(R.id.Navigation_Drawer_ViewPager);
        TabAdapter tabAdapter = new TabAdapter(getChildFragmentManager(), getLifecycle());
        viewPager.setAdapter(tabAdapter);
    }

   
    public void ChangeFragment_ViewPager(int position, boolean outside) {
        if (viewPager!=null){
            viewPager.setCurrentItem(position);
        }

    }

Tab Adapter

public class TabAdapter extends FragmentStateAdapter {
    String TAG="###TAB ADAPTER###";

    public TabAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
        super(fragmentManager, lifecycle);
    }


    @NonNull
    @Override
    public Fragment createFragment(int position) {
        switch (position) {
            case 0:
                Log.d(TAG, "Fragment 1");
                return FriendsList.newInstance();
            case 1:
                Log.d(TAG, "Fragment 2");
                return PPL_main.newInstance();
        }
        return FriendsList.newInstance();
    }

    @Override
    public int getItemCount() {
        return 2;
    }
}

How can I change the fragment from any ViewPager's child fragment?

CodePudding user response:

A couple of things that need some fix:

First:

getSupportFragmentManager().beginTransaction()
                .setReorderingAllowed(true)
                .add(R.id.Navigation_Main_Layout,MainFragment.class,null)                     
                .commit(); 

getChildFragmentManager() should be used instead of getSupportFragmentManager(), the ViewPager is hosted inside a fragment not an activity.

Second:

MainFragment.newInstance().ChangeFragment_ViewPager(0,false);

This line of code instantiates a brand new instance of MainFragment; and therefore you found a null ViewPager.

Instead of that, the current instance exist in the fragment manager is required or simply use requireParentFragment() from the ViewPager child page fragment to get the MainFragment that hosts the ViewPager:

((MainFragment) requireParentFragment()).ChangeFragment_ViewPager(0, false);
  • Related