iT邦幫忙

2022 iThome 鐵人賽

DAY 26
0
Mobile Development

Android studio 30天新手筆記系列 第 26

Day26-Android新手筆記-BottomNavigationView+ViewPage2+Fragment

  • 分享至 

  • xImage
  •  

前幾篇介紹過Fragment、BottomNavigationView與ViewPage,本篇將三篇內容結合。BottomNavigationView可以與ViewPage連動,當ViewPage滑動切換時,BottomNavigationView的標籤也會跟著變動。

BottomNavigationView內容可以參考前幾篇文章。

/images/emoticon/emoticon31.gif

建立Fragment

建立以下三個Fragment:

  • fragment_a.xml
<?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>
  • fragment_b.xml
<?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>
  • fragment_c.xml
<?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>

添加ViewPage與BottomNavigationView

  • ViewPage
<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" />
  • BottomNavigationView
<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內容可以參考前幾篇文章。

建立Menu選單


於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"

建立PageAdapter

建立一個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;
    }
}

於MainActivity中綁定元件與加入事件

將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

  • activity_main
<?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>

/images/emoticon/emoticon41.gif


上一篇
Day25-Android新手筆記-TabLayout+ViewPage
下一篇
Day27-Android新手筆記-Glide基本介紹
系列文
Android studio 30天新手筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言