Android:导航抽屉和ActionBar Compat

时间:2013-07-25 19:30:40

标签: android navigation drawer android-actionbar-compat

我使用 ActionBarSherlock 开始使用 NavigationDrawer 并取得了不错的效果,但我的公司批准此开源很可能不会很快到来,所以我正在努力切换到ActionBarCompat。

ActionBarCompat 昨天(2013年7月24日)刚刚正式发布。有没有人让两者相处得很好?我希望我能回答我自己的问题,但看看是否有人让这个问题起作用。比赛开始了! : - )

YouTube on ActionBarCompat的发布: https://www.youtube.com/watch?v=6TGgYqfJnyc


UPDATE(工作代码,是的!): 我已经从Google转换为使用ActionBarCompat的示例NavigationDrawer应用程序,它工作正常。您可以在此处找到它作为参考或开始您的项目: https://github.com/bcrider/NavigationDrawerActionBarCompat

2.x版本看起来比使用ActionBarSherlock的方式更好,但是我必须更多地使用ActionBarCompat来看看我是否更喜欢它。


4 个答案:

答案 0 :(得分:13)

注意:我太新了,不能在帖子等中添加多个链接,所以我回答我自己的问题而不是编辑它(希望不违反规则?)。一旦允许就会编辑原文。

使用ActionBarCompat添加导航抽屉的简单方法: 我发现转换现有的应用程序并不像我想象的那么糟糕。谷歌的样本让我相信片段是必需的,但事实并非如此......远非如此。

您可以使用DrawerLayout简单地包装现有布局,并插入ListView(或任何包含ListView的布局)以进行实际导航。然后将正常代码添加到现有的Activity(扩展ActionBarActivity)并构建导航,无论如何都要如此。

以下是一些用于包装现有布局的示例代码:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

[YOUR EXISTING LAYOUT GOES HERE]

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

</android.support.v4.widget.DrawerLayout>

如果你想从一个使用片段并从那里开始的示例应用开始,这里是基于示例代码的我的github存储库:https://github.com/bcrider/NavigationDrawerActionBarCompat

答案 1 :(得分:2)

我昨天将我的应用程序从ActionBarSherlock转换为ActionBarCompat。我有一些问题,但没有太严重。

我有一些意见:

要更新主题,我只需要将“Sherlock”覆盖为“AppCompat”。例如,我不是继承自@ style / Theme.Sherlock.Light.DarkActionBar,而是继承自@ style / Theme.AppCompat.Light.DarkActionBar。

对于操作项,只需这样更新:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          yourapp:showAsAction="ifRoom"  />
    ...
</menu>

在onCreateOptionsMenu中,使用普通的MenuItem,但使用MenuItemCompat的静态方法来执行ActionBar的操作。例如:MenuItemCompat.expandActionView(searchMenuItem);

如果您使用从RoboSherlockActivity继承的RoboGuice,如果您只是将其复制并更改为ActionBarActivity,则会遇到麻烦。这是我的解决方案:

public class RoboActionBarActivity extends ActionBarActivity implements RoboContext {

    protected EventManager eventManager;
    protected HashMap<Key<?>, Object> scopedObjects = new HashMap<Key<?>, Object>();

    @Inject
    ContentViewListener ignored; // BUG find a better place to put this

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final RoboInjector injector = RoboGuice.getInjector(this);
        eventManager = injector.getInstance(EventManager.class);
        injector.injectMembersWithoutViews(this);
        super.onCreate(savedInstanceState);
        eventManager.fire(new OnCreateEvent(savedInstanceState));
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        contentViewChanged();
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        contentViewChanged();
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(view, params);
        contentViewChanged();
    }

    @Override
    public void addContentView(View view, ViewGroup.LayoutParams params) {
        super.addContentView(view, params);
        contentViewChanged();
    }

    private void contentViewChanged() {
        RoboGuice.getInjector(this).injectViewMembers(this);
        eventManager.fire(new OnContentChangedEvent());
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        eventManager.fire(new OnRestartEvent());
    }

    @Override
    protected void onStart() {
        super.onStart();
        eventManager.fire(new OnStartEvent());
    }

    @Override
    protected void onResume() {
        super.onResume();
        eventManager.fire(new OnResumeEvent());
    }

    @Override
    protected void onPause() {
        super.onPause();
        eventManager.fire(new OnPauseEvent());
    }

    @Override
    protected void onNewIntent( Intent intent ) {
        super.onNewIntent(intent);
        eventManager.fire(new OnNewIntentEvent());
    }

    @Override
    protected void onStop() {
        try {
            eventManager.fire(new OnStopEvent());
        } finally {
            super.onStop();
        }
    }

    @Override
    protected void onDestroy() {
        try {
            eventManager.fire(new OnDestroyEvent());
        } finally {
            try {
                RoboGuice.destroyInjector(this);
            } finally {
                super.onDestroy();
            }
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        final Configuration currentConfig = getResources().getConfiguration();
        super.onConfigurationChanged(newConfig);
        eventManager.fire(new OnConfigurationChangedEvent(currentConfig, newConfig));
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        eventManager.fire(new OnActivityResultEvent(requestCode, resultCode, data));
    }

    @Override
    public Map<Key<?>, Object> getScopedObjectMap() {
        return scopedObjects;
    }

}

现在,您使用supportStartActionMode()启动ActionMode,并从库的包中导入ActionMode。

要使用SearchView,您需要执行以下操作:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:icon="@drawable/abc_ic_search"
        app:showAsAction="always|collapseActionView"
        android:title="@string/search"/>

</menu>

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search_menu, menu);

    searchMenuItem = menu.findItem(R.id.search);
    searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);

    if (searchView != null) {
        searchView.setIconifiedByDefault(false);

        SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
            public boolean onQueryTextChange(String newText) {
                return true;
            }

            public boolean onQueryTextSubmit(String query) {
                doSomething(query);
                return true;
            }
        };

        searchView.setOnQueryTextListener(queryTextListener);

    }

    return super.onCreateOptionsMenu(menu);
}

除了导入包之外,其他的东西都没有修改。

您可以在此处查看更多信息:http://developer.android.com/guide/topics/ui/actionbar.html

答案 2 :(得分:0)

4.3 sdk附带的示例看起来很有希望,但我将创建一个测试项目并尝试将我自己的应用程序转换为ActionBarCompact,看看它是否比ActionBarSherlock更好或更差!如果我成功与否,我会更新这篇文章!

答案 3 :(得分:0)

虽然提供的示例很好,但我做了另一个示例,它更接近原始的Google Navigation Drawer示例,因为它包含所有原始代码(现在旨在支持库)和格式化。 只有一些属性必须用类似的属性替换,因为它们仅从v11开始提供。

下载地址:https://github.com/GunnarBs/NavigationDrawerWithActionBarCompat

注意:这需要存在v7 appcompat库,有关详细信息,请参阅http://developer.android.com/tools/support-library/setup.html

相关问题