当前位置:网站首页>[已解决]setOnNavigationItemSelectedListener()被弃用
[已解决]setOnNavigationItemSelectedListener()被弃用
2022-06-26 08:26:00 【CyberESec】
在我开发app的过程中,我用了setOnNavigationItemSelectedListener()这个对象,但是我现在遇到了问题
setOnNavigationItemSelectedListener(com.google.android.material.bottomnavigation.BottomNavigationView.OnNavigationItemSelectedListener)
被弃用,所以下面向你解释可能的解决方法。
怎么解决setOnNavigationItemSelectedListener被弃用导致的问题?
你可以尝试使用setonItemSelectedListener,它的用法和setOnNavigationItemSelectedListener()是相同的
方法一:用setonItemSelectedListener替换
java样例:
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
}
java样例demo
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
import com.google.android.material.navigation.NavigationBarView;
import com.xxx.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
NavigationBarView navigationBarView;
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
navigationBarView = findViewById(R.id.bottom);
if (savedInstanceState == null){
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new HomeFragment()).commit();
}
navigationBarView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()){
case R.id.index:
fragment = new HomeFragment();
break;
case R.id.vip:
fragment = new ShoppingFragment();
break;
case R.id.User:
fragment = new UserFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, fragment).commit();
return true;
}
});
}
}
方法二:使用基类
/** * 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;
}
以上是关于这个问题的所有解决方法,希望对你有所帮助。可以在评论区留下你的想法,也可以说一下哪个对你有用。
边栏推荐
- Relevant knowledge of DRF
- Batch modify file name
- Monitor iPad Keyboard Display and hide events
- Comparison version number [leetcode]
- Swift code implements method calls
- Win10 mysql-8.0.23-winx64 solution for forgetting MySQL password (detailed steps)
- CodeBlocks integrated Objective-C development
- h5 localStorage
- Software engineering - high cohesion and low coupling
- drf的相关知识
猜你喜欢

Uniapp uses uviewui

Read excel table and render with FileReader object

Baoyan postgraduate entrance examination interview - operating system

Interview for postgraduate entrance examination of Baoyan University - machine learning

How to debug plug-ins using vs Code

Diode voltage doubling circuit

2020-10-17

And are two numbers of S

Installation of jupyter

Interpretation of x-vlm multimodal model
随机推荐
Go language shallow copy and deep copy
1. error using XPath to locate tag
XXL job configuration alarm email notification
Oracle 19C local listener configuration error - no listener
Use of jupyter notebook
JS precompile - Variable - scope - closure
Mapping '/var/mobile/Library/Caches/com. apple. keyboards/images/tmp. gcyBAl37' failed: 'Invalid argume
2020-10-17
监听iPad键盘显示和隐藏事件
Microcontroller from entry to advanced
. eslintrc. JS configuration
Discrete device ~ resistance capacitance
(vs2019 MFC connects to MySQL) make a simple login interface (detailed)
Flume learning notes
loading view时,后面所有东西屏蔽
Interpretation of x-vlm multimodal model
(4) Independent key
Necessary protection ring for weak current detection
Design of reverse five times voltage amplifier circuit
Relevant knowledge of DRF