BottomNavigationView(2)和viewpage组合
implementation 'com.google.android.material:material:1.2.1'
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.viewpager.widget.ViewPager android:id="@+id/viewPage" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginTop="0dp" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="0dp" app:layout_constraintBottom_toTopOf="@+id/navigation" /> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?android:attr/windowBackground" android:layout_alignParentBottom="true" app:layout_constraintBottom_toBottomOf="parent" app:menu="@menu/menu2" /> </androidx.constraintlayout.widget.ConstraintLayout>
public class MainActivity extends AppCompatActivity { private ViewPager viewPager; private BottomNavigationView navigation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { viewPager = (ViewPager) findViewById(R.id.viewPage); ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(adapter); navigation = (BottomNavigationView)findViewById(R.id.navigation); // 重新点击监听(即点击目前选中的Tab时触发) navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { Log.e("ee","Selected Item:"+item.getTitle()); onTabSelected(item.getItemId()); return true; } }); onTabSelected(R.id.navigation_home); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @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) { } }); } public void onTabSelected(int id) { FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction= manager.beginTransaction(); Fragment fragment = null; switch (id) { case R.id.navigation_home: viewPager.setCurrentItem(0); break; case R.id.navigation_dashboard: viewPager.setCurrentItem(1); break; case R.id.navigation_notifications: viewPager.setCurrentItem(2); break; case R.id.navigation_setting: viewPager.setCurrentItem(3); break; } } // /** // * 禁用item 缩放动画(选中放大图标/文字 ---->> 图片静止不再放大, 文字会有细微的放大效果) // * 出处: https://blog.csdn.net/zzyawei/article/details/81082257 // */ // @SuppressLint("RestrictedApi") // public static void disableItemScale(BottomNavigationView view) { // try { // BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0); // // Field mLargeLabelField = BottomNavigationItemView.class.getDeclaredField("mLargeLabel"); // Field mSmallLabelField = BottomNavigationItemView.class.getDeclaredField("mSmallLabel"); // Field mShiftAmountField = BottomNavigationItemView.class.getDeclaredField("mShiftAmount"); // Field mScaleUpFactorField = BottomNavigationItemView.class.getDeclaredField("mScaleUpFactor"); // Field mScaleDownFactorField = BottomNavigationItemView.class.getDeclaredField("mScaleDownFactor"); // // mSmallLabelField.setAccessible(true); // mLargeLabelField.setAccessible(true); // mShiftAmountField.setAccessible(true); // mScaleUpFactorField.setAccessible(true); // mScaleDownFactorField.setAccessible(true); // // final float fontScale = view.getResources().getDisplayMetrics().scaledDensity; // // for (int i = 0; i < menuView.getChildCount(); i++) { // BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(i); // // TextView lagerObj = (TextView) mLargeLabelField.get(itemView); // TextView smallObj = (TextView) mSmallLabelField.get(itemView); // lagerObj.setTextSize(smallObj.getTextSize() / fontScale + 0.5f); // // mShiftAmountField.set(itemView, 0); // mScaleUpFactorField.set(itemView, 1f); // mScaleDownFactorField.set(itemView, 1f); // // itemView.setChecked(itemView.getItemData().isChecked()); // } // } catch (Exception e) { // e.printStackTrace(); // } // } }
public class ViewPagerAdapter extends FragmentPagerAdapter { private List<Fragment> fragmentList = new ArrayList<>(); public ViewPagerAdapter(FragmentManager fm) { super(fm); fragmentList.add( new BlankFragment1() ); fragmentList.add( new BlankFragment2() ); fragmentList.add( new BlankFragment3() ); fragmentList.add( new BlankFragment4() ); } @Override public Fragment getItem(int i) { return fragmentList.get( i ); } @Override public int getCount() { return fragmentList.size(); } }
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/navigation_home" android:icon="@drawable/bottom1" android:title="home" /> <item android:id="@+id/navigation_dashboard" android:icon="@drawable/bottom2" android:title="dashboard" /> <item android:id="@+id/navigation_notifications" android:icon="@drawable/bottom3" android:title="notifications" /> <item android:id="@+id/navigation_setting" android:icon="@drawable/bottom4" android:title="share" /> </menu>