当前位置:网站首页>[已解决]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;
}
以上是关于这个问题的所有解决方法,希望对你有所帮助。可以在评论区留下你的想法,也可以说一下哪个对你有用。
边栏推荐
- Detailed explanation of SOC multi-core startup process
- How to Use Instruments in Xcode
- Read excel table and render with FileReader object
- CodeBlocks集成Objective-C开发
- swift 代码实现方法调用
- STM32 project design: smart home system design based on stm32
- Batch modify file name
- h5 localStorage
- 批量执行SQL文件
- Pychart connects to Damon database
猜你喜欢
Recognize the interruption of 80s51
Pychart connects to Damon database
JMeter performance testing - Basic Concepts
Discrete device ~ resistance capacitance
StarWar armor combined with scanning target location
MySQL practice: 4 Operation of data
Design of reverse five times voltage amplifier circuit
Uni app installation and project directory (hbuilder configuration)
STM32 project design: smart door lock PCB and source code based on stm32f1 (4 unlocking methods)
. eslintrc. JS configuration
随机推荐
STM32 based d18s20 (one wire)
73b2d wireless charging and receiving chip scheme
Idea automatically sets author information and date
STM32 porting mpu6050/9250 DMP official library (motion_driver_6.12) modifying and porting DMP simple tutorial
Double linked list -- tail interpolation construction (C language)
Vs2019-mfc setting edit control and static text font size
STM32 project design: smart home system design based on stm32
Comparison version number [leetcode]
Learning signal integrity from scratch (SIPI) -- 3 challenges faced by Si and Si based design methods
Database learning notes II
MFC writes a suggested text editor
Test method - decision table learning
How to Use Instruments in Xcode
Analysis of internal circuit of operational amplifier
SOC的多核启动流程详解
51 single chip microcomputer project design: schematic diagram of timed pet feeding system (LCD 1602, timed alarm clock, key timing) Protues, KEIL, DXP
Assembly led on
Software engineering - high cohesion and low coupling
RF filter
GHUnit: Unit Testing Objective-C for the iPhone