Android Studio:BottomNavigationView和片段之间的事务

问题描述 投票:0回答:1

我正在尝试从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>
android bottomnavigationview
1个回答
1
投票
删除行
© www.soinside.com 2019 - 2024. All rights reserved.