2017-03-17 81 views
0

我正在構建的應用使用導航抽屜android.support.v4.widget.DrawerLayoutMainActivity延伸android.support.v7.app.AppCompatActivity。我有一個父子片段被替換爲子片段。在父母片段上,默認漢堡包圖標正確地切換導航抽屜。雖然對兒童的片段,漢堡包圖標被返回箭頭圖標,即可使用這個片段漢堡包圖標未打開導航在通過單擊工具欄上的後退箭頭從子片段返回後的父級片段上的抽屜

((MainActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
toolbar = (Toolbar) (getActivity()).findViewById(R.id.toolbar); 
((MainActivity)getActivity()).setSupportActionBar(toolbar); 

兒童片段後退按鈕單擊事件上onOptionsItemSelected()處理和漢堡包圖標再次顯示父片段上onStop()取代。

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      getActivity().onBackPressed(); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 

@Override 
public void onStop() { 
    super.onStop(); 
    ((MainActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false); 
    MainActivity.toggle.syncState(); 
} 

關於父母片段漢堡包圖標現在正常顯示,但點擊它時,導航抽屜不再顯示。

任何人都可以請幫助我做錯了什麼?

以下是相關文件:

MainActivity.java

public class MainActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 
public static ActionBarDrawerToggle toggle; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    try { 
     Fragment fragment = (Fragment) ParentFragment.class.newInstance(); 

     getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content, fragment).commit(); 
    } catch (InstantiationException e) { 
     e.printStackTrace(); 
    } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 
} 

@Override 
public void onBackPressed() { 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } else { 
     super.onBackPressed(); 
    } 
} 
} 

activity_main.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" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

<include 
    layout="@layout/app_bar_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

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

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

ParentFragment.java

public class ParentFragment extends Fragment implements View.OnClickListener { 
public static ParentFragment newInstance() { 
    ParentFragment fragment = new ParentFragment(); 
    return fragment; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setHasOptionsMenu(true); 
} 

@Override 
public void onClick(View v) { 
    int i = v.getId(); 
    Fragment fragment = null; 

    if(i == R.id.child_fragment_button) { 
     fragment = new ChildFragment(); 
    } 

    if(fragment != null) { 
     FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_content, fragment) 
       .addToBackStack(null) 
       .commit(); 
    } 
} 
} 

ChildFragment.java

public class ChildFragment extends Fragment { 
private Toolbar toolbar; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setHasOptionsMenu(true); 

    // Show back button 
    ((MainActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    toolbar = (Toolbar) (getActivity()).findViewById(R.id.toolbar); 
    ((MainActivity)getActivity()).setSupportActionBar(toolbar); 
} 

@Override 
public void onStop() { 
    super.onStop(); 
    ((MainActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false); 
    MainActivity.toggle.syncState(); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      getActivity().onBackPressed(); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 
} 

回答

0

我終於想出了一個解決方案。感謝this answer

下面是我所做的更改:

ChildFragment.java

public class ChildFragment extends Fragment { 
private Toolbar toolbar; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setHasOptionsMenu(true); 

    // Show back button 

    toggle = MainActivity.toggle; 
    toggle.setDrawerIndicatorEnabled(false); 
    toggle.setToolbarNavigationClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      FragmentManager manager = getActivity().getSupportFragmentManager(); 
      manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 

      toggle.setDrawerIndicatorEnabled(true); 
     } 
    }); 

    toolbar = (Toolbar) (getActivity()).findViewById(R.id.toolbar); 
    toolbar.setNavigationIcon(((MainActivity)getActivity()).getDrawerToggleDelegate().getThemeUpIndicator()); 

} 

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

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 
} 
相關問題