简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM