如何将导航抽屉与Buttom导航视图协调 [英] How to coordinate a Navigation Drawer with a Buttom Navigation View
问题描述
我是android开发人员的新手.我正在检查材料设计库,并且已经实现了带有底部导航的导航抽屉,可轻松浏览片段.两个组件都可以正常工作,但是我不知道如何协调两个组件的导航.例如,当在导航抽屉上切换片段时,它会更改布局,但按钮导航所选项目不会随之改变.
I'm fairly new to android dev. I'm checking the material design library and I've implemented a navigation drawer with a bottom navigation to navigate easily through fragments. Both components work just fine but I don't how to coordinate the navigation with both components. For example when a fragment is toggled on the navigation drawer it changed the layout but the button navigation selected item is not changed with it.
我该如何解决此问题并将两个组件链接在一起以相互协作并同步更改的所选项目?
How can I solve this issue and link both components to work with each other and sync the changed selected item?
这是我的java文件的外观,这是GitHub上整个项目的链接: https://github .com/mreek/NavigationDrawer
This is what my java file looks like and here's link for the whole project on GitHub: https://github.com/mreek/NavigationDrawer
谢谢.
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
private TextView appBarTV;
private static final String TAG = "MyActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent= new Intent(this,IntroActivity.class);
startActivity(intent);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
appBarTV = findViewById(R.id.appbar_text_view);
ImageButton menuRight = findViewById(R.id.leftRight);
menuRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
}
});
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.action_item1:
selectedFragment = MainFragment.newInstance();
break;
case R.id.action_item2:
selectedFragment = AccountFragment.newInstance();
break;
case R.id.action_item3:
selectedFragment = SellFragment.newInstance();
break;
case R.id.action_item4:
selectedFragment = ChatFragment.newInstance();
break;
case R.id.action_item5:
selectedFragment = NotificationFragment.newInstance();
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.f_container, selectedFragment);
transaction.commit();
return true;
}
});
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.f_container, MainFragment.newInstance());
transaction.commit();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
drawer.closeDrawers();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
int id = item.getItemId();
if (id == R.id.nav_camera) {
MainFragment fragment = new MainFragment();
ft.replace(R.id.f_container, fragment);
ft.commit();
} else if (id == R.id.nav_gallery) {
//appBarTV.setText("Fragment With Tabs");
ChatFragment fragment = new ChatFragment();
ft.replace(R.id.f_container, fragment);
ft.commit();
} else if (id == R.id.nav_slideshow) {
AccountFragment fragmentTab = new AccountFragment();
ft.replace(R.id.f_container, fragmentTab);
ft.commit();
} else if (id == R.id.nav_share) {
Toast.makeText(this, "Partager", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_send) {
Toast.makeText(this, "Rate 5 stars", Toast.LENGTH_SHORT).show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/app_bar_main" />
<FrameLayout
android:id="@+id/f_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="3dp"
android:background="@color/white"
app:itemIconTint="@color/blue"
app:itemTextColor="@color/blue"
app:menu="@menu/bottom_navigation_items" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"
app:itemTextColor="@color/black"/>
</android.support.v4.widget.DrawerLayout>
推荐答案
尝试以下代码:
private void setupNavDrawer(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()){
case R.id.nav_gallery:
fragment = new GalleryFragment();
break;
case R.id.nav_share:
fragment = new ShareFragment();
break;
default:
fragment = new GalleryFragment();
break;
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
//if checked - set title menu
item.setChecked(true);
setTitle(item.getTitle());
mDrawerLayout.closeDrawers();
return true;
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
............//nav
setupNavDrawer(navigationView);
}
这篇关于如何将导航抽屉与Buttom导航视图协调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!