2013-07-25 74 views
9

我開始使用NavigationDrawerActionBarSherlock,收到了較好的效果,但我公司批准這個開源不太可能很快配備了工作,所以我的工作切換到ActionBarCompat。安卓:NavigationDrawer和ActionBarCompat

ActionBarCompat昨天剛剛正式發佈(2013年7月24日)。有沒有人得到了兩個互相良好的工作?我希望我能回答我自己的問題,但看看是否有人已經得到這個工作。比賽已經開始! :-)

的YouTube上ActionBarCompat的版本: https://www.youtube.com/watch?v=6TGgYqfJnyc


UPDATE(工作代碼,是的!): 我已經得到了從谷歌樣品NavigationDrawer應用程序轉換爲使用ActionBarCompat和它工作正常。你可以在這裏找到它作爲參考或開始到項目中: https://github.com/bcrider/NavigationDrawerActionBarCompat

2.x版看起來比它ActionBarSherlock的方式一樣甚至更好,但我會和ActionBarCompat工作很多多看如果我更喜歡它。


+0

我是否渴望使用ActionBar compat和Nav抽屜瞭解您的使用體驗,我也使用過Nav抽屜和ABS,並且工作正常。 –

+0

工作正常,菜單項除外。檢查正確的語法這個答案http://stackoverflow.com/questions/17913311/is-it-possible-to-have-a-action-bar-menu-using-the-appcompat-library – Zeezer

+0

Appcompat抽屜導航非常緩慢即使在我的M860摩托羅拉,我也試過一些自定義抽屜庫,它們的性能要好得多 – Singgum3b

回答

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

0

附帶4.3 SDK中的樣品看好,但我要創建一個測試項目,並嘗試自己的應用程序轉換爲ActionBarCompact,看看它的功能比ActionBarSherlock更好或更壞!如果我成功了,我會更新這篇文章!

+2

終於能夠回到這裏,我發現整合過程中缺少一步。該視頻沒有明確提及(或者對我來說足夠明顯)將該項目導入爲圖書館。一旦我這樣做了,很多問題就消失了。我只是將。\ sdk \ extras \ android \ support \ v7 \ appcompat導入到我的工作區中,然後將其作爲庫添加到我的主項目中。就像視頻似乎只是複製jar文件是不夠的。 –

2

我將我的應用程序從ActionBarSherlock轉換爲ActionBarCompat昨天。我遇到了一些問題,但沒有太嚴重。

我有一些意見:

要更新的主題,我只是需要重寫「大偵探」到「程序兼容性」。例如,我不是從@ 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,使用正常的菜單項,但使用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。

要使用搜索查看,你需要做的是這樣的:

<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

+0

非常好!我很高興你提到了搜索視圖,因爲我將在下週開始使用它,之後我(有希望)將ABS轉換爲ABC。 –

+0

好吧,我在使用SearchView時遇到問題。看看這裏:http://stackoverflow.com/questions/17898282/searchview-taking-all-the-space-in-the-new-actionbarcompat –

+0

另一個障礙:ActionBarCompat並在MapActivity上使用它。你有沒有機會通過這個路障?我在這裏發佈了一個問題:http://stackoverflow.com/questions/17980503/actionbarcompat-and-mapactivity –

0

雖然提供的示例很好,但我又創建了另一個更接近原始Google Navigation Drawer示例的示例,因爲它包含所有原始代碼(現在旨在支持庫)和格式。 只有一些屬性必須替換爲類似的屬性,因爲它們只能從v11開始提供。

下載:https://github.com/GunnarBs/NavigationDrawerWithActionBarCompat

注:這需要V7程序兼容性庫在場,看到http://developer.android.com/tools/support-library/setup.html瞭解詳情。