在Android中打开导航抽屉时,将汉堡操作栏图标更改为旋转箭头不起作用

时间:2015-07-06 10:26:20

标签: android android-actionbar navigation-drawer drawertoggle

我在Android应用程序中使用导航抽屉,工作正常。

我想为打开关闭 导航抽屉添加一些动画

动画:

1.Opening-Navigation Drawer显示箭头,隐藏汉堡包

2.Closing-Again显示汉堡包,隐藏箭头

打开和关闭它应该是旋转。我尝试了一些不工作的代码。

请有人帮我这样做。

styles.xml

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

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@android:color/white</item>
</style>

我试过了,link i referred

import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;


//for Navigation Bar
        DrawerLayout drawerLayout;
        ListView listView;
        String[] drawerlist;
        ActionBarDrawerToggle drawerListener;

        private MyAdapter4 myAdapter4;

@Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alerts);




                //navigation initialize
                        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
                        listView = (ListView) findViewById(R.id.drawerList);
                        drawerlist = getResources().getStringArray(R.array.drawerlist);

                        myAdapter4 = new MyAdapter4(this);
                        listView.setAdapter(myAdapter4);

                        //for navigation click

                        listView.setOnItemClickListener(new DrawerItemClickListener());

                        //Navigation Bar starts

                        drawerListener = new ActionBarDrawerToggle
                       (this, drawerLayout, R.drawable.ic_drawer, 
                                    R.string.drawer_open, R.string.drawer_close)
                        {

                            public void onDrawerClosed(View drawerView)
                            {
                                super.onDrawerClosed(drawerView);
                            }

                            public void onDrawerOpened(View drawerView)
                            {
                                super.onDrawerOpened(drawerView);
                            }
                        };

                            drawerLayout.setDrawerListener(drawerListener);
                            getSupportActionBar().setHomeButtonEnabled(true);
                            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            }

我的导航抽屉代码

2 个答案:

答案 0 :(得分:6)

下面的检查是获取汉堡风格导航抽屉的示例代码

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;

    public class MainActivity extends AppCompatActivity{
    DrawerLayout drawerLayout;
    ActionBarDrawerToggle toggle;
    private ArrayList<DrawerItem> drawerItems = new ArrayList<>();
    private ListView mDrawerList;
    private DrawerListAdapter mAdapter;

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mDrawerList = (ListView)findViewById(R.id.left_drawer_list);
    addDrawerItems();

/**Drawer Initialization*/
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    toggle = new ActionBarDrawerToggle
            (
                    this,
                    drawerLayout,
                    R.string.navigation_drawer_open,
                    R.string.navigation_drawer_close
            )
    {
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getSupportActionBar().hide();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            getSupportActionBar().show();
        }
    };
    drawerLayout.setDrawerListener(toggle);
    toggle.syncState();
}

 private void addDrawerItems() {
    drawerItems.add(new DrawerItem("ALL"));
    drawerItems.add(new DrawerItem("FAVOURITES"));
    drawerItems.add(new DrawerItem("FILTERS"));
    drawerItems.add(new DrawerItem("SETTINGS"));

     DrawerListAdapter adapter = new DrawerListAdapter(this, drawerItems);
            mDrawerList.setAdapter(adapter);
            mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                ///Write your listener here

            }});
        }
}

检查导入以了解我拥有的库。

我的v7库版本号编译&#39; com.android.support:appcompat-v7:22.1.1&#39;

更新:在下面添加到strings.xml

<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>

答案 1 :(得分:3)

将您的SDK平台更新到最新版本(当前版本为API 22),并将项目(appcompat_v7)构建目标设置为API 22。