如何切换NavController的BottomNavigationView表单同级片段

时间:2019-11-19 10:57:45

标签: android android-fragments navigation bottomnavigationview

我正在使用

  

androidx.navigation.NavController

  

com.google.android.material.bottomnavigation.BottomNavigationView

进行导航。现在,我需要从第一个打开第二个片段。我该怎么办?

这是我的代码

bottom_nav_menu_distributor

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_distributor_home"
android:icon="@mipmap/ic_home"
android:title="@string/title_distributor_home" />
<item
android:id="@+id/navigation_distributor_scan"
android:icon="@mipmap/ic_scan"
android:title="@string/title_distributor_scan" />
<item
android:id="@+id/navigation_distributor_manual"
android:icon="@mipmap/ic_manual"
android:title="@string/title_distributor_manual" />
<item
android:id="@+id/navigation_distributor_profile"
android:icon="@mipmap/ic_profile"
android:title="@string/title_distributor_profile" />
</menu>

navigation_distributor.xml

<?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/navigation_distributor"
app:startDestination="@+id/navigation_distributor_home">
<fragment
android:id="@+id/navigation_distributor_home"
android:name="com.highflextech.watersupply.uidistributor.home.HomeDistributorFragment"
android:label="@string/title_distributor_home"
tools:layout="@layout/fragment_distributor_home" />
<fragment
android:id="@+id/navigation_distributor_scan"
android:name="com.highflextech.watersupply.uidistributor.scan.ScanDistributorFragment"
android:label="@string/title_distributor_scan"
tools:layout="@layout/fragment_distributor_scan" >
</fragment>
<fragment
android:id="@+id/navigation_distributor_manual"
android:name="com.highflextech.watersupply.uidistributor.manual.ManualDistributorFragmant"
android:label="@string/title_distributor_manual"
tools:layout="@layout/fragment_distributor_manual" />
<fragment
android:id="@+id/navigation_distributor_profile"
android:name="com.highflextech.watersupply.uidistributor.profile.ProfileDistributorFragment"
android:label="@string/title_distributor_profile"
tools:layout="@layout/fragment_distributor_profile" />
</navigation>

activity_distributor_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container_distributor"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_distributor_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
app:labelVisibilityMode="labeled"
android:background="?android:attr/windowBackground"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_nav_menu_distributor" />
<fragment
android:id="@+id/nav_host_distributor_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/nav_distributor_view"
android:layout_alignParentTop="true"
app:navGraph="@navigation/navigation_distributor" />
</RelativeLayout>

DistributorHome.java

public class DistributorHome extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_distributor_home);
BottomNavigationView navView = findViewById(R.id.nav_distributor_view);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_distributor_home,
R.id.navigation_distributor_scan,
R.id.navigation_distributor_manual,
R.id.navigation_distributor_profile)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_distributor_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
}

ScanDistributorFragment.java

public class ScanDistributorFragment extends Fragment implements  View.OnClickListener
{
public View onCreateView(@NonNull LayoutInflater inflater,
                     ViewGroup container, Bundle savedInstanceState) {
Log.d("Scan Fragment:", "Entered");
View root = inflater.inflate(R.layout.fragment_distributor_scan, container, false);
//..........
}
@Override
public void onClick(View v) {
//.........
NavController navController = Navigation.findNavController(getActivity(), R.id.nav_host_distributor_fragment);
navController.navigate(R.id.action_distributor_manual);
}
} 

它显示了action_distributor_manual片段,但BottomNavigationView没有显示对它的选择。

0 个答案:

没有答案