2015-04-23 37 views
0




我想知道下一個 - 當片段管理器刪除並添加新片段時 - 導航切換按鈕不響應水龍頭。Android片段管理器更改導航抽屜的行爲

詳細信息
1.我在我的項目中有導航抽屜。它是通過標準的Android Studio項目(使用導航抽屜)創建的。
2.我使用片段和片段管理器來處理屏幕的內容。它發生在onNavigationDrawerItemSelected()中。
3.這是實現onCreateOptionsMenu()在MainActivity的:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    if (!mNavigationDrawerFragment.isDrawerOpen()) { 
     // Only show items in the action bar relevant to this screen 
     // if the drawer is not showing. Otherwise, let the drawer 
     // decide what to show in the action bar. 
     getMenuInflater().inflate(R.menu.main_activity, menu); 
     MenuItem item = menu.findItem(R.id.right_item); 
     getSupportActionBar().setTitle(mTitle); 
     return true; 
    } 
    return super.onCreateOptionsMenu(menu); 
} 

4.當我使用FragmentManager.replace() - 一切正常。 當我使用刪除(),然後添加() - 導航抽屜切換按鈕不響應水龍頭。我仍然可以按它 - 但抽屜目前未打開。代碼FragmentManager:

.beginTransaction() 
.remove(fragmentManager.getFragments().get(0)) 
.commit(); 

.beginTransaction() 
.add(container, replacingFragment) 
.commit(); 

注 - 我不覆蓋片段類任何菜單的方法。
可能是我使用錯誤的代碼來處理碎片?

我更喜歡刪除/添加而不是替換的原因 - 當用戶在導航抽屜中選擇使用相同片段的項目時 - 應該更新片段的內容;並且replace()不會將其分段。

但是,對我來說,真的很奇怪,片段管理器中的操作會破壞切換按鈕。

更多。在這種情況下,onOptionsItemSelected()在MainActivity中調用,不會在NavigationDrawerFragment片段中調用。

編輯
XML文件是標準的,由Android工作室創建

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. --> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

    <!-- As the main content view, the view below consumes the entire 
     space available using match_parent in both dimensions. --> 
    <FrameLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@drawable/bg1" /> 

    <!-- android:layout_gravity="start" tells DrawerLayout to treat 
     this as a sliding drawer on the left side for left-to-right 
     languages and on the right side for right-to-left languages. 
     If you're not building against API 17 or higher, use 
     android:layout_gravity="left" instead. --> 
    <!-- The drawer is given a fixed width in dp and extends the full height of 
     the container. --> 
    <fragment 
     android:id="@+id/navigation_drawer" 
     android:layout_width="@dimen/navigation_drawer_width" 
     android:layout_height="match_parent" 
     android:layout_gravity="start|left" 
     android:name="com.datingappanrdoid.NavigationDrawerFragment" 
     tools:layout="@layout/fragment_navigation_drawer" > 
    </fragment> 
</android.support.v4.widget.DrawerLayout> 

步驟來重現奇怪的行爲:
1.在Android Studio中創建新的項目 - 選擇 「帶抽屜式導航工程」 。
2.將onNavigationDrawerItemSelected方法從replace()更改爲remove()和add()。這裏是我的意思:

@Override 
    public void onNavigationDrawerItemSelected(int position) { 
     // update the main content by replacing fragments 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
//  fragmentManager.beginTransaction() 
//    .replace(R.id.container, PlaceholderFragment.newInstance(position + 1)) 
//    .commit(); 

     if (tempFragment != null) 
      tempFragment.setHasOptionsMenu(false); 
     fragmentManager.beginTransaction() 
       .remove(fragmentManager.getFragments().get(0)) 
       .commit(); 

     tempFragment = PlaceholderFragment.newInstance(position + 1); 
     tempFragment.setHasOptionsMenu(true); 

     fragmentManager.beginTransaction() 
       .add(R.id.container, tempFragment) 
       .commit(); 
    } 

我還增加了hasOptionsMenu的用法,正如它在評論中的建議。
3.準備就緒 - 現在導航切換按鈕不適用於水龍頭(但仍然是挖掘)。

仍然沒有理解,爲什麼發生這種情況。

+0

能你向我們展示你的XML文件?一個來自您的抽屜定義 – xanexpt

+0

添加xml的活動,但它的標準 –

+0

雅,我通常不使用導航抽屜中的片段,我只是使用正常的佈局(像scrool視圖),從來沒有遇到過問題 – xanexpt

回答

0

當您致電add()remove()時,會添加/刪除新片段。如果我們想給navigation drawer options這個片段,我們需要調用setHasOptionsMenu(boolean value)從代碼

我們將後add()打電話setHasOptionsMenu(true)並應將remove()

調用setHasOptionsMenu(false)這將解決你的問題

+0

可能是我做錯了什麼,但它不起作用。稍後我會把測試項目放在某個地方來確認它。 –

+0

將STR添加到初始答案中,仍然沒有結果。 –