【发布时间】: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