2

活動我嘗試在http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/抽屜式導航欄,並在Android的

上面鏈接的差給出的導航抽屜(滑軌菜單)和我是不是調用片段,我試圖調用活動。當應用程序打開時,我無法看到「導航」抽屜菜單,我只能看到打開HOME活動的操作欄。

這裏是我改變了代碼:(是否有必要有一個片段或我可以用活動我的抽屜式導航第一屏?)

mTitle = mDrawerTitle = getTitle(); 

    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); 
    navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons); 
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mDrawerList = (ListView) findViewById(R.id.left_drawer);  

    navDrawerItems = new ArrayList<NavDrawerItem>(); 

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(1, -1))); 
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(2, -1),true, "200")); 

    navMenuIcons.recycle(); 

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener()); 

    adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems); 
    mDrawerList.setAdapter(adapter); 

    getActionBar().setDisplayHomeAsUpEnabled(true); 
    getActionBar().setHomeButtonEnabled(true); 

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
      R.drawable.drawer, 
      R.string.drawer_open, 
      R.string.drawer_close 
      ) 
    { 
     public void onDrawerClosed(View view) 
     { 
      getActionBar().setTitle(mTitle); 
      invalidateOptionsMenu(); 
     } 

     public void onDrawerOpened(View drawerView) 
     { 
      getActionBar().setTitle(mDrawerTitle); 
      invalidateOptionsMenu(); 
     } 
    }; 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 

    if (savedInstanceState == null) 
    { 
     displayView(0); 
    } 
} 

private class SlideMenuClickListener implements 
ListView.OnItemClickListener 
{ 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    { 
     displayView(position); 
    } 
} 
private void displayView(int position) 
{ 
    switch (position) 
    { 
    case 0: 
     //fragment = new HomeFragment();   

     Intent intent = new Intent(this, Home.class); 
     startActivity(intent); 

     return; 

    case 1: 
     //fragment = new FindPeopleFragment(); 

     Intent intent1 = new Intent(this, Profile.class); 
     startActivity(intent1); 
     break; 

    case 2: 
     //fragment = new PhotosFragment(); 

     Intent intent2 = new Intent(this, Details.class); 
     startActivity(intent2); 
     break; 

    default: 
     break; 
    } 

    mDrawerList.setItemChecked(position, true); 
    mDrawerList.setSelection(position); 
    setTitle(navMenuTitles[position]); 
    mDrawerLayout.closeDrawer(mDrawerList); 
} 

我該如何解決這個問題,以顯示我家活動中的導航抽屜?

更新:

我甚至嘗試在這個環節給出以下選項:

How can I call one of my activity using navigation drawer ?但我仍然沒有得到導航幻燈片菜單。

+0

您實際上沒有顯示任何設置導航抽屜的代碼。例如,你似乎沒有一個'ActionBarDrawerToggle',它實際上將導航圖標放在操作欄中。 – 2014-09-04 18:15:23

+0

@ Tanis.7x - 我已經更新了我的問題。請現在看看它!謝謝! – TheDevMan 2014-09-04 18:30:54

+0

正如@Soutu已經指出的那樣:'NavigationDrawer'只能在'一個Activity'中工作,在這裏你可以交換**幾個''Fragments''。如果您正在使用'AndroidStudio',則可以在'New Project'下創建一個'NavigationDrawer' Activity,這爲您提供了一個很好的起點。 – longilong 2014-10-29 09:15:56

回答

2

你不能這麼做...... 導航抽屜是一個活動的佈局,你不能在另一個活動中顯示一個活動,你需要爲此使用一個片段!

活動是一個屏幕,您不能在另一個屏幕內顯示屏幕,但片段可能是屏幕的一個組件,並且您可以在活動的容器內對片段進行充氣,然後向用戶顯示。

如果你不想這樣做,你可以創建一個抽象活動並繼承,但是你不會在一個片段中有一個活動,你將擁有多個活動,每個活動都有你自己的導航抽屜。

+0

是的。 'NavigationDrawer'是一個佈局,而不是包含一個'FrameLayout'的意思是膨脹'碎片'不活動... – shkschneider 2014-11-03 13:46:24

1

如果您查看Android導航抽屜的示例文檔,它明確定義爲使用片段而不是活動從導航欄加載不同的「頁面」。隨着Android應用生命週期的推移,碎片更容易加載並對Android系統產生較小的影響。它們也可以很容易地置入背景中,並被其他不同的碎片取代。

話雖如此,你最好的辦法是將你的活動轉換成片段,並使用它們來加載你的應用程序所需的不同頁面。這是不是這樣一個艱鉅的任務,我會告訴你如何:

  1. 改變所有活動片段

    public class nameOfFragment extends Fragment { } 
    
  2. 使用onCreateView方法,而不是的onCreate

    public View onCreateView() { 
    
        View view = inflater.inflate(R.layout.activity_main, container, false); 
    
        //any other elements in that view need to be included here in this manner. 
        Button rate = (Button) rootView.findViewById(R.id.rate); 
    
        return view; 
    } 
    

這些更改應該足以將您的活動更改爲片段。 您需要對通過導航欄訪問的所有片段執行相同的操作。

如果您需要任何進一步的幫助,請發佈您正在使用的活動的代碼。 希望這有助於:)

+1

我沒有這樣做。我正在尋找解決方法爲什麼我想要爲您的解決方案投票。讓我試試你的解決方案。 – TheDevMan 2014-11-03 03:45:55

+0

好的,謝謝。不要猶豫,問你是否有困難。我們在這裏提供幫助。 – 2014-11-03 06:25:13

+1

謝謝..其實問題是我想找到的是有必要有一個片段,或者我可以使用導航抽屜中的第一個屏幕的活動? – TheDevMan 2014-11-03 09:00:38

1

你有代碼來建立和顯示導航欄吧?將上面的代碼放到基本活動類中。您想要訪問導航欄的每個活動都應該從該基類繼承。

谷歌的IO 2014應用程序做同樣的事情,看看他們的來源here

當您這樣做時,您需要找到一種方法來在轉換到下一個活動時維護抽屜的狀態。我不知道該怎麼做,但你可能會在IO應用程序的源代碼中找到答案。

+0

嗨,你有沒有找到任何方式來維護抽屜的狀態,因爲我使用導航的活動,但當按下我的導航按鈕以前的活動鎖。 – Pihu 2016-05-06 05:43:14

1

如果你必須有單個抽屜實例,那麼你必須去碎片。

如果除了活動之外別無選擇,請爲所有活動(例如BaseActivity)定義一個基類。在BaseActivity中 - 您可以執行必要的編碼來集成抽屜。每個活動擴展BaseActivity現在將顯示抽屜菜單(但實例將有所不同)

1

現在您需要使用工具欄。 Android已經制作了appcompat庫,以便在老版本的android中引入材質設計。你需要使用appcompat的編譯依賴。 更改style.xml

<resources> 

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
<!-- Customize your theme here. --> 
</style> 

</resources> 

然後,我們將選擇有沒有舊的行動吧。之後我們在navigation drawer和工具欄之間簽訂合同。所以我們現在不需要使用舊的操作欄。 您可以參考下面的代碼:

<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#ff6d7fe2" 
    app:contentInsetEnd="0dp" 
    app:contentInsetStart="0dp" 
    ></android.support.v7.widget.Toolbar>