TabLayout+ViewPager2的簡單使用詳解
本文實例為大家分享了TabLayout+ViewPager2簡單使用的實現代碼,供大家參考,具體內容如下
學習之前我們先看一下顯示的效果
這里顯示的底部導航欄,如果想實現的頂部導航欄,只需要調整一下TabLayout的位置即可。
1、導入依賴
使用ViewPager2之前需要先導入依賴,這里的依賴可能不是最新的,可以自己查找最新的版本。TabLayout不需要導入。
implementation "androidx.viewpager2:viewpager2:1.0.0"
2、布局
<androidx.viewpager2.widget.ViewPager2 ? ? android:layout_width="match_parent" ? ? android:layout_height="wrap_content" ? ? app:layout_constraintTop_toBottomOf="@id/tablayout" ? ? android:id="@+id/viewpager2"/> <com.google.android.material.tabs.TabLayout ? ? android:layout_width="match_parent" ? ? android:layout_height="30dp" ? ? app:layout_constraintTop_toTopOf="parent" ? ? app:tabTextAppearance="@style/tabLayoutTheme" ? ? android:id="@+id/tablayout" ? ? app:tabMode="fixed"> </com.google.android.material.tabs.TabLayout>
我們只需要控制TabLayout的布局位置,就可以實現它是底部導航還是頂部導航了,需要注意一下viewPager和TabLayout的布局,不要讓它們產生重疊。
3、使用方法
viewPager需要為其提供一個適配器,通過setAdapter()方法添加適配器,這個適配器它必須繼承自FragmentStateAdapter, 這里一定要用寫一個類去繼承FragmentStateAdapter,否則會出現奇怪的錯誤, 適配器中主要完成頁面的設置。
class ViewPager2Adapter extends FragmentStateAdapter{ ? ? public ViewPager2Adapter(@NonNull FragmentActivity fragmentActivity) { ? ? ? ? super(fragmentActivity); ? ? } ? ? @NonNull ? ? @Override ? ? public Fragment createFragment(int position) { ? ? ? ? // 每個頁面對應的fragment ? ? ? ? switch (position){ ? ? ? ? ?? ?// 這里我隨便寫了兩個Fragment ? ? ? ? ? ? case 0:return new Fragment1(); ? ? ? ? ? ? case 1:return new Fragment2(); ? ? ? ? } ? ? ? ? return null; ? ? } ? ? @Override ? ? public int getItemCount() { ? ? ? ? // 有幾個頁面就返回幾 ? ? ? ? return 2; ? ? } }
創(chuàng)建一個適配器實例設置為ViewPager2對象。
代碼中為TabLayout添加Tab,如果在布局中已經添加就不需要了
for (int i = 0; i < 2; i++) { ? ? tabLayout.addTab(tabLayout.newTab()); }
上面需要特別注意,是tabLayout.newTab().
最后使用TabLayoutMeditor將TabLayout和ViewPager組合起來。
TabLayoutMediator mediator = new TabLayoutMediator(binding.tabLayout, binding.viewPager2, new TabLayoutMediator.TabConfigurationStrategy() { ? ? @Override ? ? public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) { ? ? ? ? tab.setText(tabs[position]); ? ? } }); mediator.attach();
到這里,基本上就可以進行展示了,可以進行滑動切換和點擊導航欄切換,下面繼續(xù)看看其他功能。
5、tabLayout添加一個監(jiān)聽器
監(jiān)聽各個頁面之間的切換以做出不同的處理。
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { ?? ?// 頁面被選中 ? ? @Override ? ? public void onTabSelected(TabLayout.Tab tab) { ? ? ? ? Log.d(TAG, "onTabSelected: "+tab.getText()); ? ? } ?? ?// 頁面切換到其他 ? ? @Override ? ? public void onTabUnselected(TabLayout.Tab tab) { ? ? ? ? Log.d(TAG, "onTabUnselected: "+tab.getText()); ? ? } ? ? @Override ? ? public void onTabReselected(TabLayout.Tab tab) { ? ? } });
6、自定義tablayout的顯示樣式
很多時候我們會覺得tabLayout導航欄只能顯示文字太單調了,因此想要為其添加其他樣式,比如添加圖+文字的顯示效果,步驟如下:
1、自定義一個底部導航的顯示樣式,比如圖片+文字
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? xmlns:app="http://schemas.android.com/apk/res-auto"> ? ? <ImageView ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/icon" ? ? ? ? app:layout_constraintStart_toStartOf="parent" ? ? ? ? app:layout_constraintEnd_toEndOf="parent" ? ? ? ? app:layout_constraintTop_toTopOf="parent"/> ? ? <TextView ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/text" ? ? ? ? app:layout_constraintStart_toStartOf="parent" ? ? ? ? app:layout_constraintEnd_toEndOf="parent" ? ? ? ? app:layout_constraintTop_toBottomOf="@id/icon"/> </androidx.constraintlayout.widget.ConstraintLayout>
2、我們再定義一個方法,該方法可以根據不同的位置構造返回不同的底部顯示樣式
private View getViewAtI(int position){ ? ? View view = getLayoutInflater().inflate(R.layout.bottom_layout, null, false); ? ? ImageView ?imageView = view.findViewById(R.id.icon); ? ? TextView textView = view.findViewById(R.id.text); ? ? // 這個icons就是一個簡單的圖片保存的數組,保存如R.drawable.touxiang ? ? imageView.setImageResource(icons[position]); ? ? textView.setText(tabs[position]); ? ? return view; }
3、在TabLayoutMediator中為每個tab設置不同的CustomView, 就能顯示底部視圖了。
TabLayoutMediator mediator = new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() { ? ? @Override ? ? public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) { ? ? ? ? //tab.setText(tabs[position]); ? ? ? ? tab.setCustomView(getViewAtI(position)); ? ? } }); mediator.attach();
TabLayout還有許多的屬性可以設置,這里就不一一的展示了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- TabLayout+ViewPager實現切頁的示例代碼
- TabLayout實現ViewPager指示器的方法
- Android 中基于TabLayout+ViewPager實現標簽卡效果
- TabLayout關聯(lián)ViewPager后不顯示文字的解決方法
- Android中TabLayout+ViewPager實現tab和頁面聯(lián)動效果
- Android中TabLayout+ViewPager 簡單實現app底部Tab導航欄
- Android中TabLayout結合ViewPager實現頁面切換
- Android中TabLayout結合ViewPager實現頁面切換效果
- AndroidUI組件SlidingTabLayout實現ViewPager頁滑動效果
相關文章
Flutter?Widget之FutureBuilder使用示例詳解
這篇文章主要為大家介紹了Flutter?Widget之FutureBuilder使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11Android TextView的TextWatcher使用案例詳解
這篇文章主要介紹了Android TextView的TextWatcher使用案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-08-08