使用NavigationDrawer中的片段深入导航

时间:2014-02-28 07:35:29

标签: java android android-fragments navigation-drawer

所以我已经能够在我的Android应用程序中实现内置的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 Icon将执行NavigationDrawer操作并拉出要查看的片段列表,但是当向下钻取子片段时图标应该切换到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();
    }

2 个答案:

答案 0 :(得分:1)

是的,您需要根据列表项单击

向容器添加/替换片段

您可以使用interface作为活动的回调。

片段1 - &GT; HostingActivity - &GT; Fragment2。添加时请确保将片段添加到backstack。

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(); 
  } 

 }

答案 1 :(得分: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);