【问题标题】:Initiale Fragment in bottom Navigation has duplicated [ANDROID STUDIO]底部导航中的 Initiale Fragment 重复 [ANDROID STUDIO]
【发布时间】:2021-09-07 18:10:04
【问题描述】:

我为一个学校项目制作了一个android应用程序,实际上我遇到了一个问题。

我创建了 2 个活动(MainActivity 和 BottomNavActivity),我尝试在它们之间导航,但导航中的“片段”主页仍未定义。 我发现本教程是为了在片段之间导航 我添加了事件):

https://medium.com/@oluwabukunmi.aluko/bottom-navigation-view-with-fragments-a074bfd08711

但是,现在,我可以在我的“片段”之间导航,但我最初的“片段”(启动活动时首先出现的片段)仍然存在,因此在更改片段时,它会被复制。

Here is a video of my problem

我的 Infos.class

public class Infos extends AppCompatActivity {

    private ActivityInfosBinding binding;

    //I create my variable
    final Fragment frag2 = new ProduitsFragment();
    final Fragment frag1 = new EntrepriseFragment();
    final Fragment frag3 = new CGUFragment();
    final FragmentManager fm = getSupportFragmentManager();
    Fragment active = frag1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityInfosBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_entreprise, R.id.navigation_produits, R.id.navigation_CGU)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_infos);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);


        //I add my differents fragments
        fm.beginTransaction().add(R.id.nav_host_fragment_activity_infos, frag3).hide(frag3).commit();
        fm.beginTransaction().add(R.id.nav_host_fragment_activity_infos,frag1).hide(frag1).commit();

        navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem item) {

                switch (item.getItemId()) {
                    //My first fragment
                    case R.id.navigation_entreprise:
                        fm.beginTransaction().hide(active).show(frag1).commit();
                        active = frag1;
                        return true;

                        //My second fragment (default in video)
                    case R.id.navigation_CGU:
                        fm.beginTransaction().hide(active).show(frag3).commit();
                        active = frag3;
                        return true;                        
                }
                return false;
            }
        });
    }
}

还有我的 mobile_navigation.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/mobile_navigation"
    app:startDestination="@id/navigation_CGU"> //I try to change this, but nothing. This fragment is duplicated.

    <fragment
        android:id="@+id/navigation_entreprise"
        android:name="fr.romaindrouhot.aurelinfo.ui.home.EntrepriseFragment"
        android:label="@string/nav_entreprise"
        tools:layout="@layout/fragment_entreprise" />

    <fragment
        android:id="@+id/navigation_produits"
        android:name="fr.romaindrouhot.aurelinfo.ui.home.ProduitsFragment"
        android:label="@string/nav_produits"
        tools:layout="@layout/fragment_produits" />
    <fragment
        android:id="@+id/navigation_CGU"
        android:name="fr.romaindrouhot.aurelinfo.ui.home.CGUFragment"
        android:label="@string/nav_CGU"
        tools:layout="@layout/fragment_cgu" />
</navigation>

你能帮帮我吗?

【问题讨论】:

  • 好的,在那个帖子下看到
  • 您好,您是否尝试删除评论下方的所有代码//I add my differents fragments?通常,导航组件处理片段事务

标签: android android-fragments duplicates


【解决方案1】:

是的,这是我的 activity_infos.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment_activity_infos"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:defaultNavHost="true"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

我的 fragment_cgu.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textSize="50sp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

我的 fragment_entreprise.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".ui.home.EntrepriseFragment"
    android:id="@+id/fragment_entreprise"
    android:background="@drawable/logonoiretblanc">

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/entreprise_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/title"
                android:textColor="@color/black"
                android:textSize="@dimen/title"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>

            <LinearLayout
                android:id="@+id/linearLayout"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:background="@drawable/border"
                android:orientation="vertical"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/entreprise_title"
                android:layout_marginTop="20sp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/informations"
                android:textAlignment="center"
                android:textColor="@color/black"
                android:textSize="25sp"
                app:layout_constraintBottom_toBottomOf="@+id/linearLayout"
                app:layout_constraintTop_toTopOf="@+id/linearLayout"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"/>

            </LinearLayout>

            <TextView
                android:id="@+id/title_gerant"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/gerant_title"
                android:shadowColor="@color/black"
                android:shadowRadius="1"
                android:textAlignment="center"
                android:textColor="@color/dark_green"
                android:textSize="@dimen/gerant_title"
                app:layout_constraintTop_toBottomOf="@+id/linearLayout"
                android:layout_marginTop="20sp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />


            <TextView
                android:id="@+id/gerant"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/gerant"
                android:textAlignment="center"
                android:textColor="@color/blue"
                android:textSize="@dimen/gerant"
                app:layout_constraintTop_toBottomOf="@+id/title_gerant"
                android:layout_marginTop="@dimen/espace_entre_title_text"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />

            <TextView
                android:id="@+id/title_localisation"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/localisation_title"
                android:textAlignment="center"
                android:textColor="@color/dark_green"
                android:shadowColor="@color/black"
                android:shadowRadius="1"
                android:textSize="@dimen/gerant_title"
                app:layout_constraintTop_toBottomOf="@+id/gerant"
                android:layout_marginTop="@dimen/espace_entre_text_title"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />

            <TextView
                android:id="@+id/localisation"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/localisation"
                android:textAlignment="center"
                android:textColor="@color/blue"
                android:textSize="@dimen/gerant"
                app:layout_constraintTop_toBottomOf="@+id/title_contact"
                android:layout_marginTop="@dimen/espace_entre_title_text"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />

            <TextView
                android:id="@+id/title_activite"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/title_activite"
                android:textAlignment="center"
                android:textColor="@color/dark_green"
                android:shadowColor="@color/black"
                android:shadowRadius="1"
                android:textSize="@dimen/gerant_title"
                app:layout_constraintTop_toBottomOf="@+id/localisation"
                android:layout_marginTop="@dimen/espace_entre_text_title"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />

            <TextView
                android:id="@+id/activite"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/activite"
                android:textAlignment="center"
                android:textColor="@color/blue"
                android:textSize="@dimen/gerant"
                app:layout_constraintTop_toBottomOf="@+id/title_activite"
                android:layout_marginTop="@dimen/espace_entre_title_text"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />

            <TextView
                android:id="@+id/title_contact"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/title_contact"
                android:textAlignment="center"
                android:textColor="@color/dark_green"
                android:shadowColor="@color/black"
                android:shadowRadius="1"
                android:textSize="@dimen/gerant_title"
                android:layout_marginTop="@dimen/espace_entre_text_title"
                app:layout_constraintTop_toBottomOf="@+id/activite"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"/>

            <TextView
                android:id="@+id/contact_description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/contact"
                android:textAlignment="center"
                android:textColor="@color/blue"
                android:textSize="@dimen/gerant"
                app:layout_constraintTop_toBottomOf="@+id/title_contact"
                android:layout_marginTop="@dimen/espace_entre_title_text"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />

        </LinearLayout>
    </ScrollView>
</LinearLayout>

【讨论】:

    【解决方案2】:

    正如我在评论部分提到的,导航组件自己处理片段之间的事务。

    这里有一些设置底部导航栏的技巧,希望对您有所帮助。

    首先你的mobile_navigation.xml 和片段 XML 似乎很好。

    我可以修复一下你的 activity_infos.xml 文件:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <fragment
            android:id="@+id/nav_host_fragment_activity_infos"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="Odp"
            app:defaultNavHost="true"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toTopOf="@id/nav_view"
            app:navGraph="@navigation/mobile_navigation" />
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            ...
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            ...
            />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    • 使用 Start/End here is why 更新左/右约束
    • id/nav_host_fragment_activity_infosid/nav_view 的底部之间添加一个约束,以确保该片段永远不会被导航栏隐藏。它是可选的。

    然后,您的活动可以在没有FrragmentManager 的情况下更新:

    public class Infos extends AppCompatActivity {
    
        private ActivityInfosBinding binding;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // When you are using an activity you can directly use this
            binding = DataBindingUtil.setContentView(this, R.layout.activity_infos);
    
            // Passing each menu ID as a set of Ids because each
            // menu should be considered as top-level destinations.
            AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                    R.id.navigation_entreprise, R.id.navigation_produits, R.id.navigation_CGU)
                    .build();
            NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_infos);
            NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
            NavigationUI.setupWithNavController(binding.navView, navController);
    
        }
    }
    

    我找到了这个tutorial,它一步一步地解释,它可能提供比我的“提示”更多的信息。

    【讨论】:

    • 您好,谢谢您的回答,但问题是我的“项目”之一具有启动特定活动的功能(因为我在将 ListView 放入“片段”时遇到问题,当然。在那里,打开的只是我的片段(没关系),但我的活动不再有效?
    • 如果我理解得很好,您想在这三个选项卡中将项目列表显示为片段吗?您是否尝试过实现 Recyclerview 而不是 ListView?
    • 我刚刚测试了一个教程,但是和我的 ListView 一样的问题,我的 setAdapter 有错误。这就是我参加另一项活动的原因。
    • 您遇到了什么错误?这很奇怪,因为 ListView 和 RecyclerView 在片段中也能很好地工作
    • Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference 用于我的回收站视图和我的列表视图:Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
    【解决方案3】:

    您能否尝试将其替换为您的 activity_infos.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white">
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="0dp"
            android:layout_marginEnd="0dp"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/bottom_nav_menu" />
    
        <fragment
            android:id="@+id/nav_host_fragment_activity_infos"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:defaultNavHost="true"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:navGraph="@navigation/mobile_navigation" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

    • 我替换了,但没有任何改变......当我改变时,我的第一个片段在我的另一个片段之上......
    猜你喜欢
    • 1970-01-01
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多