2014-02-28 36 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