为所有子片段设置动画后退箭头

时间:2016-10-07 06:05:30

标签: android android-fragments android-toolbar

enter image description here我有多个片段,当从片段切换到片段时,工具栏将使用后退箭头进行更新。用户现在可以选择前一个片段。

当在汉堡包菜单和后退箭头之间切换时动画正在工作,而在片段之间切换后箭头不是动画。

 private FragmentManager.OnBackStackChangedListener backStackListener = new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            setNavIcon();
        }

        ;
    };


@Override
protected void onCreate(Bundle savedInstanceState) {

    Log.d(taglaunch, "on Create Running");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_appdrawer);
    navigate();
    if (savedInstanceState == null) {
        switchtofragment();
    }
    getFragmentManager().addOnBackStackChangedListener(backStackListener);

}



public void switchtofragment() {
        FragmentMenu fragmentMenu = new FragmentMenu();
        FragmentManager manger = getFragmentManager();
        manger.beginTransaction().replace(R.id.app_bar, fragmentMenu, "fragmenu").commit();
    }

 public void navigate() {

        Log.d(taglaunch, "Navigating");
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

    }



public void setDefault() {
        Log.d(taglaunch, "Setting to default");
        drawer.openDrawer(GravityCompat.START);
    }


    protected void setNavIcon() {
        int backStackEntryCount = getFragmentManager().getBackStackEntryCount();
        toggle.setDrawerIndicatorEnabled(backStackEntryCount == 0);
    }

工具栏xml

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarCustom">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.customToolbar">

    </android.support.v7.widget.Toolbar>

风格:

<style name="AppTheme.customToolbar" parent="ThemeOverlay.AppCompat.Light">
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    </style>

0 个答案:

没有答案