我正在尝试从Jetpack实施 BottomNavigationView
def nav_version = "2.0.0"
// Java language implementation
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
首次测试已完成,但不包括下面显示的注释行,并且事务自动由库完成。
这样,我面临的问题是:如果我按底部菜单的100倍,则需要按100次返回按钮,直到回到MainFragment。
第二项测试已取消注释行的注释,并且对交易进行了硬编码。
这样,我面临的问题是:“片段”内容彼此重叠,我无法弄清楚自己在做错什么。
由于第二次测试未成功,我运行了该应用,但没有再次出现下面的注释行(我删除了),但是库自动进行了交易( 首次测试中有经验的人)不再工作。我的意思是只有MainFragment被启动,其他的则没有。
public class Mainactivity extends AppCompatactivity implements BottomNavigationView.OnNavigationItemSelectedListener {
private static final String TAG = "debinf Mainactivity";
//public static final String FRAGMENT_KEY = "fragment";
private BottomNavigationView bottomNavigationView;
private NavigationView navigationView;
private DrawerLayout drawerLayout;
private NavController navController;
private AppBarConfiguration appBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG,"onCreate: ");
bottomNavigationView = (BottomNavigationView) findViewById(R.id.main_bottomnav);
navigationView = (NavigationView) findViewById(R.id.main_sidebar);
drawerLayout = (DrawerLayout) findViewById(R.id.main_drawer);
setupNavigation();
// Fragment been created for the first time
/*if (getSupportFragmentManager().findFragmentByTag(FRAGMENT_KEY) == null) {
Log.i(TAG,"onCreate: First fragment been created: MainFragment()");
fragmentTransaction(new MainFragment());
}*/
}
private void setupNavigation() {
Log.i(TAG,"setupNavigation: ");
navController = Navigation.findNavController(this,R.id.main_fragment);
appBarConfiguration =
new AppBarConfiguration.Builder(navController.getGraph())
.setDrawerLayout(drawerLayout)
.build();
NavigationUI.setupactionBarWithNavController(this,navController,appBarConfiguration);
NavigationUI.setupWithNavController(navigationView,navController);
NavigationUI.setupWithNavController(bottomNavigationView,navController);
// Listener for bottomNavigation must be called after been setupWithNavController
bottomNavigationView.setOnNavigationItemSelectedListener(this);
}
/*private void fragmentTransaction(Fragment fragment) {
Log.i(TAG,"fragmentTransaction: Hardcoded Transaction");
getSupportFragmentManager().popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out)
.replace(R.id.main_fragment,fragment,null)
.commit();
}*/
@Override
public void onBackpressed() {
Log.i(TAG,"onBackpressed: ");
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackpressed();
}
}
@Override
public boolean onSupportNavigateUp() {
Log.i(TAG,"onSupportNavigateUp: ");
return NavigationUI.navigateUp(navController,appBarConfiguration);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Log.i(TAG,"onNavigationItemSelected: ");
if (menuItem.getItemId() == R.id.mainFragment) {
Log.i(TAG,"onNavigationItemSelected: mainFrag");
//fragmentTransaction(new MainFragment()); // Hardcoded transaction
}
if (menuItem.getItemId() == R.id.shopFragment) {
Log.i(TAG,"onNavigationItemSelected: shopFrag");
//fragmentTransaction(new ShopFragment()); // Hardcoded transaction
}
if (menuItem.getItemId() == R.id.searchFragment) {
Log.i(TAG,"onNavigationItemSelected: searchFragment");
//fragmentTransaction(new SearchFragment()); // Hardcoded transaction
}
return true;
}
}
要使Jetpack库自动进行交易,我该怎么办?
P.S .:除了上面代码中显示的内容以外,我没有更改任何其他代码。
编辑
下面是 nav_graph.xml ,我想按一次返回按钮返回MainFragment,然后再次按返回按钮离开应用程序(第一个测试问题)
感谢您的帮助!
<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/mainnav_graph"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.aelecomerce.goodstag.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main" />
<fragment
android:id="@+id/shopFragment"
android:name="com.aelecomerce.goodstag.ShopFragment"
android:label="fragment_shop"
tools:layout="@layout/fragment_shop" >
<action
android:id="@+id/openAddstoreFragment"
app:destination="@id/addstoreFragment"
app:enterAnim="@anim/nav_default_enter_anim"/>
<action
android:id="@+id/openBarcodeFragment"
app:destination="@id/barcodeFragment" />
</fragment>
<fragment
android:id="@+id/searchFragment"
android:name="com.aelecomerce.goodstag.SearchFragment"
android:label="fragment_search"
tools:layout="@layout/fragment_search" />
<fragment
android:id="@+id/addstoreFragment"
android:name="com.aelecomerce.goodstag.AddstoreFragment"
android:label="fragment_addstore"
tools:layout="@layout/fragment_addstore" />
<fragment
android:id="@+id/barcodeFragment"
android:name="com.aelecomerce.goodstag.BarcodeFragment"
android:label="fragment_barcode"
tools:layout="@layout/fragment_barcode" />
</navigation>
下面是BottomNavigationView的菜单
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@id/mainFragment"
android:title="Home"
android:icon="@android:drawable/stat_sys_phone_call_on_hold"
android:menuCategory="secondary"/>
<item
android:id="@id/shopFragment"
android:title="Shop"
android:icon="@android:drawable/stat_sys_phone_call_on_hold"
android:menuCategory="secondary"/>
<item
android:id="@id/searchFragment"
android:title="Search"
android:icon="@android:drawable/stat_sys_phone_call_on_hold"
android:menuCategory="secondary"/>
</menu>
下面是 main_activity.xml
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/main_drawer"
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Mainactivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/main_fragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:name="androidx.navigation.fragment.NavHostFragment"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/main_bottomnav"
app:defaultNavHost="true"
app:navGraph="@navigation/mainnav_graph"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/main_bottomnav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/main_navmenu"
android:background="@color/coloraccent"
app:itemIconTint="@drawable/botton_item_color"
app:itemTextColor="@drawable/botton_item_color">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/main_sidebar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/main_sidebarmenu"/>
</androidx.drawerlayout.widget.DrawerLayout>