TabLayout实现ViewPager指示器的方法

所属分类: 软件编程 / Android 阅读数: 69
收藏 0 赞 0 分享

在TabLayout出现之前,基本都是通过 ViewPager+FragmentPagerAdapter+第三方开源tab指示器(TabPageIndicator)来实现的。现在Android内部提供了现成的TabLayout控件来实现ViewPager指示器的效果。

先看效果图:

导入依赖

在Gradle文件中导入依赖,代码如下:

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设置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设置标题
    */
   @Override
   public CharSequence getPageTitle(int position)
   {
    return tabTitles[position];
   }
  });

  //TabLaout和ViewPager进行关联
  mTabLayout.setupWithViewPager(mViewPager);
  //防止tab太多,都拥挤在一起
  mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
 }
}

大部分功能都在initViews()方法中实现,大致讲解一下:第23,24行获得TabLayout和ViewPager控件实例;26~29行创建了需要的Fragment实例,并保存在mFragments列表中。第32行,为ViewPager设置FragmentPagerAdapter,并通过getSupportFragmentManager()方法将FragmentManager传递给FragmentPagerAdapter。第50行,getPageTitle()回调函数,来为TabLayout中的Tab设置标题。第57行,将TabLayout和ViewPager进行关联。最后,设置了TabLayout的模式,TabLayout.MODE_SCROLLABLE表示TabLayout可以滑动,这样就可以防止过多的Tab拥挤在一屏内。

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。外部通过静态方法createFragment()用来创建Fragment实例,并且可以传递参数,传递的参数将设置到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用来设置Tab中文字颜色;
tabSelectedTextColor用来设置Tab被选中时文字颜色;tabIndicatorColor用来设置指示器颜色;tabIndicatorHeight用来设置指示器的高度。

最后,看一下效果:

好的,TabLayout的使用就说这么多。可以看出TabLayout使用起来还是很方便的,并且最终效果也很nice。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多