当前位置:网站首页>[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 .
边栏推荐
猜你喜欢

static const与static constexpr的类内数据成员初始化

1GHz active probe DIY

Project practice: parameters of pycharm configuration for credit card digital recognition and how to use opencv in Anaconda

Relation extraction model -- spit model

Jupyter的安装

Stream analysis of hevc learning

Oracle 19C download installation steps

鲸会务为活动现场提供数字化升级方案
![[postgraduate entrance examination] group planning exercises: memory](/img/ac/5c63568399f68910a888ac91e0400c.png)
[postgraduate entrance examination] group planning exercises: memory

51 MCU project design: Based on 51 MCU clock perpetual calendar
随机推荐
The principle and function of focus
Example of offset voltage of operational amplifier
h5 localStorage
2020-10-20
Project practice: parameters of pycharm configuration for credit card digital recognition and how to use opencv in Anaconda
Undefined symbols for architecture i386 is related to third-party compiled static libraries
[postgraduate entrance examination] group planning: interrupted
[已解决]setOnNavigationItemSelectedListener()被弃用
The best time to buy and sell stocks to get the maximum return
Ltp-- extract time, person and place
Recovering the system with Clonezilla USB disk
static const与static constexpr的类内数据成员初始化
Microcontroller from entry to advanced
golang json unsupported value: NaN 处理
How to Use Instruments in Xcode
Comparison between Apple Wireless charging scheme and 5W wireless charging scheme
And are two numbers of S
How to Use Instruments in Xcode
1GHz active probe DIY
STM32 project design: smart door lock PCB and source code based on stm32f1 (4 unlocking methods)