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需要為其提供一個適配器,通過setAdapter()方法添加適配器,這個適配器它必須繼承自FragmentStateAdapter, 這里一定要用寫一個類去繼承FragmentStateAdapter,否則會出現(xiàn)奇怪的錯誤, 適配器中主要完成頁面的設(shè)置。
class ViewPager2Adapter extends FragmentStateAdapter{ ? ? public ViewPager2Adapter(@NonNull FragmentActivity fragmentActivity) { ? ? ? ? super(fragmentActivity); ? ? } ? ? @NonNull ? ? @Override ? ? public Fragment createFragment(int position) { ? ? ? ? // 每個頁面對應(yīng)的fragment ? ? ? ? switch (position){ ? ? ? ? ?? ?// 這里我隨便寫了兩個Fragment ? ? ? ? ? ? case 0:return new Fragment1(); ? ? ? ? ? ? case 1:return new Fragment2(); ? ? ? ? } ? ? ? ? return null; ? ? } ? ? @Override ? ? public int getItemCount() { ? ? ? ? // 有幾個頁面就返回幾 ? ? ? ? return 2; ? ? } }
創(chuàng)建一個適配器實(shí)例設(shè)置為ViewPager2對象。
代碼中為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)行滑動切換和點(diǎn)擊導(dǎo)航欄切換,下面繼續(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導(dǎo)航欄只能顯示文字太單調(diào)了,因此想要為其添加其他樣式,比如添加圖+文字的顯示效果,步驟如下:
1、自定義一個底部導(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、我們再定義一個方法,該方法可以根據(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); ? ? // 這個icons就是一個簡單的圖片保存的數(shù)組,保存如R.drawable.touxiang ? ? imageView.setImageResource(icons[position]); ? ? textView.setText(tabs[position]); ? ? return view; }
3、在TabLayoutMediator中為每個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)容,希望對大家的學(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)動效果
- 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頁滑動效果
相關(guān)文章
Android 手機(jī)防止休眠的兩種實(shí)現(xiàn)方法
這篇文章主要介紹了Android 手機(jī)防止休眠方法的相關(guān)資料,一種是在Manifest.xml文件里面聲明,另外一種方法是在代碼里面修改LayoutParams的標(biāo)志位,需要的朋友可以參考下2017-08-08Android實(shí)現(xiàn)底部帶刻度的進(jìn)度條樣式
由于公司需要一個帶刻度的進(jìn)度條樣式,因?yàn)榭潭刃枰獎討B(tài)去改變,所以換背景圖片的方案肯定是不行的,唯一的辦法就是自己繪制一個進(jìn)度條,下面小編給大家?guī)砹薃ndroid實(shí)現(xiàn)底部帶刻度的進(jìn)度條樣式及實(shí)例代碼,需要的朋友參考下吧2019-10-10詳解Android更改APP語言模式的實(shí)現(xiàn)過程
本文詳細(xì)介紹如何更改Android中APP的語言模式,這個功能對于大家開發(fā)Android APP很有幫助,本文運(yùn)用文字介紹和代碼示例把過程寫的很詳細(xì),有需要的可以參考借鑒。2016-08-08Flutter?Widget之FutureBuilder使用示例詳解
這篇文章主要為大家介紹了Flutter?Widget之FutureBuilder使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Kotlin方法與Lambda表達(dá)式實(shí)踐使用介紹
這篇文章主要介紹了Kotlin方法與Lambda表達(dá)式實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09Android TextView的TextWatcher使用案例詳解
這篇文章主要介紹了Android TextView的TextWatcher使用案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Android應(yīng)用獲取設(shè)備序列號的方法
本篇文章主要介紹了Android應(yīng)用獲取設(shè)備序列號的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06