Android中TabLayout+ViewPager實(shí)現(xiàn)tab和頁(yè)面聯(lián)動(dòng)效果
TabLayout+ViewPager實(shí)現(xiàn)tab和頁(yè)面聯(lián)動(dòng)效果
xml中:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.TabLayout android:id="@+id/toolbar_tl_tab" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:layout_gravity="bottom" app:layout_scrollFlags="scroll" app:tabIndicatorColor="@android:color/holo_green_light" app:tabSelectedTextColor="@android:color/holo_green_light" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#f0f0f0" /> <android.support.v4.view.ViewPager android:id="@+id/vp_container" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
代碼中使用:
public class MainActivity extends AppCompatActivity { private TabLayout toolbar_tl_tab; private ViewPager vp_container; private String[] titles = {"標(biāo)題1", "標(biāo)題2", "標(biāo)題3", "標(biāo)題4"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { toolbar_tl_tab = (TabLayout) findViewById(R.id.toolbar_tl_tab); vp_container = (ViewPager) findViewById(R.id.vp_container); toolbar_tl_tab.setupWithViewPager(vp_container); toolbar_tl_tab.setTabMode(TabLayout.MODE_SCROLLABLE); vp_container.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return new PageFragment(); } @Override public CharSequence getPageTitle(int position) { return titles[position]; } @Override public int getCount() { return titles.length; } }); } }
碎片:PageFragment
public class PageFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_page, null); return view; } }
碎片xml:fragment_page.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="頁(yè)面" /> </LinearLayout>
注意:
1、模式相關(guān)
使用滾動(dòng)模式,特點(diǎn)是超過(guò)屏幕可以滾動(dòng)顯示:
toolbar_tl_tab.setTabMode(TabLayout.MODE_SCROLLABLE);
使用屏幕等分模式,特點(diǎn)是顯示tab的寬度是屏幕等分后的寬度:
toolbar_tl_tab.setTabMode(TabLayout.MODE_FIXED);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- TabLayout+ViewPager實(shí)現(xiàn)切頁(yè)的示例代碼
- TabLayout實(shí)現(xiàn)ViewPager指示器的方法
- Android 中基于TabLayout+ViewPager實(shí)現(xiàn)標(biāo)簽卡效果
- TabLayout關(guān)聯(lián)ViewPager后不顯示文字的解決方法
- Android中TabLayout+ViewPager 簡(jiǎn)單實(shí)現(xiàn)app底部Tab導(dǎo)航欄
- Android中TabLayout結(jié)合ViewPager實(shí)現(xiàn)頁(yè)面切換
- Android中TabLayout結(jié)合ViewPager實(shí)現(xiàn)頁(yè)面切換效果
- AndroidUI組件SlidingTabLayout實(shí)現(xiàn)ViewPager頁(yè)滑動(dòng)效果
- TabLayout+ViewPager2的簡(jiǎn)單使用詳解
相關(guān)文章
詳解Android的Socket通信、List加載更多、Spinner下拉列表
本文主要對(duì)Android的Socket通信、List加載更多、Spinner下拉列表進(jìn)行案例分析。具有很好的參考價(jià)值,需要的朋友一起來(lái)看下吧2016-12-12Android中實(shí)現(xiàn)密碼的隱藏和顯示的示例
本篇文章主要介紹了Android中實(shí)現(xiàn)密碼的隱藏和顯示的示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09Android 用Time和Calendar獲取系統(tǒng)當(dāng)前時(shí)間源碼分享(年月日時(shí)分秒周幾)
這篇文章主要介紹了Android 用Time和Calendar獲取系統(tǒng)當(dāng)前時(shí)間源碼分享,包括年月日時(shí)分秒周幾的源碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01Android實(shí)現(xiàn)Unity3D下RTMP推送的示例
像Unity3D下的RTMP或RTSP播放器一樣,好多開發(fā)者苦于在Unity環(huán)境下,如何高效率低延遲的把數(shù)據(jù)采集并編碼實(shí)時(shí)推送到流媒體服務(wù)器,實(shí)現(xiàn)Unity場(chǎng)景下的低延遲推拉流方案。本文介紹幾種RTMP推送的方案2021-06-06Android onMeasure與onDraw及自定義屬性使用示例
這篇文章主要介紹了Android onMeasure與onDraw及自定義屬性使用示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-02-02Android開發(fā)之圖片旋轉(zhuǎn)功能實(shí)現(xiàn)方法【基于Matrix】
這篇文章主要介紹了Android開發(fā)之圖片旋轉(zhuǎn)功能實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android基于matrix矩陣操作圖形變換的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09基于Rxjava實(shí)現(xiàn)輪詢定時(shí)器
這篇文章主要為大家詳細(xì)介紹了基于Rxjava實(shí)現(xiàn)輪詢定時(shí)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06