TabLayout實現(xiàn)ViewPager指示器的方法
在TabLayout出現(xiàn)之前,基本都是通過 ViewPager+FragmentPagerAdapter+第三方開源tab指示器(TabPageIndicator)來實現(xiàn)的?,F(xiàn)在Android內(nèi)部提供了現(xiàn)成的TabLayout控件來實現(xiàn)ViewPager指示器的效果。
先看效果圖:
導(dǎo)入依賴
在Gradle文件中導(dǎo)入依賴,代碼如下:
compile 'com.android.support:design:23.4.0'
TabLayout類就在這個依賴包中定義的。
布局文件中使用
<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標簽和ViewPager標簽。
Activity代碼編寫
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中每一個tab設(shè)置標題 */ @Override public CharSequence getPageTitle(int position) { return tabTitles[position]; } }); //TabLaout和ViewPager進行關(guān)聯(lián) mTabLayout.setupWithViewPager(mViewPager); //防止tab太多,都擁擠在一起 mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); } }
大部分功能都在initViews()方法中實現(xiàn),大致講解一下:第23,24行獲得TabLayout和ViewPager控件實例;26~29行創(chuàng)建了需要的Fragment實例,并保存在mFragments列表中。第32行,為ViewPager設(shè)置FragmentPagerAdapter,并通過getSupportFragmentManager()方法將FragmentManager傳遞給FragmentPagerAdapter。第50行,getPageTitle()回調(diào)函數(shù),來為TabLayout中的Tab設(shè)置標題。第57行,將TabLayout和ViewPager進行關(guān)聯(lián)。最后,設(shè)置了TabLayout的模式,TabLayout.MODE_SCROLLABLE表示TabLayout可以滑動,這樣就可以防止過多的Tab擁擠在一屏內(nèi)。
Fragment代碼編寫
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控件很簡單,僅僅包含一個TextView。外部通過靜態(tài)方法createFragment()用來創(chuàng)建Fragment實例,并且可以傳遞參數(shù),傳遞的參數(shù)將設(shè)置到TextView中。
OK,至此TabLayout就可以正常使用了,效果就為文章開始貼的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用來設(shè)置Tab中文字顏色;
tabSelectedTextColor用來設(shè)置Tab被選中時文字顏色;tabIndicatorColor用來設(shè)置指示器顏色;tabIndicatorHeight用來設(shè)置指示器的高度。
最后,看一下效果:
好的,TabLayout的使用就說這么多??梢钥闯鯰abLayout使用起來還是很方便的,并且最終效果也很nice。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- TabLayout+ViewPager實現(xiàn)切頁的示例代碼
- Android 中基于TabLayout+ViewPager實現(xiàn)標簽卡效果
- TabLayout關(guān)聯(lián)ViewPager后不顯示文字的解決方法
- Android中TabLayout+ViewPager實現(xiàn)tab和頁面聯(lián)動效果
- Android中TabLayout+ViewPager 簡單實現(xiàn)app底部Tab導(dǎo)航欄
- Android中TabLayout結(jié)合ViewPager實現(xiàn)頁面切換
- Android中TabLayout結(jié)合ViewPager實現(xiàn)頁面切換效果
- AndroidUI組件SlidingTabLayout實現(xiàn)ViewPager頁滑動效果
- TabLayout+ViewPager2的簡單使用詳解
相關(guān)文章
Android連接MySQL數(shù)據(jù)庫并進行增刪改查操作示例講解
這篇文章主要介紹了Android 連接MySQL數(shù)據(jù)庫并進行增刪改查操作示例講解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Android學(xué)習(xí)教程之動態(tài)GridView控件使用(6)
這篇文章主要為大家詳細介紹了Android動態(tài)GridView控件的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11淺析Android中g(shù)etWidth()和getMeasuredWidth()的區(qū)別
這篇文章主要介紹了淺析Android中g(shù)etWidth()和getMeasuredWidth()的區(qū)別 ,getMeasuredWidth()獲取的是view原始的大小,getWidth()獲取的是這個view最終顯示的大小,具體區(qū)別介紹大家參考下本文2018-04-04