aurelhubert / ahbottomnavigation

A library to reproduce the behavior of the Bottom Navigation guidelines from Material Design.
3.83k stars 682 forks source link

getItemViewPosition always return null but not under OnTabSelectedListener #373

Open yjj781265 opened 6 years ago

yjj781265 commented 6 years ago

I found out something interesting the getItemViewPosition always return null after added all the items, but it won't return null if I put under setOnTabSelectedListener. Here is the code snippet homepageTab.setTitleState(AHBottomNavigation.TitleState.ALWAYS_SHOW); homepageTab.setAccentColor(getResources().getColor(R.color.colorPrimary)); homepageTab.setForceTint(true);

    AHBottomNavigationItem item1 = new AHBottomNavigationItem("Questions",
            R.drawable.survey_tab_icon);
    AHBottomNavigationItem item2 = new AHBottomNavigationItem("Nearby",R.drawable.peoplenearby );

    AHBottomNavigationItem item3 = new AHBottomNavigationItem("Chat",
            R.drawable.message_selector);
    AHBottomNavigationItem item4 = new AHBottomNavigationItem("Me", R.drawable.me_selector);

    ArrayList< AHBottomNavigationItem> itemArrayList = new ArrayList<>();
    itemArrayList.add(item1);
    itemArrayList.add(item2);
    itemArrayList.add(item3);
    itemArrayList.add(item4);

    // Add items

    homepageTab.addItems(itemArrayList);

    homepageTab.setCurrentItem(0);

    // for smooth swipe
    viewPager.setOffscreenPageLimit(3);

    Log.d("HomePage123","OutSide tabSelected " +  homepageTab.getViewAtPosition(0) );

    // Set listeners
    homepageTab.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
        @Override
        public boolean onTabSelected(int position, boolean wasSelected) {
            viewPager.setCurrentItem(position,false);
            homepageTab.getViewAtPosition(0);
            Log.d("HomePage123","Under tabSelected " +  homepageTab.getViewAtPosition(0) );
            return true;
        }
    });

Here is the log output : image