0

I want to add clickListener to all specific item item1, item2, item3 and I want to set separate fragment on this. can anyone please help me to set onClickListener in every item.

This is my code:

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

            }
        });


    }
}
MBT
  • 21,733
  • 19
  • 84
  • 102
Shrawan Thakur
  • 829
  • 7
  • 8
  • Do your stuff - change fragment inside onTabSelected method. There is no onclicklistener you can call to change fragment inside onTabselected based on position. You can find demo for this library with clean explanation https://github.com/aurelhubert/ahbottomnavigation/tree/master/demo – Mohamed Mohaideen AH Sep 06 '18 at 14:33

1 Answers1

1

I've been checking the library and i think you can handle the position of the items like that. But you cant add a listener for every item.

Code:

 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;
            }
        });
Juanjo Berenguer
  • 789
  • 1
  • 5
  • 8