使用ActionBarSherlock实现滑动导航抽屉

时间:2014-03-27 23:25:34

标签: android actionbarsherlock

我正在研究android项目,我正在尝试使用滑动导航抽屉实现ActionBarSherlock。

实际菜单工作正常但是,3行图标(我想如果我没记错的话,它叫做汉堡图标)不会出现,而是回到父母的小图标,不过点击它显示/隐藏菜单。

以下是我的代码:

public class MainActivity extends SherlockFragmentActivity {

    DrawerLayout mDrawerLayout;
    ListView mDrawerList;
    ActionBarDrawerToggle mDrawerToggle;


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

        FragmentTransaction t = getSupportFragmentManager().beginTransaction();
        Fragment frag = new MainFragment();
        t.replace(R.id.content_frame, frag);
        t.commit();

        mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        mDrawerList = (ListView)findViewById(R.id.listview_drawer);

        NavigationManagerAdapter menuAdapter = new NavigationManagerAdapter(MainActivity.this);
        menuAdapter.add(new NavigationMenuItem("MenuItem", "MENU_PRODUCTS", GuiType.TEXTVIEW, null));
        menuAdapter.add(new NavigationMenuItem("MenuItem2", "MENU_TEST", GuiType.TEXTVIEW_SUMMARY, "This is a summary", mSpecificClickListener));

        mDrawerList.setAdapter(menuAdapter);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

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

            public void onDrawerClosed(View view) {
                // TODO Auto-generated method stub
                super.onDrawerClosed(view);
            }

            public void onDrawerOpened(View drawerView) {
                // TODO Auto-generated method stub
                // Set the title on the action when drawer open
                getSupportActionBar().setTitle("hello");
                super.onDrawerOpened(drawerView);
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        mDrawerList.setOnItemClickListener(new DrawerClickListener());
    }

    public OnClickListener mSpecificClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Specific click listener used", Toast.LENGTH_LONG).show();
        }
    };

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == android.R.id.home) {

            if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
                mDrawerLayout.closeDrawer(mDrawerList);
            } else {
                mDrawerLayout.openDrawer(mDrawerList);
            }
        }

        return super.onOptionsItemSelected(item);
    }

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

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.main, menu);
        getSupportMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

感谢您提供的任何帮助。

3 个答案:

答案 0 :(得分:1)

<强>更新

注意:将指标更改为三行图标的ActionBarDrawerToggle只适用于AppCompat,请按以下方法之一进行操作! (第二是强烈推荐和更容易)

它并不像你那么简单...

将ActionBar(Sherlock)UpIndicator设置为NavigationDrawerToggle,您应该使用其中一个:

  1. SlidingMenu:https://github.com/sockeqwe/SlidingMenu
  2. 这是一个库,其中包含一个导航工具本身,它与NavigationDrawer

    不同

    差异:

    • 移动内容而非内容上的绘图菜单!
    • 淡化打开/关闭菜单,而不是淡化内容

    • 手势有一些错误
    • source完全可以编辑

    slidingmenu http://alexanderblom.se/images/spotify-menu.png

    1. SherlockNavigationDrawer:https://github.com/nicolasjafelle/SherlockNavigationDrawer
    2. 这个库只为您带来UpIndicator作为NavigationDrawerToggle功能! 应该使用blong Android支持库!

      navdrawer

答案 1 :(得分:0)

删除这些行

    getSupportActionBar().setHomeButtonEnabled(true); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

答案 2 :(得分:0)

如果您使用 ActionBarCompat 而不是 ActionBarSherlock ,您的代码将完美运行,因为ABS不支持此功能。对于ABS删除getSupportActionBar().setDisplayHomeAsUpEnabled(true);并添加以下行:

getSupportActionBar().setIcon(R.drawable.ic_drawer);