java - 如何解决导航栏有导航底部问题

标签 java android

我的问题是,我试图制作一个简单的应用程序,并且我在同一个 Activity 中使用了导航栏和底部导航,但我遇到了一个简单的问题,例如当我运行应用程序时,我位于主页 fragment 中,但是当我拖动导航栏并单击电影时,它会进入电影 fragment ,并且我的电影项目被选中,但问题是当我再次单击底部导航菜单中的主页而不是导航菜单中时,电影项目仍然被选中。请告诉我,如果我单击底部导航中的主页,然后在导航栏中我的电影项目变为未选中状态,我该怎么做。

希望你能理解我的问题

这是我的代码:-

主要 Activity :-

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private FirebaseAuth mAuth;
    private DrawerLayout drawerLayout;
    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment = null;

                    switch (item.getItemId()) {
                        case R.id.nav_home:
                            selectedFragment = new HomeFragment();

                            break;
                        case R.id.nav_search:
                            selectedFragment = new SearchFragment();
                            break;
                        case R.id.nav_features:
                            selectedFragment = new FeaturesFragment();
                            break;
                        case R.id.nav_myMusic:
                            selectedFragment = new MyMusicFragment();
                            break;

                    }
                    assert selectedFragment != null;
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                            selectedFragment).commit();
                    return true;
                }
            };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        drawerLayout = findViewById(R.id.Drawyer_layout);


        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.navigation_drawer_open,
                R.string.navigation_drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();


        mAuth = FirebaseAuth.getInstance();

        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(navListener);

        Fragment selections = null;

        if (savedInstanceState == null) {


            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        FirebaseUser currentuser;
        currentuser = mAuth.getCurrentUser();

        if (currentuser == null) {
            SendUserToSelectTypeActivity();
        }
    }

    private void SendUserToSelectTypeActivity() {

        Intent testintent = new Intent(MainActivity.this, Selecttype.class);
        testintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(testintent);
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
        finish();

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {

            case R.id.nav_Movies:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new MoviesFragment()).commit();

                break;
            case R.id.nav_log_out:
                SendUserToSelectTypeActivity();
                mAuth.signOut();
                break;
            case R.id.nav_live:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container
                        , new LiveVideosFragment()).commit();
        }
        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
}

主页 fragment :-

public class HomeFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home , container , false);

    }
}

导航菜单:-

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_Movies"
            android:icon="@drawable/ic_movie"
            android:title="Movies" />
        <item
            android:id="@+id/nav_live"
            android:icon="@drawable/ic_movie"
            android:title="Live Videos" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_share_black_24dp"
                android:title="Share" />
            <item
                android:id="@+id/nav_contact"
                android:icon="@drawable/ic_report_black_24dp"
                android:title="Report us" />
            <item
                android:id="@+id/nav_log_out"
                android:icon="@drawable/ic_arrow_back_black_24dp"
                android:title="Log Out" />

        </menu>
    </item>

</menu>

Activity main.xml:-

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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/Drawyer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:fitsSystemWindows="true"
    tools:context=".MainClasses.MainActivity"
    tools:openDrawer="start">

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


    </LinearLayout>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/bottom_navigation">

        </FrameLayout>

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/design_default_color_primary_dark"
            app:itemIconTint="@color/White"
            app:itemTextColor="@color/White"
            app:menu="@menu/bottom_navigation_menu" />


    </RelativeLayout>




    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/naviation_menu_main"
        >

    </com.google.android.material.navigation.NavigationView>


</androidx.drawerlayout.widget.DrawerLayout>

如果您找到解决方案,请回答我

最佳答案

在可绘制文件中创建一个文件,例如“bottom_icon_color.xml”

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="@color/red" />
    <item android:state_checked="false" android:color="@color/lightgray"  />
</selector>

然后将其应用到底部导航

 app:itemIconTint="@drawable/bottom_icon_color"
 app:itemTextColor="@drawable/bottom_icon_color"

关于java - 如何解决导航栏有导航底部问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57572847/

相关文章:

java - 并行执行竞争计算并丢弃除第一个完成的计算之外的所有计算

java - Java 中的 char** 等价物

java - 应用工具栏时崩溃

android - ListView 适配器中的图像

android - 强制停止 httpClient httpget?

java - 在命令对象中绑定(bind)集合时出错

java - SimpleDateFormat 函数 parse(String s) 给出了错误的日期

android - 如何使用 react native 正确卸载注销时的所有屏幕?

java - Wicket:更新 DropDownChoice 项目选择事件的模型

android - 相机 Intent 在 android 上保持打开状态