1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener ,BlankFragment.OnFragmentInteractionListener,BlankFragment2.OnFragmentInteractionListener,BlankFragment3.OnFragmentInteractionListener{ private BottomNavigationView navigation; private ViewPager viewpager; private List<Fragment> fragmentList =new ArrayList<Fragment>();
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { viewpager.setCurrentItem(item.getOrder()); return false; } };
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
viewpager =(ViewPager)findViewById(R.id.viewpager); viewpager.addOnPageChangeListener(this);
Fragment fragment1 = new BlankFragment(); Fragment fragment2 = new BlankFragment2(); Fragment fragment3 = new BlankFragment3(); fragmentList.add(fragment1); fragmentList.add(fragment2); fragmentList.add(fragment3);
viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return fragmentList.get(position); }
@Override public int getCount() { return fragmentList.size(); } }); }
@Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override public void onPageSelected(int position) { navigation.getMenu().getItem(position).setChecked(true); }
@Override public void onPageScrollStateChanged(int state) {
}
@Override public void onFragmentInteraction(Uri uri) {
} }
|