当前位置:网站首页>[resolved]setonnavigationitemselectedlistener() deprecated
[resolved]setonnavigationitemselectedlistener() deprecated
2022-06-26 08:31:00 【CyberESec】
Developed in me app In the process of , I used it setOnNavigationItemSelectedListener() This object , But now I have a problem
setOnNavigationItemSelectedListener(com.google.android.material.bottomnavigation.BottomNavigationView.OnNavigationItemSelectedListener)
deprecated , So here are some possible solutions .
How to solve setOnNavigationItemSelectedListener Problems caused by abandonment ?
You can try it setonItemSelectedListener, Its usage and setOnNavigationItemSelectedListener() It's the same
Method 1 : use setonItemSelectedListener Replace
java Examples :
bnv.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch(id){
//check id
}
return true;
}
});
kotlin Examples :
bnv.setOnItemSelectedListener {
item ->
when (item.itemId) {
}
true
}
java Examples 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;
}
});
}
}
Method 2 : Use base classes
/** * 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;
}
These are all the solutions to this problem , I hope it helped you . You can leave your thoughts in the comments section , You can also say which is useful to you .
边栏推荐
- Realizing sequence annotation with transformers
- (3) Dynamic digital tube
- Discrete device ~ resistance capacitance
- VS2005 project call free() compiled with static libcurl library reported heap error
- Installation of jupyter
- JS file message invalid character error
- STM32 project design: temperature, humidity and air quality alarm, sharing source code and PCB
- Oracle database self study notes
- Batch modify file name
- Interpretation of x-vlm multimodal model
猜你喜欢

STM32 porting mpu6050/9250 DMP official library (motion_driver_6.12) modifying and porting DMP simple tutorial

2020-10-29

2020-10-20

Method of measuring ripple of switching power supply

FFmpeg音视频播放器实现

MySQL practice: 1 Common database commands

1. error using XPath to locate tag

鲸会务为活动现场提供数字化升级方案

(vs2019 MFC connects to MySQL) make a simple login interface (detailed)

"System error 5 occurred when win10 started mysql. Access denied"
随机推荐
批量修改文件名
Introduction of laser drive circuit
[postgraduate entrance examination] group planning exercises: memory
Analysis of internal circuit of operational amplifier
鲸会务一站式智能会议系统帮助主办方实现数字化会议管理
Necessary protection ring for weak current detection
Koa_mySQL_Ts 的整合
opencv学习笔记三
(5) Matrix key
Opencv learning notes 3
h5 localStorage
Idea automatically sets author information and date
Cause analysis of serial communication overshoot and method of termination
加密的JS代码,变量名能破解还原吗?
Flume learning notes
Installation of jupyter
OpenCV Learning notes iii
RF filter
Torch model to tensorflow
STM32 porting mpu6050/9250 DMP official library (motion_driver_6.12) modifying and porting DMP simple tutorial