Android:在ViewPager中更改片段而不滑动

时间:2017-08-07 12:06:52

标签: java android android-fragments android-activity 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的搜索结果。 任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:3)

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