当前位置:网站首页>[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 .
边栏推荐
- Transformers loading Roberta to implement sequence annotation task
- What is Qi certification Qi certification process
- Getting started with idea
- Using transformers of hugging face to realize multi label text classification
- 教你几招:30句哄女孩的“霸道”温馨话,不看后悔!
- I Summary Preface
- Batch execute SQL file
- STM32 project design: smart home system design based on stm32
- Koa_mySQL_Ts 的整合
- 在 KubeSphere 部署 Wiki 系统 wiki.js 并启用中文全文检索
猜你喜欢

And are two numbers of S

HEVC学习之码流分析

OpenCV Learning notes iii

2020-10-17

Opencv learning notes 3

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

RF filter
![[postgraduate entrance examination] group planning exercises: memory](/img/ac/5c63568399f68910a888ac91e0400c.png)
[postgraduate entrance examination] group planning exercises: memory

Relationship extraction -- casrel

Interpretation of x-vlm multimodal model
随机推荐
SOC wireless charging scheme
Time functions supported in optee
你为什么会浮躁
xxl-job配置告警邮件通知
Esp8266wifi module tutorial: punctual atom atk-esp8266 for network communication, single chip microcomputer and computer, single chip microcomputer and mobile phone to send data
RF filter
Introduction of laser drive circuit
Go language shallow copy and deep copy
Vs2019-mfc setting edit control and static text font size
Design of reverse five times voltage amplifier circuit
"System error 5 occurred when win10 started mysql. Access denied"
Fabrication of modulation and demodulation circuit
STM32 project design: an e-reader making tutorial based on stm32f4
Project practice: parameters of pycharm configuration for credit card digital recognition and how to use opencv in Anaconda
Assembly led on
Discrete device ~ resistance capacitance
批量修改文件名
Relationship extraction -- casrel
VS2005 compiles libcurl to normaliz Solution of Lib missing
When loading view, everything behind is shielded