欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

TabLayout實現(xiàn)ViewPager指示器的方法

 更新時間:2018年06月13日 11:41:13   作者:lorienzhang  
這篇文章主要為大家詳細介紹了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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android Bitmap壓縮方法的選擇詳解

    Android Bitmap壓縮方法的選擇詳解

    這篇文章主要介紹了Android Bitmap壓縮方法的選擇的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • Android 使用騰訊X5瀏覽器上傳圖片的示例

    Android 使用騰訊X5瀏覽器上傳圖片的示例

    這篇文章主要介紹了Android 使用騰訊X5瀏覽器上傳圖片的示例,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下
    2021-04-04
  • Android連接MySQL數(shù)據(jù)庫并進行增刪改查操作示例講解

    Android連接MySQL數(shù)據(jù)庫并進行增刪改查操作示例講解

    這篇文章主要介紹了Android 連接MySQL數(shù)據(jù)庫并進行增刪改查操作示例講解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Android學(xué)習(xí)教程之動態(tài)GridView控件使用(6)

    Android學(xué)習(xí)教程之動態(tài)GridView控件使用(6)

    這篇文章主要為大家詳細介紹了Android動態(tài)GridView控件的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android自定義控制條效果

    Android自定義控制條效果

    這篇文章主要為大家詳細介紹了Android自定義控制條效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • 淺析Android中g(shù)etWidth()和getMeasuredWidth()的區(qū)別

    淺析Android中g(shù)etWidth()和getMeasuredWidth()的區(qū)別

    這篇文章主要介紹了淺析Android中g(shù)etWidth()和getMeasuredWidth()的區(qū)別 ,getMeasuredWidth()獲取的是view原始的大小,getWidth()獲取的是這個view最終顯示的大小,具體區(qū)別介紹大家參考下本文
    2018-04-04
  • android app跳轉(zhuǎn)到微信的示例

    android app跳轉(zhuǎn)到微信的示例

    這篇文章主要介紹了android app跳轉(zhuǎn)到微信的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • Android實現(xiàn)簡單的分頁效果

    Android實現(xiàn)簡單的分頁效果

    這篇文章主要為大家詳細介紹了Android實現(xiàn)簡單的分頁效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Libgdx解決部分Android機型鎖屏崩潰的方法

    Libgdx解決部分Android機型鎖屏崩潰的方法

    今天小編就為大家分享一篇關(guān)于Libgdx解決部分Android機型鎖屏崩潰的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • Android自定義圖片選擇器簡單版

    Android自定義圖片選擇器簡單版

    這篇文章主要為大家詳細介紹了Android自定義圖片選擇器簡單版,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02

最新評論