前幾篇介紹過Fragment、BottomNavigationView與ViewPage,本篇將三篇內容結合。BottomNavigationView可以與ViewPage連動,當ViewPage滑動切換時,BottomNavigationView的標籤也會跟著變動。
BottomNavigationView內容可以參考前幾篇文章。
建立以下三個Fragment:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentA">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="50dp"
android:text="FragmentA" />
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentB">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="50dp"
android:text="FragmentB" />
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentC">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="50dp"
android:text="FragmentC" />
</FrameLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPage"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelVisibilityMode="selected"
app:menu="@menu/menu"
app:itemIconTint="@drawable/sl_color"
app:itemTextColor="@drawable/sl_color"
app:itemRippleColor="@color/teal_200"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
BottomNavigationView內容可以參考前幾篇文章。
於item中設定對應的title、id與icon。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="首頁"
android:id="@+id/na_home"
android:icon="@drawable/ic_baseline_home">
</item>
<item android:title="設定"
android:id="@+id/na_setting"
android:icon="@drawable/ic_baseline_settings">
</item>
<item android:title="設定"
android:id="@+id/na_other"
android:icon="@drawable/ic_baseline_other">
</item>
</menu>
於activity_main.xml中綁定
app:menu="@menu/menu"
建立一個class繼承FragmentStateAdapter
public class PageAdapter extends FragmentStateAdapter {
FragmentA fragmentA;
FragmentB fragmentB;
FragmentC fragmentC;
public PageAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
super(fragmentManager, lifecycle);
}
@NonNull
@Override
public Fragment createFragment(int position) {
//position當前ViewPage編號
switch (position) {
case 0:
fragmentA = new FragmentA();
return fragmentA;
case 1:
fragmentB = new FragmentB();
return fragmentB;
case 2:
fragmentC = new FragmentC();
return fragmentC;
default:
return null;
}
}
@Override
public int getItemCount() {
return 3;
}
}
將viewPager與bottomNavigationView連動
public class MainActivity extends AppCompatActivity {
PageAdapter pageAdapter;
ViewPager2 viewPager;
BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.viewPage);
bottomNavigationView = findViewById(R.id.bottomNavigationView);
pageAdapter = new PageAdapter(getSupportFragmentManager(),getLifecycle());
viewPager.setAdapter(pageAdapter);
//viewPager
viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
switch (position) {
case 0:
bottomNavigationView.setSelectedItemId(R.id.na_home);
break;
case 1:
bottomNavigationView.setSelectedItemId(R.id.na_setting);
break;
case 2:
bottomNavigationView.setSelectedItemId(R.id.na_other);
break;
}
}
});
//bottomNavigationView
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.na_home:
viewPager.setCurrentItem(0);
break;
case R.id.na_setting:
viewPager.setCurrentItem(1);
break;
case R.id.na_other:
viewPager.setCurrentItem(2);
break;
}
return true;
}
});
}
}
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPage"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelVisibilityMode="selected"
app:menu="@menu/menu"
app:itemIconTint="@drawable/sl_color"
app:itemTextColor="@drawable/sl_color"
app:itemRippleColor="@color/teal_200"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>