package com.tools.ttt;
import static android.content.ContentValues.TAG;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;
import com.google.android.material.navigation.NavigationBarView;
import com.tools.qrtestassist.databinding.ActivityMainBinding;
import com.tools.qrtestassist.ui.BindTagsFragment;
import com.tools.qrtestassist.ui.PrintLabelsFragment;
import com.tools.qrtestassist.ui.SearchFragment;
import com.tools.qrtestassist.ui.SettingsFragment;
import com.tools.qrtestassist.ui.StatisticsFragment;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// 不执行任何操作以阻止方向更改
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//固定竖屏模式
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_statistics, R.id.navigation_bind_tags, R.id.navigation_search,R.id.navigation_print_labels,R.id.navigation_settings)
.build();
ViewPager2 viewPager = findViewById(R.id.viewPager);
viewPager.setAdapter(new MyFragmentAdapter(this));
BottomNavigationView navView = findViewById(R.id.nav_view);
navView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
// 删除之前的fragment自动导航管理
// NavController navController = Navigation.findNavController(this, main_fragment);
// NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
// NavigationUI.setupWithNavController(binding.navView, navController);
viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
// 页面选中时执行操作
BottomNavigationView navView = findViewById(R.id.nav_view);
Log.i(TAG, "onPageSelected: " + position);
navView.getMenu().getItem(position).setChecked(true);
MainActivity activity = MainActivity.this;
activity.setTitle(navView.getMenu().getItem(position).getTitle());
}
});
navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// 获取外部 Activity 对象
MainActivity activity = MainActivity.this;
int itemId = item.getItemId(); // 将结果存储在常量中
activity.setTitle(item.getTitle());
if (itemId == R.id.navigation_A) {
viewPager.setCurrentItem(0);
} else if (itemId == R.id.navigation_B) {
viewPager.setCurrentItem(1);
} else if (itemId == R.id.navigation_C) {
viewPager.setCurrentItem(2);
} else if (itemId == R.id.navigation_D) {
viewPager.setCurrentItem(3);
} else if (itemId == R.id.navigation_E) {
viewPager.setCurrentItem(4);
}
return true;
}
});
}
private static class MyFragmentAdapter extends FragmentStateAdapter {
private static final int NUM_PAGES = 5;
public MyFragmentAdapter(FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
@Override
public Fragment createFragment(int position) {
switch (position) {
case 0:
return new A_Fragment();
case 1:
return new B_Fragment();
case 2:
return new C_Fragment();
case 3:
return new D_Fragment();
case 4:
return new E_Fragment();
default:
return new F_Fragment();
}
}
@Override
public int getItemCount() {
return NUM_PAGES;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
标签:菜单,androidx,Demo,com,import,android,navigation,id
From: https://www.cnblogs.com/zhaogaojian/p/17724100.html