2013-10-18 72 views
42

是否有可能在android中使用導航抽屜,而不是更新片段,我想在應用程序內作爲我的導航方式在活動之間切換。導航抽屜切換活動,而不是片段

+0

你想要什麼? –

+0

我想要的是使用一個導航抽屜,它可以用於我所有的活動來切換活動。 –

+0

你能告訴我你的設計嗎?爲什麼你只需要導航抽屜? –

回答

43

是的,這是可能的 - 這是我爲我的應用程序做的。我已經完成了一些活動,而不是將它們全部轉換爲碎片,我想調整導航抽屜以適用於所有這些活動。不幸的是,這不是一個快速的解決方法,所以如果你有使用碎片的選擇,我會去那裏。但不管我是怎麼做到的:

比方說,我有2個活動,我想要兩個導航抽屜。在每個版本的layout.xml中,我指定DrawerLayout和適當的ListView來保存我的導航選項。實質上,每次在活動之間切換時都會創建導航抽屜,從而使外觀保持不變。爲了讓生活變得更輕鬆,我採用了設置導航抽屜並將它們放入自己的班級所需的常用方法:NavigationDrawerSetup.java。這樣,我的活動,可以使用相同的自定義適配器等

在這個NavigationDrawerSetup.java類,我有以下幾點:

  • configureDrawer() - 這樹立ActionBarActionBarDrawerToggle,以及所需的聽衆
  • 我的自定義陣列適配器(用於填充列表中的導航選項)
  • selectOptions()方法,該方法處理抽屜項目點擊

當您在其中一項活動中設置導航抽屜時,只需創建一個新的NavigationDrawerSetup對象並傳遞所需的佈局參數(如DrawerLayout,ListView等)。然後你會打電話configureDrawer()

 navigationDrawer = new NavigationDrawerSetup(mDrawerView, mDrawerLayout, 
      mDrawerList, actionBar, mNavOptions, currentActivity); 

    navigationDrawer.configureDrawer(); 

currentActivity由於導航抽屜被綁定到你的活動傳遞英寸您將有當您設置ActionBarDrawerToggle使用它:

​​

您還需要使用currentActivity設置自定義Adapter時:

至於如何通過導航抽屜活動之間進行切換,你可以建立新的意圖你的選擇信息()方法中:

private void selectItem(int position) { 

    // Handle Navigation Options 
    Intent intent; 
    switch (position) { 
     case 0: 
      intent = new Intent(currentActivity, NewActivity.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      currentActivity.startActivity(intent); 
      break; 
     case 1: 
      // etc. 
    } 

只要確保你的新Activity也有抽屜式導航欄的設置,它應該顯示。

有很多事情可以根據自己的需要來定製這種方法,但這是我如何做到的一般結構。希望這可以幫助!

+1

稍後會檢查,因爲它似乎是一個很好的建議。當涉及新片段的過渡動畫時,可能很難仿效使用帶有片段的導航抽屜,因爲去新的活動看起來不同於加載新的片段。 –

+14

你是如何管理活動堆棧的? – AKh

+0

@David Crozier你在那裏?我得到Stucked創建一個導航抽屜,如果你想要,我可以告訴你我的代碼 – bipin

2

作爲@ David-Crozier指出的解決方案的一個小改進,爲了避免兩個動畫重疊(關閉NavigationDrawer並開始一個新的活動),您可以在方法中包含一些延遲在iosched應​​用v2014:

private void onNavDrawerItemClicked(final int itemId) { 
    if (itemId == getSelfNavDrawerItem()) { 
     mDrawerLayout.closeDrawer(GravityCompat.START); 
     return; 
    } 

    if (isSpecialItem(itemId)) { 
     goToNavDrawerItem(itemId); 
    } else { 
     // launch the target Activity after a short delay, to allow the close animation to play 
     mHandler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       goToNavDrawerItem(itemId); 
      } 
     }, NAVDRAWER_LAUNCH_DELAY); 

     // change the active item on the list so the user can see the item changed 
     setSelectedNavDrawerItem(itemId); 
     // fade out the main content 
     View mainContent = findViewById(R.id.main_content); 
     if (mainContent != null) { 
      mainContent.animate().alpha(0).setDuration(MAIN_CONTENT_FADEOUT_DURATION); 
     } 
    } 

    mDrawerLayout.closeDrawer(GravityCompat.START); 
} 

下面的鏈接以供參考:https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/BaseActivity.java

8

你需要一個BaseDrawerActivity實現了這個抽屜式導航然後在每個需要抽屜式導航活動延長BaseDrawerActivity

首先創建BaseDrawerActivity.java

​​

然後在res/layout文件夾中創建activity_base_drawer.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:openDrawer="start"> 

    <include layout="@layout/app_bar_home"/> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:headerLayout="@layout/nav_header_home" 
     app:menu="@menu/activity_home_drawer" /> 

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

其中@layout/app_bar_home是:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <FrameLayout android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</android.support.design.widget.CoordinatorLayout> 

下一頁輸入您的活動,將有抽屜式導航欄如CameraActivity.java

public class CameraActivity extends BaseDrawerActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getLayoutInflater().inflate(R.layout.activity_camera, frameLayout); 

     /** 
     * Setting title 
     */ 
     setTitle("Camera"); 

    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     // to check current activity in the navigation drawer 
     navigationView.getMenu().getItem(0).setChecked(true); 
    } 
} 

哪裏R.layout.activity_camera是你CameraActivity.java佈局。

然後創建其他活動一樣GalleryActivity.java等,將有抽屜式導航:

public class GalleryActivity extends BaseDrawerActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getLayoutInflater().inflate(R.layout.activity_gallery, frameLayout); 

     // Setting title 
     setTitle("Gallery"); 

    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     navigationView.getMenu().getItem(1).setChecked(true); 
    } 
} 
+1

你如何管理活動堆棧...所有的活動將保持堆棧。當你按下返回按鈕時,所有的活動都會一一關閉 –

+1

你是如何初始化frameLayout的? (關於行:getLayoutInflater()。inflate(R.layout.activity_gallery,frameLayout);) –