当前位置:网站首页>[已解决]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;
}
以上是关于这个问题的所有解决方法,希望对你有所帮助。可以在评论区留下你的想法,也可以说一下哪个对你有用。
边栏推荐
- Idea auto Guide
- Database learning notes I
- Double linked list -- tail interpolation construction (C language)
- Timer code guide in optee
- loading view时,后面所有东西屏蔽
- HEVC学习之码流分析
- SOC wireless charging scheme
- 51 single chip microcomputer project design: schematic diagram of timed pet feeding system (LCD 1602, timed alarm clock, key timing) Protues, KEIL, DXP
- Reflection example of ads2020 simulation signal
- Method of measuring ripple of switching power supply
猜你喜欢

Oracle 19C local listener configuration error - no listener

Baoyan postgraduate entrance examination interview - Network

RF filter

Embedded Software Engineer (6-15k) written examination interview experience sharing (fresh graduates)

Test method - decision table learning

Uploading pictures with FileReader object

51 MCU project design: Based on 51 MCU clock perpetual calendar

Crawler case 1: JS reversely obtains HD Wallpapers of minimalist Wallpapers

Rewrite string() method in go language

XXL job configuration alarm email notification
随机推荐
STM32 encountered problems using encoder module (library function version)
swift 代码实现方法调用
Go language shallow copy and deep copy
Idea update
Why are you impetuous
51 MCU project design: Based on 51 MCU clock perpetual calendar
Discrete device ~ resistance capacitance
MySQL practice: 4 Operation of data
MFC writes a suggested text editor
监听iPad键盘显示和隐藏事件
Interpretation of x-vlm multimodal model
Introduction of laser drive circuit
Use of jupyter notebook
Can the encrypted JS code and variable name be cracked and restored?
批量修改文件名
Method of measuring ripple of switching power supply
加密的JS代码,变量名能破解还原吗?
STM32 based d18s20 (one wire)
JS file message invalid character error
Getting started with idea