有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在BottomNavigationView中禁用重新选择

BottomNavigationView中再次单击当前选项卡时,如何禁用重新加载片段

换句话说,如何在BottomNavigationView中禁用重新选择

PS:我正在使用NavigationUI来设置BottomNavigationView


共 (3) 个答案

  1. # 1 楼答案

    在kotlin中,代码如下所示:

     val navView: BottomNavigationView = findViewById(R.id.nav_view)
    
     navView.setOnNavigationItemReselectedListener { 
        // do nothing here when reselected
        }
    
  2. # 2 楼答案

    试试这个让我知道穆罕默德·侯赛因

    final Fragment fragment1 = new Fragment1();
    final Fragment fragment2 = new Fragment2();
    final Fragment fragment3 = new Fragment3();
    final FragmentManager fm = getSupportFragmentManager();
    Fragment active = fragment1; //make the first screen as your active fragment.
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    fm.beginTransaction().add(R.id.main_container, fragment3, "3").hide(fragment3).commit();
            fm.beginTransaction().add(R.id.main_container, fragment2, "2").hide(fragment2).commit();
            fm.beginTransaction().add(R.id.main_container, fragment1, "1").commit();
    
    }
    
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
    
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.f1_menu_item:
                    fm.beginTransaction().hide(active).show(fragment1).commit();
                    active = fragment1;
                    return true;
    
                case R.id.f2_menu_item:
                    fm.beginTransaction().hide(active).show(fragment2).commit();
                    active = fragment2;
                    return true;
    
                case R.id.f3_menu_item:
                   fm.beginTransaction().hide(active).show(fragment3).commit();
                  active = fragment3;
    
                    return true;
            }
            return false;
        }
    };
    

    在活动中。xml

    <RelativeLayout
    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/coordinatorlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FBF9F9"
    tools:context=".view.HomeScreenActivity"
    android:orientation="vertical">
    
    
    <FrameLayout 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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:padding="1dp"
        android:id="@+id/main_container">
    
    </FrameLayout>
    
    </RelativeLayout>
    

    如果你困在何处,请告诉我。如果这有效,就接受答案

  3. # 3 楼答案

    这是出人意料的简单

        NavigationUI.setupWithNavController(bottomNavigationView, hostNavController);
        bottomNavigationView.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
            @Override
            public void onNavigationItemReselected(@NonNull MenuItem item) {
                // Nothing here to disable reselect
            }
        });