2014-02-28 60 views
2

因此,我已經能夠在我的Android應用程序中實現內置的NavigationDrawer而沒有任何問題,並且所有主要碎片都在選定時安裝並工作。從NavigationDrawer向下鑽取導航

我被卡住的地方在於,在某些片段中,我需要在選擇某個項目時添加向下鑽取功能,例如,一個片段是客戶列表,因此選擇客戶應推入下一個片段,同時還向用戶提供後退選項(我相信這將通過主頁按鈕完成)。

我遇到的問題是,與NavigationDrawer模板的主頁按鈕現在是打開/關閉列表的按鈕,所以我似乎無法弄清楚我應該如何將主頁按鈕更改爲作爲後退按鈕,我不確定是否正確呈現我的下一個片段。下面是選擇時移動到客戶細節片段的代碼(請注意,現在我沒有將客戶端列表片段中的任何數據傳遞給客戶端數據蛙人,我只想先正確地導航設置):

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_clients, container, false); 

     thisActivity = this.getActivity(); 

     clientListView = (ListView)rootView.findViewById(R.id.clientListView); 



     return rootView; 
    } 

//later after the list view adapter has been updated with data 
clientListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       String name = dataList.get(position).clientName; 
       Log.d("message", "the client clicked on is: " + name); 
       Fragment fragment = new FragmentClientDetail(); 
       FragmentManager manager = thisActivity.getFragmentManager(); 
       FragmentTransaction transaction = manager.beginTransaction(); 
       transaction.replace(R.id.container, fragment); 
       transaction.addToBackStack("clientdetail"); 
       transaction.commit(); 

      } 
     }); 

所以我的問題主要是,第一是我居然用新片段替換容器中,再其次是什麼,我需要在我的第二個片段來改變,使後退按鈕作爲主按鈕,而不是正確地處理導航NavigationDrawer的?

編輯1

所以經過Raghunandan的評論使我下了一些額外的谷歌搜索我是能夠得到ListView的正確拉起一個片段,並且回調方法被調用,但由於某種原因,我仍然無法使用後退箭頭將ActionBar Home按鈕從NavigationDrawer樣式切換到正常的導航操作欄。現在它仍然默認爲仍然拉起導航菜單的NavigationDrawer類型的按鈕。所以基本上我想要完成的是,當我是應用程序的「主要」片段時,Home圖標將執行NavigationDrawer操作並拉出要查看的片段列表,但是當向下鑽取子片段時,Home圖標應該切換到Icon的後退選項按鈕樣式。這是我到目前爲止已經有回調方法在片段試圖推子片段:

@Override 
    public void callBackList(String fragmentName, String displayName) { 
     Log.d("message", "callbacklist called"); 
     mTitle = fragmentName; 
     displayTitle = displayName; 
     //Push child fragment on top of current fragment 
     Fragment fragment = new FragmentClientDetail(); 
     FragmentManager manager = getFragmentManager(); 
     FragmentTransaction transaction = manager.beginTransaction(); 
     transaction.replace(R.id.container, fragment); 
     transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
     transaction.addToBackStack(null); 
     //Change action bar style to default action bar style with back button 
     ActionBar actionBar = getActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
     actionBar.setDisplayShowTitleEnabled(true); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     actionBar.setHomeButtonEnabled(true); 
     actionBar.setTitle(title); 
     transaction.commit(); 
     //call to update menu icons for child fragments that may be different than parent fragment 
     invalidateOptionsMenu(); 
    } 

回答

1

好了,經過大量的閱讀和通過Android文檔挖我終於得到它的設置,使得子視圖顯示上面的插入符號並作爲後退按鈕,但當顯示主視圖時,將再次顯示導航抽屜。如果有人試圖做同樣的事情(或者如果有人發現我如何做到這一點,並有更好的解決方案),我最終做了什麼。

//First in the NavigationDrawerFragment class that is created with the 
//Drawer template I added two methods, that will adjust the drawer's view 
//change action bar to show up caret 
    public static void showSubActionBar() { 
     mDrawerToggle.setDrawerIndicatorEnabled(false); 
    } 
    //change action bar to show navigation drawer icon 
    public static void showNavActionBar() { 
     mDrawerToggle.setDrawerIndicatorEnabled(true); 
    } 

//Then in my MainActivity class I add a small method that uses a counter 
// to determine if a user is in a sub level (since some 
//fragments may have up to 4 sub levels) or back on the main view 
//and updates the ActionBar 
public void subLevelCounter(int counter) { 

     levelCounter = levelCounter + counter; 
     if (levelCounter > 0) { 
      NavigationDrawerFragment.showSubActionBar(); 
     } 
     else { 
      NavigationDrawerFragment.showNavActionBar(); 

     } 
     invalidateOptionsMenu(); 

    } 

//So now when the back button is visible and pressed I updated the counter 
//and call the onBackPressed method 
if (item.getItemId() == android.R.id.home) { 
      ((MainActivity) getActivity()).subLevelCounter(-1); 
      getActivity().onBackPressed(); 
      return true; 
     } 

//And when I'm drilling down I just add this line before calling the 
//backlist method to perform the navigation 
       ((MainActivity) thisActivity).subLevelCounter(1); 
+0

爲了不必在每次添加新片段時調用subLevelCounter(),都可以設置OnBackStackChangedListener,每次添加新片段時都會通知您。 在回調函數中,您可以訪問getSupportFragmentManager()。getBackStackEntryCount(),它可以爲您提供在堆棧中添加的片段數量。數字爲0時設置抽屜圖標,或數字較大時設置抽屜圖標。 記得在加載新片段時包含.addToBackStack()。 – juanmeanwhile

1

是的,你需要添加/替換片段基於列表項的容器點擊

你可以使用界面作爲活動的回調。

Fragment1 - > HostingActivity - > Fragment2。添加確保你添加碎片到後臺堆棧。

http://developer.android.com/training/basics/fragments/communicating.html

您可以使用

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

更多信息@

http://developer.android.com/training/implementing-navigation/ancestral.html

例子:

public interface ListItemCallback 
{ 
     public void callBackList() 
} 

ŧ母雞在片段

ListItemCallback mCallback; 
@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 

    // This makes sure that the container activity has implemented 
    // the callback interface. If not, it throws an exception 
    try { 
     mCallback = (ListItemCallback) activity; 
    } catch (ClassCastException e) { 
     throw new ClassCastException(activity.toString() 
       + " must implement ListItemCallback"); 
    } 
} 

然後

clientListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      String name = dataList.get(position).clientName; 
      Log.d("message", "the client clicked on is: " + name); 
      mCallback.callBackList(); 

     } 
    }); 

然後在活動

public class MainActivity extends Activity implements ListItemCallback 
{ 
    ...// rest of the code 
    @Override 
    public void callBackList(String name) 
    { 
     Fragment fragment = new FragmentClientDetail(); 
     FragmentManager manager = getFragmentManager(); 
     FragmentTransaction transaction = manager.beginTransaction(); 
     transaction.replace(R.id.container, fragment); 
     transaction.addToBackStack("clientdetail"); 
     transaction.commit(); 
    } 

} 
+0

所以這幫助我正確地得到推動的設置,但在主頁圖標上的動作條仍然顯示爲NavigationDrawer風格,而不是顯示一個後退按鈕,按下時搬回來。 –

+0

@Sal不,它不會。你可以檢查文檔。建議其正確的一個 – Raghunandan

+0

@Sal在頂部建議的兩條線可用於片段。應該完成這項工作。 http://developer.android.com/training/implementing-navigation/ancestral.html。請參閱add up navigation – Raghunandan