当前位置:网站首页>[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 .
边栏推荐
- Idea uses regular expressions for global substitution
- STM32 project design: smart home system design based on stm32
- 三菱PLC若想实现以太网无线通讯,需要具备哪些条件?
- Project practice: parameters of pycharm configuration for credit card digital recognition and how to use opencv in Anaconda
- golang json unsupported value: NaN 处理
- opencv学习笔记三
- Interpretation of x-vlm multimodal model
- Vs2019-mfc setting edit control and static text font size
- h5 localStorage
- Deploy wiki system Wiki in kubesphere JS and enable Chinese full-text retrieval
猜你喜欢

StarWar armor combined with scanning target location

Example of offset voltage of operational amplifier

The difference between push-pull circuit drive and totem pole drive

三菱PLC若想实现以太网无线通讯,需要具备哪些条件?

Cause analysis of serial communication overshoot and method of termination

Calculation of decoupling capacitance

Stream analysis of hevc learning

Idea uses regular expressions for global substitution

Compiling owncloud client on win10

MySQL insert Chinese error
随机推荐
Opencv learning notes 3
Realizing sequence annotation with transformers
GHUnit: Unit Testing Objective-C for the iPhone
Ora-12514: tns: the listener currently does not recognize the service requested in the connection descriptor
你为什么会浮躁
[postgraduate entrance examination planning group] conversion between signed and unsigned numbers
教你几招:30句哄女孩的“霸道”温馨话,不看后悔!
(3) Dynamic digital tube
(4) Independent key
What is Qi certification Qi certification process
Application of wireless charging receiving chip xs016 coffee mixing cup
Design of reverse five times voltage amplifier circuit
MySQL practice: 1 Common database commands
2020-10-29
(5) Matrix key
Can the encrypted JS code and variable name be cracked and restored?
Esp8266wifi module tutorial: punctual atom atk-esp8266 for network communication, single chip microcomputer and computer, single chip microcomputer and mobile phone to send data
2020-10-20
1GHz active probe DIY
The principle and function of focus