我是Android开发人员的新手。我正在检查材料设计库,并且已经实现了带有底部导航的导航抽屉,可轻松浏览片段。两个组件都可以正常工作,但是我不怎么协调两个组件的导航。例如,在导航抽屉上切换片段时,它会更改布局,但按钮导航所选项目不会随之改变。
我该如何解决此问题并将两个组件链接在一起以相互协作并同步更改的所选项目?
这是我的java文件的外观,这是GitHub上整个项目的链接:https://github.com/mreek/NavigationDrawer
谢谢。
package com.example.android.myndapplication.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.example.android.myndapplication.fragments.AccountFragment;
import com.example.android.myndapplication.fragments.ChatFragment;
import com.example.android.myndapplication.fragments.MainFragment;
import com.example.android.myndapplication.fragments.MainFragmentTab;
import com.example.android.myndapplication.R;
import com.example.android.myndapplication.fragments.NotificationFragment;
import com.example.android.myndapplication.fragments.SellFragment;
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);
// launch the introActivity
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();
// Log.v(TAG, "lwl" );
break;
case R.id.action_item2:
selectedFragment = AccountFragment.newInstance();
//Log.v(TAG, "tani" );
break;
case R.id.action_item3:
selectedFragment = SellFragment.newInstance();
//Log.v(TAG, "talet" );
break;
case R.id.action_item4:
selectedFragment = ChatFragment.newInstance();
//Log.v(TAG, "talet" );
break;
case R.id.action_item5:
selectedFragment = NotificationFragment.newInstance();
//Log.v(TAG, "talet" );
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.f_container, selectedFragment);
transaction.commit();
return true;
}
});
//Manually displaying the first fragment - one time only
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.f_container, MainFragment.newInstance());
transaction.commit();
//Used to select an item programmatically
//bottomNavigationView.getMenu().getItem(2).setChecked(true);
}
@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) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
drawer.closeDrawers();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// appBarTV.setText("Main Page");
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) {
//appBarTV.setText("Slideshow Page");
//AccountFragment
//Toast.makeText(this, "Slideshow", Toast.LENGTH_SHORT).show();
AccountFragment fragmentTab = new AccountFragment();
ft.replace(R.id.f_container, fragmentTab);
ft.commit();
} else if (id == R.id.nav_manage) {
//appBarTV.setText("Tools Page");
// Toast.makeText(this, "Contactez_nous Activity", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_share) {
//appBarTV.setText("Share Page");
Toast.makeText(this, "Partager", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_send) {
//appBarTV.setText("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.design.widget.FloatingActionButton
android:id="@+id/fab"
app:rippleColor="@color/colorPrimaryDark"
android:clickable="true"
android:layout_gravity="bottom|center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="16dp"
app:srcCompat="@drawable/ic_menu_manage"
android:tint="@color/colorAccent"
app:backgroundTint="@color/colorPrimary"
app:borderWidth="0dp"
app:elevation="9dp" />
-->
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:0)
尝试此代码:
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);
}
答案 1 :(得分:0)
首先,当用户从底部导航中选择一个选项卡时,应避免重新创建片段。您应该将片段保留为私有变量:
computed.write.parquet('data/computed.parquet')
当用户选择一个恰好贴在该片段上的标签时:
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;
});
在将其修复到名为androidx的新分支之前,我已经分叉了您的仓库并将其迁移到AndroidX。我向您提出了要求。您可以接受并尝试。