在特定项目中设置OnClickListener(所有项目分开)?

问题描述 投票:0回答:1

我想将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]));

            }
        });


    }
}
android tabs bottomnavigationview bottom-sheet
1个回答
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;
            }
        });
© www.soinside.com 2019 - 2024. All rights reserved.