TabLayout實(shí)現(xiàn)ViewPager指示器的方法
在TabLayout出現(xiàn)之前,基本都是通過(guò) ViewPager+FragmentPagerAdapter+第三方開(kāi)源tab指示器(TabPageIndicator)來(lái)實(shí)現(xiàn)的?,F(xiàn)在Android內(nèi)部提供了現(xiàn)成的TabLayout控件來(lái)實(shí)現(xiàn)ViewPager指示器的效果。
先看效果圖:
導(dǎo)入依賴
在Gradle文件中導(dǎo)入依賴,代碼如下:
compile 'com.android.support:design:23.4.0'
TabLayout類就在這個(gè)依賴包中定義的。
布局文件中使用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="tech.czh.example.MainActivity"> <android.support.design.widget.TabLayout android:id="@+id/tablayout" android:layout_width="match_parent" android:layout_height="50dp" android:layout_gravity="top"/> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white"> </android.support.v4.view.ViewPager> </LinearLayout>
在LinearLayout中使用TabLayout標(biāo)簽和ViewPager標(biāo)簽。
Activity代碼編寫(xiě)
public class MainActivity extends AppCompatActivity { public static final String[] tabTitles = new String[]{"短信1","短信2","短信3","短信4","短信5","短信6","短信7","短信8","短信9"}; private TabLayout mTabLayout; private ViewPager mViewPager; private List<SingleFragment> mFragments = new ArrayList<SingleFragment>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } private void initViews() { mTabLayout = (TabLayout) findViewById(R.id.tablayout); mViewPager = (ViewPager) findViewById(R.id.viewpager); for(int i = 0; i < tabTitles.length; i++) { mFragments.add(SingleFragment.createFragment(tabTitles[i])); } //為ViewPager設(shè)置FragmentPagerAdapter mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return mFragments.get(position); } @Override public int getCount() { return mFragments.size(); } /** * 為TabLayout中每一個(gè)tab設(shè)置標(biāo)題 */ @Override public CharSequence getPageTitle(int position) { return tabTitles[position]; } }); //TabLaout和ViewPager進(jìn)行關(guān)聯(lián) mTabLayout.setupWithViewPager(mViewPager); //防止tab太多,都擁擠在一起 mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); } }
大部分功能都在initViews()方法中實(shí)現(xiàn),大致講解一下:第23,24行獲得TabLayout和ViewPager控件實(shí)例;26~29行創(chuàng)建了需要的Fragment實(shí)例,并保存在mFragments列表中。第32行,為ViewPager設(shè)置FragmentPagerAdapter,并通過(guò)getSupportFragmentManager()方法將FragmentManager傳遞給FragmentPagerAdapter。第50行,getPageTitle()回調(diào)函數(shù),來(lái)為TabLayout中的Tab設(shè)置標(biāo)題。第57行,將TabLayout和ViewPager進(jìn)行關(guān)聯(lián)。最后,設(shè)置了TabLayout的模式,TabLayout.MODE_SCROLLABLE表示TabLayout可以滑動(dòng),這樣就可以防止過(guò)多的Tab擁擠在一屏內(nèi)。
Fragment代碼編寫(xiě)
public class SingleFragment extends Fragment { public static final String ARGUMENT = "ARGUMENT"; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Bundle bundle = getArguments(); String text = ""; if(bundle != null) { text = bundle.getString(ARGUMENT); } TextView tv = new TextView(getActivity()); tv.setText(text); tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22); tv.setGravity(Gravity.CENTER); return tv; } public static SingleFragment createFragment(String argument) { Bundle bundle = new Bundle(); bundle.putString(ARGUMENT, argument); SingleFragment fragment = new SingleFragment(); fragment.setArguments(bundle); return fragment; } }
Fragment的UI控件很簡(jiǎn)單,僅僅包含一個(gè)TextView。外部通過(guò)靜態(tài)方法createFragment()用來(lái)創(chuàng)建Fragment實(shí)例,并且可以傳遞參數(shù),傳遞的參數(shù)將設(shè)置到TextView中。
OK,至此TabLayout就可以正常使用了,效果就為文章開(kāi)始貼的gif圖。
另外,TabLayout還提供了很多自定義屬性,讓我們自定義Tab的樣式。
示例代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="tech.czh.example.MainActivity"> <android.support.design.widget.TabLayout android:id="@+id/tablayout" android:layout_width="match_parent" android:layout_height="50dp" android:layout_gravity="top" app:tabTextColor="@color/colorPrimary" app:tabSelectedTextColor="@color/colorAccent" app:tabIndicatorColor="@color/colorAccent" app:tabIndicatorHeight="5dp"/> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white"> </android.support.v4.view.ViewPager> </LinearLayout>
這里我們使用一些屬性,比如:tabTextColor用來(lái)設(shè)置Tab中文字顏色;
tabSelectedTextColor用來(lái)設(shè)置Tab被選中時(shí)文字顏色;tabIndicatorColor用來(lái)設(shè)置指示器顏色;tabIndicatorHeight用來(lái)設(shè)置指示器的高度。
最后,看一下效果:
好的,TabLayout的使用就說(shuō)這么多。可以看出TabLayout使用起來(lái)還是很方便的,并且最終效果也很nice。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- TabLayout+ViewPager實(shí)現(xiàn)切頁(yè)的示例代碼
- Android 中基于TabLayout+ViewPager實(shí)現(xiàn)標(biāo)簽卡效果
- TabLayout關(guān)聯(lián)ViewPager后不顯示文字的解決方法
- Android中TabLayout+ViewPager實(shí)現(xiàn)tab和頁(yè)面聯(lián)動(dòng)效果
- 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連接MySQL數(shù)據(jù)庫(kù)并進(jìn)行增刪改查操作示例講解
這篇文章主要介紹了Android 連接MySQL數(shù)據(jù)庫(kù)并進(jìn)行增刪改查操作示例講解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Android學(xué)習(xí)教程之動(dòng)態(tài)GridView控件使用(6)
這篇文章主要為大家詳細(xì)介紹了Android動(dòng)態(tài)GridView控件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11淺析Android中g(shù)etWidth()和getMeasuredWidth()的區(qū)別
這篇文章主要介紹了淺析Android中g(shù)etWidth()和getMeasuredWidth()的區(qū)別 ,getMeasuredWidth()獲取的是view原始的大小,getWidth()獲取的是這個(gè)view最終顯示的大小,具體區(qū)別介紹大家參考下本文2018-04-04Android實(shí)現(xiàn)簡(jiǎn)單的分頁(yè)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單的分頁(yè)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Libgdx解決部分Android機(jī)型鎖屏崩潰的方法
今天小編就為大家分享一篇關(guān)于Libgdx解決部分Android機(jī)型鎖屏崩潰的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10