我为
android实现了BottomNavigation视图,我有一些片段显示为BottomNavigation页面.根据
Google Material Design Guide Lines我想用交叉淡入淡出动画显示片段.
通过触摸BottomNavigation的项目,我的ViewpPager使用默认幻灯片动画更改片段.
我在this和this阅读了一些解决方案.但这些并不是真正的淡入淡出动画,我无法设置渐弱的持续时间.
那么有什么方法可以在更改ViewPager的选项卡时设置动画?
解决方法
最后,我找到了答案.
我用一个布局更改了ViewPager以保留我的片段(帧布局).然后我将片段添加到fragmentTransaction中.
通过触摸BottomNavigation上的项目,当前片段隐藏和新片段将显示为使用fragmentTransaction中定义的淡入淡出动画.
这是我的代码:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_page); addFragmentsToManager(); } private void addFragmentsToManager() { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.setCustomAnimations(R.anim.fade_in,R.anim.fade_out); fragmentTransaction.add(R.id.flContent,tripFragment,tripFragment.getClass().getSimpleName()); fragmentTransaction.add(R.id.flContent,notificationFragment,notificationFragment.getClass().getSimpleName()); fragmentTransaction.add(R.id.flContent,searchFragment,searchFragment.getClass().getSimpleName()); fragmentTransaction.add(R.id.flContent,profileFragment,profileFragment.getClass().getSimpleName()); fragmentTransaction.hide(tripFragment); fragmentTransaction.hide(notificationFragment); fragmentTransaction.hide(searchFragment); fragmentTransaction.hide(profileFragment); fragmentTransaction.commit(); } private void changeTab(int position) { Fragment fragment; switch (position) { fragment = .....// get framgnet from position } FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.setCustomAnimations(R.anim.fade_in,R.anim.fade_out); fragmentTransaction.hide(prvFragment); fragmentTransaction.show(fragment).commit(); prvFragment = fragment; }