[英]How can I combine my BottomNavigationView with Navigation Drawer
因为我在我的应用程序中同时使用了 BottomNavigationView 和 NavigationDrawer,但是当我将主页按钮放在它们两个(BottomNavigationView 和 NavigationDrawer)中时出现问题,导航抽屉的按钮运行良好,但是当从 BottomNavigationView 使用 Home(仪表板)时出现问题当我从BottomNavigationView选择Home后单击任何卡片查看cardView时,应用程序崩溃但当我从NavigationDrawer打开它时运行良好。
这是错误:
java.lang.IllegalArgumentException: 导航目标 com.appsnipp.admin:id/action_nav_home_to_nav_resource 对这个 NavController 是未知的
应用程序图片
导航抽屉导航抽屉这里是我的代码!!
导航文件
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.appsnipp.admin.Navigation_Profile.ui.dashboard.DashBoardFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_dashboard" >
<action
android:id="@+id/action_nav_home_to_nav_account2"
app:destination="@id/nav_account" />
<action
android:id="@+id/action_nav_home_to_navB_building2"
app:destination="@id/navB_building" />
<action
android:id="@+id/action_nav_home_to_navB_notice3"
app:destination="@id/navB_notice" />
<action
android:id="@+id/action_nav_home_to_nav_resource"
app:destination="@id/nav_resource" />
<action
android:id="@+id/action_nav_home_to_nav_complain"
app:destination="@id/nav_complain" />
<action
android:id="@+id/action_nav_home_to_navB_profile"
app:destination="@id/navB_profile" />
<action
android:id="@+id/action_nav_home_to_nav_visitor"
app:destination="@id/nav_visitor" />
<action
android:id="@+id/action_nav_home_to_nav_election"
app:destination="@id/nav_election" />
<action
android:id="@+id/action_nav_home_to_nav_document"
app:destination="@id/nav_document" />
<action
android:id="@+id/action_nav_home_to_nav_event"
app:destination="@id/nav_event" />
<action
android:id="@+id/action_nav_home_to_nav_member"
app:destination="@id/nav_member" />
</fragment>
<fragment
android:id="@+id/navB_home"
android:name="com.appsnipp.admin.Navigation_Profile.ui.dashboard.DashBoardFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_dashboard" />
<fragment
android:id="@+id/navB_building"
android:name="com.appsnipp.admin.Navigation_Profile.ui.buildingdetails.BuildingDetailsFragment"
android:label="@string/menu_buiding"
tools:layout="@layout/fragment_buildingdetails" />
<fragment
android:id="@+id/navB_notice"
android:name="com.appsnipp.admin.Navigation_Profile.ui.noticeboard.NoticeBoardFragment"
android:label="@string/menu_notice"
tools:layout="@layout/fragment_notice" />
<fragment
android:id="@+id/navB_profile"
android:name="com.appsnipp.admin.Navigation_Profile.ui.profile.ProfileFragment"
android:label="Profile"
tools:layout="@layout/fragment_profile" />
<fragment
android:id="@+id/nav_account"
android:name="com.appsnipp.admin.Navigation_Profile.ui.account.AccountFragment"
android:label="@string/menu_account"
tools:layout="@layout/fragment_account" />
<fragment
android:id="@+id/nav_member"
android:name="com.appsnipp.admin.Navigation_Profile.ui.members.MembersFragment"
android:label="@string/menu_member"
tools:layout="@layout/fragment_member" />
<fragment
android:id="@+id/nav_election"
android:name="com.appsnipp.admin.Navigation_Profile.ui.electionandpoll.ElectionFragment"
android:label="@string/menu_election"
tools:layout="@layout/fragment_election" />
<fragment
android:id="@+id/nav_document"
android:name="com.appsnipp.admin.Navigation_Profile.ui.document.DocumentFragment"
android:label="@string/menu_document"
tools:layout="@layout/fragment_document" />
<fragment
android:id="@+id/nav_resource"
android:name="com.appsnipp.admin.Navigation_Profile.ui.resource.ResourceFragment"
android:label="@string/menu_resource"
tools:layout="@layout/fragment_resources" />
<fragment
android:id="@+id/nav_visitor"
android:name="com.appsnipp.admin.Navigation_Profile.ui.visitor.VisitorFragment"
android:label="@string/menu_visitor"
tools:layout="@layout/fragment_visitor" />
<fragment
android:id="@+id/nav_event"
android:name="com.appsnipp.admin.Navigation_Profile.ui.event.EventFragment"
android:label="@string/menu_event"
tools:layout="@layout/fragment_event" />
<fragment
android:id="@+id/nav_complain"
android:name="com.appsnipp.admin.Navigation_Profile.ui.complain.ComplainFragment"
android:label="@string/menu_complain"
tools:layout="@layout/fragment_complain" />
</navigation>
底部导航菜单文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navB_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="Home" />
<item
android:id="@+id/navB_building"
android:icon="@drawable/icon_building_black"
android:title="Building Details" />
<item
android:id="@+id/navB_notice"
android:icon="@drawable/icon_notice_black"
android:title="NoticeBoard" />
<item
android:id="@+id/navB_profile"
android:icon="@drawable/ic_person_outline_black_24dp"
android:title="Profile" />
</menu>
NavigationDrawer 菜单文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/icon_home"
android:title="@string/menu_home"
/>
<item
android:id="@+id/nav_account"
android:icon="@drawable/icon_account"
android:title="@string/menu_account" />
<item
android:id="@+id/nav_member"
android:icon="@drawable/icon_member"
android:title="@string/menu_member" />
<item
android:id="@+id/nav_election"
android:icon="@drawable/icon_election"
android:title="@string/menu_election" />
<item
android:id="@+id/nav_document"
android:icon="@drawable/icon_document"
android:title="@string/menu_document" />
<item
android:id="@+id/nav_resource"
android:icon="@drawable/icon_resource"
android:title="@string/menu_resource" />
<item
android:id="@+id/nav_visitor"
android:icon="@drawable/icon_visitor"
android:title="@string/menu_visitor" />
<item
android:id="@+id/nav_event"
android:icon="@drawable/icon_event"
android:title="@string/menu_event" />
<item
android:id="@+id/nav_complain"
android:icon="@drawable/icon_complain"
android:title="@string/menu_complain" />
</group>
</menu>
NavigationDrawerActivity,BottomNavigationView 和 NavigationDrawer 的代码都在这里完成
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_);
...
DrawerLayout drawer = findViewById(R.id.drawer_layout);
navigationView =findViewById(R.id.nav_view);
bottomNavigationView = findViewById(R.id.nav_view_bottom);
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomNavigationView.getLayoutParams();
layoutParams.setBehavior(new BottomNavigationBehaviour());
bottomNavigationView.setSelectedItemId(R.id.navB_home);
navigationView=findViewById(R.id.nav_view);
navigationView.setCheckedItem(R.id.nav_home);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_account, R.id.nav_member,
R.id.nav_election, R.id.nav_document, R.id.nav_resource,R.id.navB_home,
R.id.nav_complain,R.id.navB_notice,
R.id.navB_building,R.id.navB_profile,
R.id.nav_event,R.id.nav_visitor)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(bottomNavigationView,navController);
NavigationUI.setupWithNavController(navigationView, navController);
}
主页片段文件
public class DashBoardFragment extends Fragment {
CardView c1,c2,c3,c4,c5,c6,c7,c8;
FragmentManager manager;
NavigationView navigationView;
private DashBoardViewModel dashBoardViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
dashBoardViewModel =
ViewModelProviders.of(this).get(DashBoardViewModel.class);
View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
navigationView =root.findViewById(R.id.nav_view);
manager=getFragmentManager();
c1=(CardView) root.findViewById(R.id.card_account);
c2=(CardView) root.findViewById(R.id.card_resource);
c3=(CardView) root.findViewById(R.id.card_document);
c4=(CardView) root.findViewById(R.id.card_member);
c5=(CardView) root.findViewById(R.id.card_event);
c6=(CardView) root.findViewById(R.id.card_election);
c7=(CardView) root.findViewById(R.id.card_complain);
c8=(CardView) root.findViewById(R.id.card_visitor);
c1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(R.id.action_nav_home_to_nav_account2);
}
});
c2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(R.id.action_nav_home_to_nav_resource);
}
});
c3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(R.id.action_nav_home_to_nav_document);
}
});
c4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(R.id.action_nav_home_to_nav_member);
}
});
c5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(R.id.action_nav_home_to_nav_event);
}
});
c6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(R.id.action_nav_home_to_nav_election);
}
});
c7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(R.id.action_nav_home_to_nav_complain);
}
});
c8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(R.id.action_nav_home_to_nav_visitor);
}
});
return root;
}
}
如果需要任何其他文件代码,请告诉我。
提前致谢!
所以问题是我在导航文件中创建了一个重复的片段作为nav_home
navB_home
但是在导航时我总是从nav_home
导航所以为了克服这个问题我从导航和菜单中删除了navB_home
并在那里充气了nav_home
(BottomNavigationView) 所以现在两个的NavigationView和BottomNavigationView具有相同片段导航如nav_home
。
导航文件
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@id/nav_home">
<fragment
android:id="@+id/nav_home" android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.dashboard.DashBoardFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_dashboard" >
<action
android:id="@+id/action_nav_home_to_nav_account2"
app:destination="@id/nav_account" />
<action
android:id="@+id/action_nav_home_to_navB_building2"
app:destination="@id/navB_building" />
<action
android:id="@+id/action_nav_home_to_navB_notice3"
app:destination="@id/navB_notice" />
<action
android:id="@+id/action_nav_home_to_nav_resource"
app:destination="@id/nav_resource" />
<action
android:id="@+id/action_nav_home_to_nav_complain"
app:destination="@id/nav_complain" />
<action
android:id="@+id/action_nav_home_to_navB_profile"
app:destination="@id/navB_profile" />
<action
android:id="@+id/action_nav_home_to_nav_visitor"
app:destination="@id/nav_visitor" />
<action
android:id="@+id/action_nav_home_to_nav_election"
app:destination="@id/nav_election" />
<action
android:id="@+id/action_nav_home_to_nav_document"
app:destination="@id/nav_document" />
<action
android:id="@+id/action_nav_home_to_nav_event"
app:destination="@id/nav_event" />
<action
android:id="@+id/action_nav_home_to_nav_member"
app:destination="@id/nav_member" />
</fragment>
<fragment
android:id="@+id/navB_building"
android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.buildingdetails.BuildingDetailsFragment"
android:label="@string/menu_buiding"
tools:layout="@layout/fragment_buildingdetails" />
<fragment
android:id="@+id/navB_notice"
android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.noticeboard.NoticeBoardFragment"
android:label="@string/menu_notice"
tools:layout="@layout/fragment_notice" />
<fragment
android:id="@+id/navB_profile"
android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.profile.ProfileFragment"
android:label="Profile"
tools:layout="@layout/fragment_profile" />
<fragment
android:id="@+id/nav_account"
android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.account.AccountFragment"
android:label="@string/menu_account"
tools:layout="@layout/fragment_account" />
<fragment
android:id="@+id/nav_member"
android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.members.MembersFragment"
android:label="@string/menu_member"
tools:layout="@layout/fragment_member" />
<fragment
android:id="@+id/nav_election"
android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.electionandpoll.ElectionFragment"
android:label="@string/menu_election"
tools:layout="@layout/fragment_election" />
<fragment
android:id="@+id/nav_document"
android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.document.DocumentFragment"
android:label="@string/menu_document"
tools:layout="@layout/fragment_document" />
<fragment
android:id="@+id/nav_resource"
android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.resource.ResourceFragment"
android:label="@string/menu_resource"
tools:layout="@layout/fragment_resources" />
<fragment
android:id="@+id/nav_visitor"
android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.visitor.VisitorFragment"
android:label="@string/menu_visitor"
tools:layout="@layout/fragment_visitor" />
<fragment
android:id="@+id/nav_event"
android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.event.EventFragment"
android:label="@string/menu_event"
tools:layout="@layout/fragment_event" />
<fragment
android:id="@+id/nav_complain"
android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.complain.ComplainFragment"
android:label="@string/menu_complain"
tools:layout="@layout/fragment_complain" />
</navigation>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.