android TabLayout使用方法詳解
Google在2015的IO大會上,給我們帶來了更加詳細(xì)的Material Design設(shè)計(jì)規(guī)范,同時,也給我們帶來了全新的Android Design Support Library,在這個support庫里面,Google給我們提供了更加規(guī)范的MD設(shè)計(jì)風(fēng)格的控件。最重要的是,Android Design Support Library的兼容性更廣,直接可以向下兼容到Android 2.2。
這兩天需要做一個仿京東詳情的頁面,上面的Tab切換,以前都是自己寫Viewpager+fragment,或者Indicator的深度定制,一直想嘗試一下TabLayout,于是就有了下面的坑。
然后下面是我簡單的實(shí)現(xiàn)效果(個人覺得很坑,還不如自己自定義的導(dǎo)航器)
添加引用庫
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:24.2.0' compile 'com.android.support:design:24.2.0' compile 'com.android.support:recyclerview-v7:24.2.0' compile 'com.android.support:cardview-v7:24.2.0' }
Toolbar與TabLayout
我們來看一下實(shí)現(xiàn)的布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="48dp" android:gravity="center_vertical" app:navigationIcon="@drawable/back_icon" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal"> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="match_parent" style="@style/style_c7_s20" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:background="@drawable/more_icon" /> </android.support.v7.widget.Toolbar> <View style="@style/horizontal_line" /> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
這布局文件最關(guān)鍵的一點(diǎn)就是android.support.design.widget.TabLayout 標(biāo)簽中的app:tabMode=”scrollable”,他設(shè)置tab的模式為“可滑動的”。
其他的用法和Indicator的用法差不多,都需要設(shè)置適配器,然后通過數(shù)據(jù)實(shí)現(xiàn)頁面的適配。直接上代碼
Adapter
public class ProductDetailPagerAdapter extends FragmentPagerAdapter { private List<String> mTitles; public ProductDetailPagerAdapter(FragmentManager fm, List<String> mTitles) { super(fm); this.mTitles = mTitles; } @Override public Fragment getItem(int position) { if (position == 0) { return new ProductFragment(); } else if (position == 1) { return new ProductDetailFragment(); } return new ProductFragment(); } @Override public int getCount() { return mTitles.size(); } @Override public CharSequence getPageTitle(int position) { return mTitles.get(position); } }
主頁面的相關(guān)邏輯,這里的Fragment就是簡單的Fragment。
public class ProductDetailsActivity extends BaseActivity { @BindView(R.id.viewPager) ViewPager viewPager; @BindView(R.id.tabLayout) TabLayout tabLayout; @BindView(R.id.toolbar) Toolbar toolbar; private TextView tabProduct; private TextView tabDetail; private List<String> mTitles = null; private ProductDetailPagerAdapter productPagerAdapter = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_product_details); ButterKnife.bind(this); init(); } private void init() { initToolbar(); initViewPager(); } private void initToolbar() { setTitle(""); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); initTab(); initTabChange(); } private void initTabChange() { tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); switch (tab.getPosition()){ case 0: tabProduct.setTextColor(getResources().getColor(R.color.c8)); tabProduct.setTextSize(18); break; case 1: tabDetail.setTextColor(getResources().getColor(R.color.c8)); tabDetail.setTextSize(18); break; } } @Override public void onTabUnselected(TabLayout.Tab tab) { tabProduct.setTextColor(getResources().getColor(R.color.c7)); tabDetail.setTextColor(getResources().getColor(R.color.c7)); } @Override public void onTabReselected(TabLayout.Tab tab) { } }); } private void initTab() { tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.c8)); tabLayout.setSelectedTabIndicatorHeight(UIUtils.dp2px(this, 2)); tabLayout.setTabTextColors(R.color.c7, R.color.c8); tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.item_detail_tab_product_layout)); tabProduct= (TextView) findViewById(R.id.tab_product); tabProduct.setTextColor(getResources().getColor(R.color.c8)); tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.item_detail_tab_detail_layout)); tabDetail= (TextView) findViewById(R.id.tab_detail); tabProduct.setTextColor(getResources().getColor(R.color.c7)); } private void initViewPager() { mTitles = new ArrayList<>(); mTitles.add("商品"); mTitles.add("詳情"); productPagerAdapter = new ProductDetailPagerAdapter(getSupportFragmentManager(), mTitles); viewPager.setAdapter(productPagerAdapter); viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { tabLayout.getTabAt(position).select(); } }); } public static void open(Context context) { Intent intent = new Intent(context, ProductDetailsActivity.class); context.startActivity(intent); } }
我相信很多人看了上面的代碼會覺得很麻煩,其實(shí)我也覺得,這種雖然可定制高,但是相對于以前的寫法,代碼絲毫沒有減少,我還是建議使用自定義控件,之前有一篇Android萬能的指示器,大家可以借鑒借鑒。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android TabLayout(選項(xiàng)卡布局)簡單用法實(shí)例分析
- Android TabLayout設(shè)置指示器寬度的方法
- Android中修改TabLayout底部導(dǎo)航條Indicator長短的方法
- Android 自定義View結(jié)合自定義TabLayout實(shí)現(xiàn)頂部標(biāo)簽滑動效果
- Android中TabLayout結(jié)合ViewPager實(shí)現(xiàn)頁面切換效果
- Android自定義TabLayout效果
- android TabLayout的指示器寬度問題
- Android原生TabLayout使用的超全解析(看這篇就夠了)
相關(guān)文章
手勢滑動結(jié)束Activity基本功能的實(shí)現(xiàn)(一)
這篇文章主要為大家詳細(xì)介紹了手勢滑動結(jié)束Activity基本功能的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Android實(shí)現(xiàn)淘寶商品列表切換效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)淘寶商品列表切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Android Studio 3.1.3升級至3.6.1后舊項(xiàng)目的兼容操作方法
這篇文章主要介紹了Android Studio 3.1.3升級至3.6.1后舊項(xiàng)目的兼容操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Android實(shí)戰(zhàn)之Cocos游戲容器搭建
這篇文章主要介紹了Android實(shí)戰(zhàn)之Cocos游戲容器搭建,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-06-06Android 使用ViewPager實(shí)現(xiàn)圖片左右循環(huán)滑動自動播放
這篇文章主要介紹了Android 使用ViewPager實(shí)現(xiàn)圖片左右循環(huán)滑動自動播放的相關(guān)資料,非常不錯,具有參考解決價(jià)值,需要的朋友可以參考下2016-08-08一行代碼教你解決Scrollview和TextInput焦點(diǎn)獲取問題
這篇文章主要為大家介紹了一行代碼教你解決Scrollview和TextInput焦點(diǎn)獲取問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Kotlin協(xié)程Dispatchers原理示例詳解
這篇文章主要為大家介紹了Kotlin協(xié)程Dispatchers原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08