繁体   English   中英

如何解决导航栏与导航底部的问题

[英]How to solve navigation bar with navigation bottom problem

我的问题是我正在尝试制作一个简单的应用程序,并且在同一活动中同时使用了导航栏和底部导航,但是我遇到了一个简单的问题,例如,当我运行该应用程序时,我身处碎片中,但是,当我拖动导航栏并单击电影时,它将进入电影片段,并且我的电影项目被检查了,但是问题是当我再次在底部导航菜单中单击主页时,而不是在我的导航菜单中,电影项目仍然被选中了。 请告诉我,如果我在底部导航栏中单击首页,然后在导航栏中未选中我的电影项目,该怎么做。

希望你能理解我的问题

这是我的代码:

主要活动 : -

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();
        }
    }
}

HomeFragment:-

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>

活动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>

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

根据您在问题中提到的问题,您必须首先存储先前单击的MenuItem,然后使用MenuItem的setCheckable(false)将其设为未选中状态。

我假设所有其他功能都正常工作,例如,当选择了底部导航项时,请检查NavigationView的项。

在您的onNavigationItemSelected存储中,在一个变量中单击的MenuItem在一个变量中,即MenuItem类型的prevMenuItem。 现在,在您的BottomNavigationView.OnNavigationItemSelectedListener()中,将此prevMenuItem设为未选中状态。

NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.getMenu().findItem(previousMenuItem.getId()).setCheckable(false);

在可绘制文件中制作一个文件,例如“ 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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM