Android中TabLayout+ViewPager實(shí)現(xiàn)tab和頁面聯(lián)動效果
TabLayout+ViewPager實(shí)現(xiàn)tab和頁面聯(lián)動效果
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="頁面" />
</LinearLayout>
注意:
1、模式相關(guān)
使用滾動模式,特點(diǎn)是超過屏幕可以滾動顯示:
toolbar_tl_tab.setTabMode(TabLayout.MODE_SCROLLABLE);
使用屏幕等分模式,特點(diǎn)是顯示tab的寬度是屏幕等分后的寬度:
toolbar_tl_tab.setTabMode(TabLayout.MODE_FIXED);
以上就是本文的全部內(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)app底部Tab導(dǎo)航欄
- Android中TabLayout結(jié)合ViewPager實(shí)現(xiàn)頁面切換
- Android中TabLayout結(jié)合ViewPager實(shí)現(xiàn)頁面切換效果
- AndroidUI組件SlidingTabLayout實(shí)現(xiàn)ViewPager頁滑動效果
- TabLayout+ViewPager2的簡單使用詳解
相關(guān)文章
Android自定義view實(shí)現(xiàn)車載可調(diào)整軌跡線
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)車載可調(diào)整軌跡線,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-06-06
Android自定義View仿QQ運(yùn)動步數(shù)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View仿QQ運(yùn)動步數(shù)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11
Android編程實(shí)現(xiàn)隱藏狀態(tài)欄及測試Activity是否活動的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)隱藏狀態(tài)欄及測試Activity是否活動的方法,涉及Android界面布局設(shè)置及Activity狀態(tài)操作的相關(guān)技巧,需要的朋友可以參考下2016-10-10
Android計算器簡單邏輯實(shí)現(xiàn)實(shí)例分享
這篇文章主要介紹了Android計算器簡單邏輯實(shí)現(xiàn)實(shí)例,有需要的朋友可以參考一下2014-01-01
Android學(xué)習(xí)之AppWidget筆記分享
這篇文章主要為大家詳細(xì)介紹了Android學(xué)習(xí)筆記之AppWidget的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-08-08
Android中獲取網(wǎng)頁表單中的數(shù)據(jù)實(shí)現(xiàn)思路及代碼
在Android中獲取網(wǎng)頁里表單中的數(shù)據(jù)具體實(shí)現(xiàn)代碼如下,感興趣的各位可以參考過下哈,希望對大家有所幫助2013-06-06

