【问题标题】:Drill Down Navigation with Fragments from NavigationDrawer使用 NavigationDrawer 中的片段向下钻取导航
【发布时间】:2014-02-28 07:35:28
【问题描述】:

因此,我已经能够在我的 Android 应用程序中实现内置 NavigationDrawer,没有任何问题,并且我的所有主要 Fragment 都已设置并在选择时工作。

我遇到的问题是,在某些片段中,我需要在选择项目时添加向下钻取类型的功能,例如,一个片段是客户列表,因此选择一个客户应该推送到下一个片段,同时仍向用户提供返回选项(我相信这将通过主页按钮完成)。

我遇到的问题是,使用 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();

            }
        });

所以我的问题主要是,首先我是否通过用新片段替换容器来正确处理导航,其次我需要在我的第二个片段中进行哪些更改以启用后退按钮作为主页按钮而不是导航抽屉?

编辑 1

因此,在 Raghunandan 的评论引导我进行了一些额外的谷歌搜索之后,我能够让 ListView 正确拉出下一个片段,并且正在调用回调方法,但由于某种原因,我仍然无法获得 ActionBar 主页按钮从 NavigationDrawer 样式切换到带有后退箭头的普通导航操作栏。现在它仍然默认为仍然拉出导航菜单的 NavigationDrawer 类型的按钮。所以基本上我想要完成的是,当我是应用程序的“主要”片段时,主页图标将执行 NavigationDrawer 操作并拉出要查看的片段列表,但是当向下钻取子片段时,主页Icon 应该只切换到 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();
    }

【问题讨论】:

    标签: java android android-fragments navigation-drawer


    【解决方案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(); 
      } 
    
     }
    

    【讨论】:

    • 所以这有助于我正确设置推送,但 ActionBar 上的主页图标仍显示为 NavigationDrawer 样式,而不是显示后退按钮并在按下时向后移动。
    • @Sal 不,不会。您可以查看文档。这个建议是正确的
    • @Sal 顶部建议的两行可以在片段中使用。这应该做的工作。 developer.android.com/training/implementing-navigation/…。请参阅添加导航
    【解决方案2】:

    好的,经过大量阅读和挖掘 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);
    

    【讨论】:

    • 为了不必每次添加新片段时都调用 subLevelCounter(),您可以设置一个 OnBackStackChangedListener,它会在您每次添加新片段时通知您。在回调中,您可以访问 getSupportFragmentManager().getBackStackEntryCount() ,它会为您提供添加到后台堆栈中的片段数。当数字为 0 时设置您的抽屉图标,或者当数字较大时设置后退箭头。请记住在加载新片段时包含 .addToBackStack()。
    猜你喜欢
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 2014-05-22
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多