Florian Walther
Florian Walther

Reputation: 6971

Android: Change Fragment within a ViewPager without Sliding

i am a beginner so please keep that in mind when answering.

I just implemented the functionality to slide between 2 different Fragments. I followed this tutorial: https://developer.android.com/training/animation/screen-slide.html

It is working fine. But i also want to give the option to switch between those 2 Fragments by clicking a Button instead of swiping the screen.

public void timerWindowButton(View v) {
    //timerWindow() executes the newInstance() method of the fragment
    Fragment fragment = timerWindow();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction ft = fragmentManager.beginTransaction();
    //here i try to replace the content of the pager, but the result is 
    //an empty screen
    ft.replace(R.id.pager, fragment);
    ft.commit();

I tried to google my problem, but i just end up with search results for how to implement a ViewPager in the first place. Any help is appreciated!

Upvotes: 0

Views: 440

Answers (1)

Shrikant
Shrikant

Reputation: 1580

Try to use viewPager.setCurrentItem(1); inside your button click listener. Make sure you load your fragments properly before showing them up.

Upvotes: 3

Related Questions