最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2018年06月13日 11:41:13   作者:lorienzhang  
這篇文章主要為大家詳細(xì)介紹了TabLayout實(shí)現(xiàn)ViewPager指示器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在TabLayout出現(xiàn)之前,基本都是通過 ViewPager+FragmentPagerAdapter+第三方開源tab指示器(TabPageIndicator)來實(shí)現(xiàn)的?,F(xiàn)在Android內(nèi)部提供了現(xiàn)成的TabLayout控件來實(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代碼編寫

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,并通過getSupportFragmentManager()方法將FragmentManager傳遞給FragmentPagerAdapter。第50行,getPageTitle()回調(diào)函數(shù),來為TabLayout中的Tab設(shè)置標(biāo)題。第57行,將TabLayout和ViewPager進(jìn)行關(guān)聯(lián)。最后,設(shè)置了TabLayout的模式,TabLayout.MODE_SCROLLABLE表示TabLayout可以滑動(dòng),這樣就可以防止過多的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控件很簡(jiǎn)單,僅僅包含一個(gè)TextView。外部通過靜態(tài)方法createFragment()用來創(chuàng)建Fragment實(shí)例,并且可以傳遞參數(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被選中時(shí)文字顏色;tabIndicatorColor用來設(shè)置指示器顏色;tabIndicatorHeight用來設(shè)置指示器的高度。

最后,看一下效果:

好的,TabLayout的使用就說這么多。可以看出TabLayout使用起來還是很方便的,并且最終效果也很nice。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android自定義ScrollView實(shí)現(xiàn)阻尼回彈

    Android自定義ScrollView實(shí)現(xiàn)阻尼回彈

    這篇文章主要為大家詳細(xì)介紹了Android自定義ScrollView實(shí)現(xiàn)阻尼回彈,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android獲取應(yīng)用程序名稱(ApplicationName)示例

    Android獲取應(yīng)用程序名稱(ApplicationName)示例

    本文以實(shí)例方式為大家介紹下獲取應(yīng)用程序名稱(ApplicationName)的具體實(shí)現(xiàn),感興趣的各位可以參考下哈
    2013-06-06
  • Android中不同狀態(tài)頁面管理優(yōu)化技巧詳解

    Android中不同狀態(tài)頁面管理優(yōu)化技巧詳解

    在Android中,不管是activity或者fragment,在加載視圖的時(shí)候都有可能會(huì)出現(xiàn)多種不同的狀態(tài)頁面View,所以本文就來聊聊Android中不同狀態(tài)頁面管理優(yōu)化吧
    2024-04-04
  • Android 控制wifi 相關(guān)操作實(shí)例

    Android 控制wifi 相關(guān)操作實(shí)例

    本篇文章主要介紹了Android 控制wifi 的開發(fā)實(shí)例,并附有實(shí)例源碼等相關(guān)資料,需要的朋友可以參考下
    2016-07-07
  • Android編程獲取通知欄高度的方法

    Android編程獲取通知欄高度的方法

    這篇文章主要介紹了Android編程獲取通知欄高度的方法,涉及Android針對(duì)通知欄屬性相關(guān)操作技巧,需要的朋友可以參考下
    2016-01-01
  • Android調(diào)用堆棧跟蹤實(shí)例分析

    Android調(diào)用堆棧跟蹤實(shí)例分析

    這篇文章主要介紹了Android調(diào)用堆棧跟蹤的方法,以實(shí)例形式較為詳細(xì)的分析了Android錯(cuò)誤信息分析的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • Android rom解包打包工具

    Android rom解包打包工具

    這篇文章主要介紹了Android rom解包打包工具的相關(guān)資料,對(duì)rom解包打包相關(guān)知識(shí)感興趣的朋友可以參考下
    2016-01-01
  • Android編程實(shí)現(xiàn)使用webView打開本地html文件的方法

    Android編程實(shí)現(xiàn)使用webView打開本地html文件的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)使用webView打開本地html文件的方法,結(jié)合實(shí)例形式分析了Android中webview布局及打開HTML文件的功能實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-02-02
  • kotlin 注解 @Parcelize 使用示例和步驟詳解

    kotlin 注解 @Parcelize 使用示例和步驟詳解

    Parcelable 接口是 Android 中用于在組件之間傳遞對(duì)象的一種方式,在 Kotlin 中,@Parcelize 注解用于簡(jiǎn)化實(shí)現(xiàn) Android Parcelable 接口的過程,本文給大家分享kotlin 注解 @Parcelize 使用示例,感興趣的朋友一起看看吧
    2024-06-06
  • Android多套環(huán)境的維護(hù)思路詳解

    Android多套環(huán)境的維護(hù)思路詳解

    這篇文章主要為大家介紹了Android多套環(huán)境的維護(hù)思路詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11

最新評(píng)論

木兰县| 于田县| 巴青县| 磐安县| 惠东县| 永顺县| 桂东县| 包头市| 定州市| 华阴市| 舞钢市| 东阳市| 岐山县| 岳阳县| 石柱| 伊金霍洛旗| 武义县| 溧阳市| 庆安县| 东乡县| 乐昌市| 阳东县| 田林县| 剑阁县| 黑水县| 锦州市| 九龙县| 台南县| 乐亭县| 桦川县| 景泰县| 古丈县| 正安县| 寻甸| 锡林郭勒盟| 巨野县| 视频| 象山县| 台东县| 汕尾市| 于都县|