我对Android开发相当陌生。我正在检查材料设计库,我已经实现了一个带有底部导航的抽屉菜单,可以轻松浏览片段。两个组件都很好,但我不知道如何协调两个组件的导航。例如,当一个片段在抽屉菜单上切换时,它会改变布局,但按钮导航选定的项目不会随之改变。
如何解决此问题,并将这两个组件链接起来,使其相互工作,并同步更改的选定项?
这是我的java文件的样子,下面是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);
}
首先,当用户从底部导航中选择选项卡时,您应该避免重新创建片段。您应该将您的片段保留为私有变量:
private DrawerLayout drawer;
private BottomNavigationView bottomNavigationView;
private MainFragment mainFragment = MainFragment.newInstance();
private AccountFragment accountFragment = AccountFragment.newInstance();
private SellFragment sellFragment = SellFragment.newInstance();
private ChatFragment chatFragment = ChatFragment.newInstance();
private NotificationFragment notificationFragment = NotificationFragment.newInstance();
当用户选择一个选项卡时,只需切换到该片段:
bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.action_announces:
selectedFragment = mainFragment;
break;
case R.id.action_account:
selectedFragment = accountFragment;
break;
case R.id.action_sell:
selectedFragment = sellFragment;
break;
case R.id.action_chat:
selectedFragment = chatFragment;
break;
case R.id.action_notifications:
selectedFragment = notificationFragment;
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if (selectedFragment != null) {
transaction.replace(R.id.f_container, selectedFragment);
transaction.commit();
}
return true;
});
当用户使用导航菜单选择选项卡时,切换到所需片段:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
drawer.closeDrawers();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
switch (item.getItemId()) {
case R.id.nav_announces:
bottomNavigationView.setSelectedItemId(R.id.action_announces);
ft.replace(R.id.f_container, mainFragment);
break;
case R.id.nav_account:
bottomNavigationView.setSelectedItemId(R.id.action_account);
ft.replace(R.id.f_container, accountFragment);
break;
case R.id.nav_sell:
bottomNavigationView.setSelectedItemId(R.id.action_sell);
ft.replace(R.id.f_container, sellFragment);
break;
case R.id.nav_chat:
bottomNavigationView.setSelectedItemId(R.id.action_chat);
ft.replace(R.id.f_container, chatFragment);
break;
case R.id.nav_notifications:
bottomNavigationView.setSelectedItemId(R.id.action_notifications);
ft.replace(R.id.f_container, notificationFragment);
break;
}
ft.commit();
return true;
}
我已经将你的回购分叉,并将其迁移到AndroidX,然后将其固定在一个名为AndroidX的新分支上。我向你提出拉车请求。你可以接受并尝试。
我开始创建一个应用程序,它使用一个活动(导航抽屉)和许多片段。但我无法使用工具栏上的“后退”按钮从片段中导航回来。硬件后退按钮工作正常。我知道我需要覆盖选项ItemSelected,捕捉android。R、 id.home,检查后堆栈中是否有内容,然后弹出它。在更改片段后,“burger”按钮变为“back arrow”,但当我单击它时,选项ItemSelected将打开NavigationDra
有人能告诉我如何创建活动到这个主要活动,导航抽屉将看到在所有他们?我需要使用这个特定的MainActivity代码。我不需要使用碎片,只要3个简单的活动将添加到这个抽屉。 NavDrawer布局:
问题内容: 我有一个导航图,该导航图将此片段用作主活动XML中的主页。 我有一个带有菜单的Drawer布局,单击导航抽屉按钮时,我无法设法使导航正常工作(它可以从主要片段工作,但是当我单击Drawer按钮时,不能工作),如果我使用的是旧方法使用:对导航抽屉编程,我的navcontroller丢失了!!我得到类似的错误 navcontroller无法识别目标片段,因为即使不是这种情况,控制器也会看到
我有一个导航图,它在主活动XML中使用这个片段作为主页。 我有一个带菜单的抽屉布局,当我点击抽屉菜单按钮时,我无法使导航工作(它从主片段工作,但当我点击抽屉按钮时就不工作),如果我使用旧的方式来编程抽屉菜单:
问题内容: 在以下活动中,我上面有一个片段和一个图像。片段只是一个较暗的操作栏,上面有图片。我正在尝试将左侧幻灯片菜单作为一个片段,以便可以在每次活动中使用它。 主要活动; 主要活动XML; 每个活动上应包含的标题栏片段; 标题栏片段XML; 如何在标题栏中实现导航抽屉? 问题答案: 尝试这样的操作,对于布局文件,您只需要 实现可以像这样简单。
实际上,我正在我的应用程序中使用新的架构组件,并且我已经设置了导航组件。我有一个导航抽屉,我想用它。我已经设置好了,但我面临一些问题: 1-抽屉不会自动关闭。菜单正常工作并导航到正确的位置,但导航后不会关闭。我必须添加一个目的地ChangedListener才能自己关闭它。 在代码实验室里,抽屉自动关闭,我真的不明白为什么。 2-向上按钮打开抽屉。当我导航到非顶级片段时,菜单图标变为向上箭头,但当