我目前有一个具有MainActivity,带有3个项目的BottomNavigationView和3个相应片段的应用程序:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// Bottom Nav
bottomNav = findViewById(R.id.bottom_nav);
bottomNav.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment fragment = null;
switch (menuItem.getItemId()) {
case R.id.bottom_nav_home:
fragment = new HomeFragment();
break;
case R.id.bottom_nav_favorites:
fragment = new FavoritesFragment();
break;
case R.id.bottom_nav_settings:
fragment = new SettingsFragment();
break;
}
return loadFragment(fragment, position);
}
}
);
}
private boolean loadFragment(Fragment fragment) {
if(fragment != null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
transaction.replace(R.id.container, fragment);
transaction.commit();
return true;
}
return false;
}
这真的很好。我的问题是循环遍历片段时,输入/退出动画始终相同。
因此,如果我从“首页”转到“收藏夹”,然后返回“首页”,则当我想从左侧输入时,首页将从右侧进入。
是否可以根据用户来自应用程序的位置来设置自定义进入/退出过渡动画?
答案 0 :(得分:1)
对于以后找到它的任何人。正如Filippo Vigani建议的那样,我想出了一种跟踪当前位置的方法。这是我所做的:
public class MainActivity extends AppCompatActivity implements
AddCategoryFragment.AddCategoryFragmentListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadFragment(new HomeFragment(), 1);
// Bottom Nav
bottomNav = findViewById(R.id.bottom_nav);
bottomNav.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment fragment = null;
int newPosition = 0;
switch (menuItem.getItemId()) {
case R.id.bottom_nav_home:
fragment = new HomeFragment();
newPosition = 1;
break;
case R.id.bottom_nav_favorites:
fragment = new FavoritesFragment();
newPosition = 2;
break;
case R.id.bottom_nav_settings:
fragment = new SettingsFragment();
newPosition = 3;
break;
}
return loadFragment(fragment, newPosition);
}
}
);
} // End of onCreate
private boolean loadFragment(Fragment fragment, int newPosition) {
if(fragment != null) {
if(startingPosition > newPosition) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right );
transaction.replace(R.id.container, fragment);
transaction.commit();
}
if(startingPosition < newPosition) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
transaction.replace(R.id.container, fragment);
transaction.commit();
}
startingPosition = newPosition;
return true;
}
return false;
}
答案 1 :(得分:0)
对于以后找到它的任何人。我对方法进行了一些修改@Ian Greene。 在应用程序启动时有一项禁用动画的检查
private boolean loadFragment(Fragment fragment, int newPosition) {
if(fragment != null) {
if(newPosition == 0) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.contentLayout, fragment).commit();
}
if(startingPosition > newPosition) {
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right )
.replace(R.id.contentLayout, fragment).commit();
}
if(startingPosition < newPosition) {
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left)
.replace(R.id.contentLayout, fragment).commit();
}
startingPosition = newPosition;
return true;
}
return false;
}
然后创建
if (savedInstanceState == null) {
loadFragment(mFirstFragment, 0);
}
和backPressed
@Override
public void onBackPressed() {
int selectedItemId = bottomNavigationView.getSelectedItemId();
if (R.id.navigation_home != selectedItemId) {
loadFragment(mFirstFragment,1);
toolbar.setTitle(title[0]);
bottomNavigationView.setSelectedItemId(R.id.navigation_home);
} else {
super.onBackPressed();
}
}
我希望这会对某人有所帮助
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment fragment = null;
int newPosition = 0;
if (bottomNavigationView.getSelectedItemId() != menuItem.getItemId()) {
switch (menuItem.getItemId()) {
case R.id.navigation_home:
fragment = mFirstFragment;
toolbar.setTitle(title[0]);
newPosition = 1;
break;
case R.id.navigation_dashboard:
fragment = mSecondFragment;
toolbar.setTitle(title[1]);
newPosition = 2;
break;
case R.id.navigation_notifications:
fragment = mThirdFragment;
toolbar.setTitle(title[2]);
newPosition = 3;
break;
}
}
return loadFragment(fragment, newPosition);
}