gpt4 book ai didi

java - 使用 BottomNavigationView 的 fragment 过渡动画

转载 作者:行者123 更新时间:2023-11-29 04:12:21 25 4
gpt4 key购买 nike

我目前有一个应用程序,它有一个 MainActivity、一个包含 3 个项目的 BottomNavigationView 和 3 个相应的 fragment :

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;
}

这真的很好用。我的问题是循环浏览 fragment 时,进入/退出动画总是相同的。

因此,如果我从主页转到 Collection 夹,然后返回主页,主页从右侧进入,而我想从左侧进入。

有没有办法根据用户在应用中的来源来设置自定义进入/退出过渡动画?

最佳答案

对于后来发现这个的人。我稍微修改了方法@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);
}

关于java - 使用 BottomNavigationView 的 fragment 过渡动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54355204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com