我想将clickListener
添加到所有特定的item item1, item2, item3
中,并且我想为此设置单独的片段。有人可以帮我在每个项目中设置onClickListener
。
这是我的代码:
public class BottomNavigation extends AppCompatActivity {
private FrameLayout frameLayout;
private AHBottomNavigation bottomNavigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_navigation);
frameLayout = (FrameLayout) findViewById(R.id.main_frame);
final AHBottomNavigation bottomNavigation=(AHBottomNavigation) findViewById(R.id.bottom_navigation);
final AHBottomNavigationItem item1 =
new AHBottomNavigationItem("Home",
R.drawable.ic_home_black_24dp);
AHBottomNavigationItem item2 =
new AHBottomNavigationItem("Home",
R.drawable.ic_home_black_24dp);
final AHBottomNavigationItem item3 =
new AHBottomNavigationItem("Home",
R.drawable.ic_home_black_24dp);
AHBottomNavigationItem item4 =
new AHBottomNavigationItem("Home",
R.drawable.ic_home_black_24dp);
AHBottomNavigationItem item5 =
new AHBottomNavigationItem("Home",
R.drawable.ic_home_black_24dp);
bottomNavigation.addItem(item1); //specific listener for all these items
bottomNavigation.addItem(item5);
bottomNavigation.addItem(item4);
bottomNavigation.addItem(item3);
bottomNavigation.addItem(item2);
bottomNavigation.setTitleState(AHBottomNavigation.TitleState.ALWAYS_SHOW);
bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
@Override
public void onTabSelected(int position, boolean wasSelected) {
// fragment.updateColor(Color.parseColor(colors[position]));
}
});
}
}
答案 0 :(得分:0)
我一直在检查图书馆,我认为您可以处理类似项目的位置。但是您不能为每个项目添加一个侦听器。
代码:
bottomNavigation.setOnTabSelectedListener((position, wasSelected) -> {
switch (position){
case 0: /*Do whatever you want here*/ return true; //1 tab
case 1: /*Do whatever you want here*/ return true; //2 tab
case 2: /*Do whatever you want here*/ return true; //3 tab
case 3: /*Do whatever you want here*/ return true; //4 tab
case 4: /*Do whatever you want here*/ return true; //4 tab
default: return false;
}
});