0

I have implemented default fragment pager adapter for three fragments. And its working fine but what I want is to swap fragment from inside one of those three fragment(Not from activity). I have tried to search for ans but couldn't find it.

EDIT: I am using DEFAULT tablayout activity with slider.I am not using custom fragmentPagerAdapter. Below class is implemented in main activity.

public class SectionsPagerAdapter extends FragmentPagerAdapter { 
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    return new FragmentTest1();
                case 1:
                    return new FragmentTest2();
                case 2:
                    return new FragmentTest3();
                default:
                    return null;
            }}
        @Override
        public int getCount() {
            return 3;
        }

MainActivity onCreate method of main activity is given below...

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slider);    
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

    }

FragmenTest1 From FragmentTest1 I want to make transition to another fragment on action.

public class FragmentTest1 extends android.support.v4.app.Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.tab_log, container, false);
        Button button = rootView.findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        return rootView;
    }
}

I want to make transition on click event to another fragment...

3 Answers3

1

I have the same implementation, I have a ViewPagerFragment that uses an ImageAdapter to swap between fragments, which is an ImageFragment, so basically you need an adapter.

ViewPagerFragment:

public class ViewPagerFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_viewpager, container, false);
        ButterKnife.bind(this, view);

        mAdapter = new ImageAdapter(getChildFragmentManager(), yourObjects);
        mViewPager.setAdapter(mAdapter);

        return view;
}

ImageAdapter:

public class ImageAdapter extends FragmentStatePagerAdapter {

    private ArrayList<YourObject> mYourObjects;

    public ImageAdapter(FragmentManager fm, ArrayList<YourObject> yourObjects) {
        super(fm);
        mYourObjects = yourObjects;
    }

    @Override
    public Fragment getItem(int position) {
        return new ImageFragment().newInstance(position, mYourObjects);
    }

    @Override
    public int getCount() {
        return mYourObjects.size();
    }
}

And finally the ImageFragment:

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_image, container, false);
        ButterKnife.bind(this, view);
        mClickedImagePosition = getArguments().getInt(POSITION_KEY);
        mYourObject = getArguments().getParcelableArrayList(YOUR_OBJECT_KEY);

        // set your objects here

        return view;
    }

    public ImageFragment newInstance(int position, ArrayList<YourObject> yourObjects) {
        ImageFragment fragment = new ImageFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(POSITION_KEY, position);
        bundle.putParcelableArrayList(YOUR_OBJECT_KEY, yourObjects);
        fragment.setArguments(bundle);

        return fragment;
    }

I omitted some of the boilerplate code, like inflating views, but this will work. Tell me if you have any questions.

Suleyman
  • 2,765
  • 2
  • 18
  • 31
  • Thanks but I am using tablayout activity. I updated my question. And I would also try your ans. I don't have much time so that's why I'm hesitating to re-implement tablayout acitvity. – Abdul Rahman Khan Apr 13 '18 at 20:24
  • @khan did you setup your tablayout with the viewpager `tabLayout.setupWithViewPager(viewpager)`? – Suleyman Apr 13 '18 at 20:52
  • @khan I don't think you have to be frustrated, I'm trying to help as much as I can, you'll have to provide more code then, because I'll just keep guessing, like you viewpager fragment where your adapter is used and single page fragment. – Suleyman Apr 15 '18 at 10:38
  • I am really thankful for your help and I am not frustrated at all. I am sorry if I sounded like frustrated. I updated my question again. Please take a look. And thanks again for your help... – Abdul Rahman Khan Apr 15 '18 at 17:38
  • @khan no worries :) I was a bit harsh myself :) I believe that in your case when you do `mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());` you need to pass `getChildFragmentManager()` instead of `getSupportFragmentManager()` because when you are dealing with nested fragments in viewpager, the inner fragments are managed by the viewpager and not the activity. – Suleyman Apr 15 '18 at 21:45
  • `getChildFragmentManager()` is not accessible in MainActivity... But this trick works for me https://stackoverflow.com/questions/46705116/how-to-select-next-tab-from-child-fragment and I am really thankful for your help ... – Abdul Rahman Khan Apr 16 '18 at 13:07
  • @khan perfect, thanks for sharing, glad you found a solution :) – Suleyman Apr 16 '18 at 19:17
0

I can't remember exact code to do this but it IS possible! If you through your Fragment accesses the fragments parent (should be the fragment manager?) and then tells the fragment manager to switch to fragment x?..

I remember doing that in a app a while ago.. it's not pretty but it works! (If it works it ain't stupid... or is it?)

Payerl
  • 1,042
  • 2
  • 16
  • 33
  • I tried but it's not working. I attached the code below ... `final FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.container, new AppList(), "Log"); ft.addToBackStack(null); ft.commit();` – Abdul Rahman Khan Apr 13 '18 at 17:23
0

In this case fragments can only be accessible through viewpager(As for as I know) and to make viewpager to child fragments you have to make viewpager static in MainActivity. public static ViewPager viewPager; Access viewPager ParentActivity.viewPager.arrowScroll(View.FOCUS_RIGHT); Perform Action ParentActivity.viewPager.arrowScroll(View.FOCUS_LEFT);

Reference:
https://stackoverflow.com/a/46707510/3481412