2 回答
TA贡献1871条经验 获得超13个赞
对于以后发现这个的任何人。我稍微修改了方法@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);
}
和背压
@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);
}
TA贡献2037条经验 获得超6个赞
对于以后发现这个的任何人。正如 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;
}
添加回答
举报