我是初学者,所以请在回答时牢记这一点。
我刚刚实现了在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 :(得分:3)
尝试使用viewPager.setCurrentItem(1);在你的按钮内点击监听器。确保在显示之前正确加载碎片。