0

我是初学者,所以请在回答时记住这一点。Android:在ViewPager中更改片段而不滑动

我刚刚实现了在2个不同片段之间滑动的功能。我遵循本教程:https://developer.android.com/training/animation/screen-slide.html

它工作正常。但我也想通过点击按钮而不是刷屏来选择在这两个片段之间切换。

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

我试图谷歌我的问题,但我只是最终搜索结果如何实现ViewPager的第一个地方。 任何帮助表示赞赏!

+0

尝试使用viewPager.setCurrentItem(1);在你的按钮里面点击监听器。 请确保在展示之前正确加载您的碎片。 – Shrikant

+0

谢谢你,完美的工作! –

+0

太棒了!快乐编码.. – Shrikant

回答

1

尝试使用viewPager.setCurrentItem(1);在你的按钮里面点击监听器。确保在展示之前正确加载你的碎片。

+0

谢谢你,工作! –