简体   繁体   English

抽屉导航栏的DrawerArrowToggle动画

[英]DrawerArrowToggle animation for the Drawer Navigation bar

I want to use Google's animation to turn the navigation icon to an arrow upon opening of the navigation bar and turn it back to a drawer navigation icon. 我想使用Google的动画在打开导航栏时将导航图标变为箭头,然后再将其变为抽屉导航图标。 I am also wondering if I can keep the action bar stationary when moving to another activity. 我也想知道在移至其他活动时是否可以保持操作栏静止。

---EDIT--- - -编辑 - -

This question is out of date and no longer relevant as the navigation drawer in material design has been provided for developers in an easy and seemless process that no longer requires developers to add the hamburger side menu to back arrow animation as it is already provided. 这个问题已经过时了,不再相关,因为已经以简单易行的过程为开发人员提供了材料设计中的导航抽屉,不再需要开发人员将汉堡包侧面菜单添加到后退箭头动画中,因为它已经提供了。

     menutitles = getResources().getStringArray(R.array.sidebar);
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mDrawerList = (ListView) findViewById(R.id.slider_list);

            mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                    R.layout.list_tem, R.id.title, menutitles));
            // Set the list's click listener
            mTitle = mDrawerTitle = getTitle();
            mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                    R.mipmap.drawer, R.string.drawer_open, R.string.drawer_close) {

                /**
                 * Called when a drawer has settled in a completely closed state.
                 */
                public void onDrawerClosed(View view) {
                    super.onDrawerClosed(view);
                    getActionBar().setTitle(mTitle);
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }

                /**
                 * Called when a drawer has settled in a completely open state.
                 */
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);
                    getActionBar().setTitle(mDrawerTitle);
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }
            };

            // Set the drawer toggle as the DrawerListener
            mDrawerLayout.setDrawerListener(mDrawerToggle);
            getActionBar().setDisplayHomeAsUpEnabled(true);
            getActionBar().setHomeButtonEnabled(true);

    /**   * When using the ActionBarDrawerToggle, you must call it during   * onPostCreate() and onConfigurationChanged()...   */
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();    }
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
    /** Swaps fragments in the main content view */
    private void selectItem(int position) {
        switch (position){
            case 0:
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                finish();
                break;
            case 1: Intent intent2 = new Intent(this, Time.class);
                startActivity(intent2);
                finish();
                break;
            case 2: Intent intent3 = new Intent(this, Temperature.class);
                startActivity(intent3);
                finish();
                break;
            case 3: Intent intent4 = new Intent(this, Weight.class);
                startActivity(intent4);
                finish();
                break;
            case 4: Intent intent5 = new Intent(this, Length.class);
                startActivity(intent5);
                finish();
                break;
            case 5: Intent intent6 = new Intent(this, Money.class);
                startActivity(intent6);
                finish();
                break;
            case 6:
                Intent intent1 = new Intent(this, Metric.class);
                startActivity(intent1);
                finish();
                break;
            case 7:
                Intent intent7 = new Intent(this, Food.class);
                startActivity(intent7);
                finish();
                break;
            case 8:
                Intent intent8 = new Intent(this, Data.class);
                startActivity(intent8);
                finish();
                break;
        }
        // Create a new fragment and specify the planet to show based on position


        // Highlight the selected item, update the title, and close the drawer
        mDrawerList.setItemChecked(position, true);
        setTitle("Converjz");
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }




    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            selectItem(position);
        }
    }

}

I want to use Google's animation to turn the navigation icon to an arrow upon opening of the navigation bar and turn it back to a drawer navigation icon 我想使用Google的动画在打开导航栏时将导航图标变为箭头,然后再将其变为抽屉导航图标。

Looking at the constructor call, it's evident that you're using the v4 support version of the ActionBarDrawerToggle . 查看构造函数调用,很明显您正在使用ActionBarDrawerToggle的v4支持版本。 If you want the hamburger-arrow animation, you need to use the v7 appcompat version. 如果要汉堡包箭头动画,则需要使用v7 appcompat版本。

To do this, first add the v7 appcompat library to your project. 为此,首先将v7 appcompat库添加到您的项目中。 Then, in the ActionBarDrawerToggle import statement, change v4 to v7 , and drop the R.mipmap.drawer argument in the constructor call. 然后,在ActionBarDrawerToggle import语句中,将v4更改为v7 ,并在构造函数调用中删除R.mipmap.drawer参数。

import android.support.v7.app.ActionBarDrawerToggle;
...

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
    R.string.drawer_open, R.string.drawer_close) {...}

I am also wondering if I can keep the action bar stationary when moving to another activity. 我也想知道在移至其他活动时是否可以保持操作栏静止。

If you want to use the Navigation Drawer pattern, you should be using Fragment s for the different sections, rather than opening a new Activity for each. 如果要使用Navigation Drawer模式,则应该对不同部分使用Fragment ,而不是为每个部分打开新的Activity Given the comments in your code, the example you've followed demonstrates how to do that. 给定代码中的注释,您所遵循的示例演示了如何执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM