向下钻取导航功能与NavigationDrawer片段 [英] Drill Down Navigation with Fragments from NavigationDrawer

查看:159
本文介绍了向下钻取导航功能与NavigationDrawer片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我已经能够实现内置NavigationDrawer在我的Andr​​oid应用程序没有任何问题,并入选当我所有的主要片段设置和工作。

在哪里我被困的是,在一些片断,我将需要添加一个项目被选中,当特征的向下钻取类型,例如,一个片段是客户的名单,所以选择一个客户,然后应推动下片段,同时还提供了一个回选项给用户(我相信这将通过home键来完成)。

我遇到的问题是,与NavigationDrawer模板home键现在是开启按钮/关闭列表,所以我似乎无法弄清楚,我应该如何改变home键是后退按钮,我不知道如果我是presenting我的下一个片段正确要么。下面是code移动到客户的细节片段选中时(注意现在我无法从客户名单片段到客户端的数据传递蛙人任何数据,我只想得到正确的导航设置在前)

  @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
        查看rootView = inflater.inflate(R.layout.fragment_clients,集装箱,FALSE);        thisActivity = this.getActivity();        clientListView =(ListView控件)rootView.findViewById(R.id.clientListView);        返回rootView;
    } //后来经过列表视图适配器已经更新数据
 clientListView.setOnItemClickListener(新AdapterView.OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
                字符串名称= dataList.get(位置).clientName;
                Log.d(消息,客户机点击是:+名称);
                片段片段=新FragmentClientDetail();
                FragmentManager经理= thisActivity.getFragmentManager();
                FragmentTransaction交易= manager.beginTransaction();
                transaction.replace(R.id.container,片段);
                transaction.addToBackStack(clientdetail);
                器transaction.commit();            }
        });

所以我的问题主要是,首先我是实际上是由新片段更换容器和正确处理导航再其次是什么,我需要在我的第二个片段来改变的,以使后退按钮作为主按钮,而不是NavigationDrawer?

修改1

所以经过Raghunandan的评论使我倒了一些额外的谷歌搜索我是能够得到ListView的正确拉起一个碎片,并且回调方法被调用,但由于某种原因,我仍然不能得到的动作条Home键从NavigationDrawer风格切换与后退箭头一个正常的导航操作栏。现在它仍然默认为NavigationDrawer类型的按钮仍然拉起导航菜单。所以基本上我想要完成的任务是,当我申请的主的片段,然后在主页图标将执行NavigationDrawer行动,并拉起片段列表查看,但随后向下钻取时,子片段首页图标应该切换到图标的只是一个背选项按钮样式。下面就是我在片段回调方法推子片段到目前为止已经试过:

  @覆盖
    公共无效callBackList(字符串fragmentName,字符串显示名){
        Log.d(消息,callbacklist称为);
        mTitle = fragmentName;
        DISPLAYTITLE =显示名;
        //推子片段的当前片段的顶部
        片段片段=新FragmentClientDetail();
        FragmentManager经理= getFragmentManager();
        FragmentTransaction交易= manager.beginTransaction();
        transaction.replace(R.id.container,片段);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        transaction.addToBackStack(NULL);
        //更改动作条样式默认与后退按钮的操作栏风格
        动作条动作条= getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(真);
        actionBar.setDisplayHomeAsUpEnabled(真);
        actionBar.setHomeButtonEnabled(真);
        actionBar.setTitle(职称);
        器transaction.commit();
        //调用以更新子片段菜单图标可能比母体片段不同
        invalidateOptionsMenu();
    }


解决方案

好了,大量的阅读,并通过Android文档挖后,我终于得到它设置,使子视图显示了插入符和作品作为后退按钮,但显示主视图时,那么导航抽屉再次显示。下面是我落得这样做的情况下,任何人试图做同样的(或者,如果有人发现有我做到这一点,有一个更好的解决问题)。

  //首先在与创建NavigationDrawerFragment类
//抽屉模板我添加了两个方法,即会调整抽屉的看法
//改变操作栏显示插入符号
    公共静态无效showSubActionBar(){
        mDrawerToggle.setDrawerIndicatorEnabled(假);
    }
    //改变操作栏显示导航抽屉图标
    公共静态无效showNavActionBar(){
        mDrawerToggle.setDrawerIndicatorEnabled(真);
    }//然后在我MainActivity类别我添加使用计数器小法
//以确定用户是否在子级(因为一些
//片段最多可有4个子水平),或回到主视图
//并更新动作条
公共无效subLevelCounter(INT计数器){       levelCounter = levelCounter +计数器;
        如果(levelCounter大于0){
            NavigationDrawerFragment.showSubActionBar();
        }
        其他{
            NavigationDrawerFragment.showNavActionBar();        }
        invalidateOptionsMenu();    }//所以现在当后退按钮是可见的,pressed我更新的计数器
//并调用onBack pressed方法
如果(item.getItemId()== android.R.id.home){
            ((MainActivity)getActivity())subLevelCounter(-1)。
            getActivity()onBack pressed()。
            返回true;
        }//当我向下钻取我只是调用前添加此行
//重版方法来执行导航
                ((MainActivity)thisActivity).subLevelCounter(1);

So I've been able to implement the built in NavigationDrawer in my Android app without any issues, and all my main Fragments are setup and work when selected.

Where I'm stuck is that in some fragments I will need to add a drill down type of feature when an item is selected, for example one Fragment is a list of customers, so selecting a customer should then push to the next fragment while still providing a back option to the user (I believe this would be done through the home button).

The problem I'm having is that with the NavigationDrawer template the home button is now the button for opening/closing the list, so I can't seem to figure out how I'm supposed to change the home button to be a back button, and I'm not sure if I'm presenting my next fragment correctly either. Below is the code for moving to a customers detail fragment when selected (note right now I'm not passing any data from the client list fragment to the client data frogmen yet, I just want to get the navigation setup properly first):

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

            }
        });

So my questions mainly are, first am I actually handling the navigation correctly by replacing the container with the new fragment and then secondly what do I need to change in my second fragment to enable the back button as the home button instead of the NavigationDrawer?

EDIT 1

So after Raghunandan's comment lead me down some extra google searching I was able to get the ListView to pull up the next fragment correctly, and the callback method is being called but for some reason I still can't get the ActionBar Home button to switch from the NavigationDrawer style to a the normal navigation action bar with the back arrow. Right now it still defaults as the NavigationDrawer type of button that still pulls up the navigation menu. So basically what I want to accomplish is that when I'm the "main" fragments of the application then the Home Icon will perform the NavigationDrawer action and pull up the list of fragments to view, but then when drilling down sub-fragments the Home Icon should switch to just a back option button style of Icon. Here's what I've tried so far with the callback method in the fragment to push the sub fragments:

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

解决方案

Okay, so after a lot of reading and digging through the android docs I finally got it setup so that the subviews show the up caret and works as the back button, but when the main view is displayed then the navigation drawer is shown again. Here's what I ended up doing in case anyone else is trying to do the same (or if anyone finds a problem with how I did this and have a better solution).

//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);

这篇关于向下钻取导航功能与NavigationDrawer片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆