猿问

使用 BottomNavigationView 的片段过渡动画

我目前有一个应用程序,它有一个 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;

}

这真的很好用。我的问题是在片段中循环时,进入/退出动画总是相同的。


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


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


慕田峪9158850
浏览 132回答 2
2回答

慕桂英4014372

对于以后发现这个的任何人。我稍微修改了方法@Ian Greene。应用程序启动时有一个禁用动画的检查&nbsp;private boolean loadFragment(Fragment fragment, int newPosition) {&nbsp; &nbsp; if(fragment != null) {&nbsp; &nbsp; &nbsp; &nbsp; if(newPosition == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getSupportFragmentManager()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .beginTransaction()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .replace(R.id.contentLayout, fragment).commit();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(startingPosition > newPosition) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getSupportFragmentManager()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .beginTransaction()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .replace(R.id.contentLayout, fragment).commit();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(startingPosition < newPosition) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getSupportFragmentManager()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .beginTransaction()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .replace(R.id.contentLayout, fragment).commit();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; startingPosition = newPosition;&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; return false;}然后在创建&nbsp; &nbsp; if (savedInstanceState == null) {&nbsp; &nbsp; &nbsp; &nbsp; loadFragment(mFirstFragment, 0);&nbsp; &nbsp; }和背压@Overridepublic void onBackPressed() {&nbsp; &nbsp; int selectedItemId = bottomNavigationView.getSelectedItemId();&nbsp; &nbsp; if (R.id.navigation_home != selectedItemId) {&nbsp; &nbsp; &nbsp; &nbsp; loadFragment(mFirstFragment,1);&nbsp; &nbsp; &nbsp; &nbsp; toolbar.setTitle(title[0]);&nbsp; &nbsp; &nbsp; &nbsp; bottomNavigationView.setSelectedItemId(R.id.navigation_home);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; super.onBackPressed();&nbsp; &nbsp; }}我希望这会对某人有所帮助&nbsp;@Overridepublic boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {&nbsp; &nbsp; Fragment fragment = null;&nbsp; &nbsp; int newPosition = 0;&nbsp; &nbsp; if (bottomNavigationView.getSelectedItemId() != menuItem.getItemId()) {&nbsp; &nbsp; &nbsp; &nbsp; switch (menuItem.getItemId()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R.id.navigation_home:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fragment = mFirstFragment;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toolbar.setTitle(title[0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newPosition = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R.id.navigation_dashboard:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fragment = mSecondFragment;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toolbar.setTitle(title[1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newPosition = 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R.id.navigation_notifications:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fragment = mThirdFragment;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toolbar.setTitle(title[2]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newPosition = 3;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return loadFragment(fragment, newPosition);}

阿晨1998

对于以后发现这个的任何人。正如 Filippo Vigani 建议的那样,我想出了一种方法来跟踪当前位置。这是我所做的:public class MainActivity extends AppCompatActivity implements&nbsp; &nbsp; AddCategoryFragment.AddCategoryFragmentListener{@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; loadFragment(new HomeFragment(), 1);&nbsp; &nbsp; // Bottom Nav&nbsp; &nbsp; bottomNav = findViewById(R.id.bottom_nav);&nbsp; &nbsp; bottomNav.setOnNavigationItemSelectedListener(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new BottomNavigationView.OnNavigationItemSelectedListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Fragment fragment = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int newPosition = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (menuItem.getItemId()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R.id.bottom_nav_home:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fragment = new HomeFragment();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newPosition = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R.id.bottom_nav_favorites:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fragment = new FavoritesFragment();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newPosition = 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R.id.bottom_nav_settings:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fragment = new SettingsFragment();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newPosition = 3;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return loadFragment(fragment, newPosition);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; );} // End of onCreateprivate boolean loadFragment(Fragment fragment, int newPosition) {&nbsp; &nbsp; if(fragment != null) {&nbsp; &nbsp; &nbsp; &nbsp; if(startingPosition > newPosition) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transaction.replace(R.id.container, fragment);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transaction.commit();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(startingPosition < newPosition) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transaction.replace(R.id.container, fragment);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transaction.commit();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; startingPosition = newPosition;&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; return false;}
随时随地看视频慕课网APP

相关分类

Java
我要回答