Skip to content

Instantly share code, notes, and snippets.

@dyguests
Last active April 11, 2017 13:08
Show Gist options
  • Save dyguests/a775605a38f140a6bab8713950752784 to your computer and use it in GitHub Desktop.
Save dyguests/a775605a38f140a6bab8713950752784 to your computer and use it in GitHub Desktop.
BottomNavigationAttachViewPagerHelper,BottomNavigation,ViewPager

#USAGE

BottomNavigationAttachViewPagerHelper.setup(navigation, viewPager);
package com.fanhl.hbookerauthor.util;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.view.ViewPager;
/**
* BottomNavigationView Attach ViewPager Helper
*
* Created by fanhl on 2017/4/11.
*/
public class BottomNavigationAttachViewPagerHelper {
public static void setup(BottomNavigationView navigation, ViewPager viewPager) {
int naviSize = navigation.getMenu().size();
int pagerSize = viewPager.getAdapter().getCount();
for (int i = 0; i < naviSize; i++) {
int finalI = i;
navigation.getMenu().getItem(i).setOnMenuItemClickListener(item -> {
if (finalI < pagerSize) {
viewPager.setCurrentItem(finalI);
}
return false;
});
}
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position < naviSize) {
navigation.getMenu().getItem(position).setChecked(true);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment