繁体   English   中英

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

[英]set OnClickListener in specific item (seperate for all item)?

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

            }
        });


    }
}

我一直在检查图书馆,我认为您可以处理类似项目的位置。 但是您不能为每个项目添加一个侦听器。

码:

 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;
            }
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM