android setOnNavigationItemSelectedListener已弃用

dy2hfwbg  于 2023-02-06  发布在  Android
关注(0)|答案(7)|浏览(346)

目前我正在开发一个应用程序,它有一个底部导航栏,有三个菜单项。我曾经使用setOnNavigationItemSelectedListener()的项目被点击。但现在我面临的问题是,该方法已贬值。

  • 应用程序语言:Java
  • 版本:'setOnNavigationItemSelectedListener(com.google.android.material.bottomnavigation.BottomNavigationView.OnNavigationItemSelectedListener)' is deprecated

有什么方法可以解决这个问题吗?有没有比setOnNavigationItemSelectedListener()方法更好的选择。

oo7oh9g9

oo7oh9g91#

根据github来源,它已被弃用:底部导航视图.setOnNavigationItemSelectedListener
在其评论中,你可以看到:

@deprecated Use {@link NavigationBarView#setOnItemSelectedListener(OnItemSelectedListener)}
   *     instead.

因此请从其基类中使用NavigationBarView.setOnItemSelectedListener:

/**
   * Set a listener that will be notified when a navigation item is selected. This listener will
   * also be notified when the currently selected item is reselected, unless an {@link
   * OnItemReselectedListener} has also been set.
   *
   * @param listener The listener to notify
   * @see #setOnItemReselectedListener(OnItemReselectedListener)
   */
  public void setOnItemSelectedListener(@Nullable OnItemSelectedListener listener) {
    selectedListener = listener;
  }

另请参阅此commit
因为它解释了对这一变化的困惑:
监听器被NavigationBarView#OnItemSelectedListenerNavigationBarView#OnItemReselectedListener取代,但从未添加过弃用文档,因此不清楚开发人员应该使用什么。

w8f9ii69

w8f9ii692#

你可以试试setonItemSelectedListener .它的工作原理和setOnNavigationItemSelectedListener()一样[在安卓11中测试]

bnv.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            int id = item.getItemId();
            switch(id){
              //check id
            }
            return true;
        }
    });

Kotlin:

bnv.setOnItemSelectedListener { item ->
            when (item.itemId) {
            }
            true
        }
8fq7wneg

8fq7wneg3#

我们可以用setOnItemSelectedListener代替setOnNavigationItemSelectedListener,用setOnItemReselectedListener代替setOnNavigationItemReselectedListener

navView.setOnItemSelectedListener {
    // do something
    true
}

// In case the default menu can be the first menu 
// Should set the default selected menu BETWEEN setOnItemSelectedListener and setOnItemReselectedListener. 
// It will make setOnItemSelectedListener fired when you launch app. 

// If you set default menu AFTER setOnItemReselectedListener. 
// Then setOnItemReselectedListener will fired when you launch app
navView.selectedItemId = R.id.navigation_home

navView.setOnItemReselectedListener {
    // do something
}
z2acfund

z2acfund4#

你可以在Kotlin

buttmNav.setOnItemSelectedListener { item ->
        when (item.itemId) {
        }
        true
    }
sbtkgmzw

sbtkgmzw5#

viewBindingMainActivity.navView.setOnItemSelectedListener { menuItem ->
            if (menuItem.itemId != R.id.navigation_home) {
                Add your code
                false
            } else {
                Add your code
                true
            }
        }
rsaldnfx

rsaldnfx6#

Kotlin:使用setOnItemSelectedListener

bottomNavigationView.setOnItemSelectedListener { item: MenuItem ->
            when (item.itemId) {
                 R.id. ... -> {
                    Add your code
                 true
            }
            
            else ->
                true
        }
gkn4icbw

gkn4icbw7#

公共类HomeActivity扩展AppCompatActivity实现导航栏视图。OnItemSelectedListener {

BottomNavigationView bottomNavigationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    bottomNavigationView = findViewById(R.id.bottom_nav);
    bottomNavigationView.setOnItemSelectedListener(this);
    displayfragment(new FragmentHome());

}

private void displayfragment(Fragment fragment) {

    getSupportFragmentManager().beginTransaction().replace(R.id.content_area, fragment).commit();

}

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

        case R.id.nav_home:
            fragment = new FragmentHome();
            break;
        case R.id.nav_fav:
            fragment = new FavouriteFragment();
            break;
        case R.id.nav_set:
            fragment = new FragmentSetting();
            break;

        default:
            fragment = new FragmentHome();
            break;

    }
    displayfragment(fragment);
    return true;
}

}

相关问题