TabLayout+ViewPager2的簡單使用詳解
本文實(shí)例為大家分享了TabLayout+ViewPager2簡單使用的實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
學(xué)習(xí)之前我們先看一下顯示的效果

這里顯示的底部導(dǎo)航欄,如果想實(shí)現(xiàn)的頂部導(dǎo)航欄,只需要調(diào)整一下TabLayout的位置即可。
1、導(dǎo)入依賴
使用ViewPager2之前需要先導(dǎo)入依賴,這里的依賴可能不是最新的,可以自己查找最新的版本。TabLayout不需要導(dǎo)入。
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的布局位置,就可以實(shí)現(xiàn)它是底部導(dǎo)航還是頂部導(dǎo)航了,需要注意一下viewPager和TabLayout的布局,不要讓它們產(chǎn)生重疊。
3、使用方法
viewPager需要為其提供一個(gè)適配器,通過setAdapter()方法添加適配器,這個(gè)適配器它必須繼承自FragmentStateAdapter, 這里一定要用寫一個(gè)類去繼承FragmentStateAdapter,否則會(huì)出現(xiàn)奇怪的錯(cuò)誤, 適配器中主要完成頁面的設(shè)置。
class ViewPager2Adapter extends FragmentStateAdapter{
? ? public ViewPager2Adapter(@NonNull FragmentActivity fragmentActivity) {
? ? ? ? super(fragmentActivity);
? ? }
? ? @NonNull
? ? @Override
? ? public Fragment createFragment(int position) {
? ? ? ? // 每個(gè)頁面對(duì)應(yīng)的fragment
? ? ? ? switch (position){
? ? ? ? ?? ?// 這里我隨便寫了兩個(gè)Fragment
? ? ? ? ? ? case 0:return new Fragment1();
? ? ? ? ? ? case 1:return new Fragment2();
? ? ? ? }
? ? ? ? return null;
? ? }
? ? @Override
? ? public int getItemCount() {
? ? ? ? // 有幾個(gè)頁面就返回幾
? ? ? ? return 2;
? ? }
}創(chuàng)建一個(gè)適配器實(shí)例設(shè)置為ViewPager2對(duì)象。
代碼中為TabLayout添加Tab,如果在布局中已經(jīng)添加就不需要了
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();到這里,基本上就可以進(jìn)行展示了,可以進(jìn)行滑動(dòng)切換和點(diǎn)擊導(dǎo)航欄切換,下面繼續(xù)看看其他功能。
5、tabLayout添加一個(gè)監(jiān)聽器
監(jiān)聽各個(gè)頁面之間的切換以做出不同的處理。
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的顯示樣式
很多時(shí)候我們會(huì)覺得tabLayout導(dǎo)航欄只能顯示文字太單調(diào)了,因此想要為其添加其他樣式,比如添加圖+文字的顯示效果,步驟如下:
1、自定義一個(gè)底部導(dǎo)航的顯示樣式,比如圖片+文字
<?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、我們再定義一個(gè)方法,該方法可以根據(jù)不同的位置構(gòu)造返回不同的底部顯示樣式
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);
? ? // 這個(gè)icons就是一個(gè)簡單的圖片保存的數(shù)組,保存如R.drawable.touxiang
? ? imageView.setImageResource(icons[position]);
? ? textView.setText(tabs[position]);
? ? return view;
}3、在TabLayoutMediator中為每個(gè)tab設(shè)置不同的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還有許多的屬性可以設(shè)置,這里就不一一的展示了。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- TabLayout+ViewPager實(shí)現(xiàn)切頁的示例代碼
- TabLayout實(shí)現(xiàn)ViewPager指示器的方法
- Android 中基于TabLayout+ViewPager實(shí)現(xiàn)標(biāo)簽卡效果
- TabLayout關(guān)聯(lián)ViewPager后不顯示文字的解決方法
- Android中TabLayout+ViewPager實(shí)現(xiàn)tab和頁面聯(lián)動(dòng)效果
- Android中TabLayout+ViewPager 簡單實(shí)現(xiàn)app底部Tab導(dǎo)航欄
- Android中TabLayout結(jié)合ViewPager實(shí)現(xiàn)頁面切換
- Android中TabLayout結(jié)合ViewPager實(shí)現(xiàn)頁面切換效果
- AndroidUI組件SlidingTabLayout實(shí)現(xiàn)ViewPager頁滑動(dòng)效果
相關(guān)文章
Android時(shí)間對(duì)話框TimePickerDialog詳解
這篇文章主要為大家詳細(xì)介紹了Android時(shí)間對(duì)話框TimePickerDialog的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
圖解 Kotlin SharedFlow 緩存系統(tǒng)及示例詳解
這篇文章主要為大家介紹了圖解 Kotlin SharedFlow 緩存系統(tǒng)及示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Android studio刪除Android項(xiàng)目方法
在本篇內(nèi)容里我們給大家介紹的是關(guān)于Android studio刪除Android項(xiàng)目方法和步驟,需要的可以學(xué)習(xí)下。2018-12-12
Android簡單實(shí)現(xiàn)動(dòng)態(tài)權(quán)限獲取相機(jī)權(quán)限及存儲(chǔ)空間等多權(quán)限
這篇文章主要介紹了Android簡單實(shí)現(xiàn)動(dòng)態(tài)權(quán)限獲取相機(jī)權(quán)限及存儲(chǔ)空間等多權(quán)限,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07
android計(jì)算器實(shí)現(xiàn)兩位數(shù)的加減乘除
這篇文章主要為大家詳細(xì)介紹了android計(jì)算器實(shí)現(xiàn)兩位數(shù)的加減乘除,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
完美實(shí)現(xiàn)ExpandableListView二級(jí)分欄效果
這篇文章主要為大家詳細(xì)介紹了ExpandableListView實(shí)現(xiàn)二級(jí)分欄效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11

