最近项目要使用类似TabLayout的控件,其实我感觉就是TabLayout只是换了一个Indicator,先说一说TabLayout这是Android Support Design的控件要使用的同学们应导入Design库在Gradle中在dependencies加入下面代码同步Gradle就可以使用了,Design里面还有很多有意思的东西推荐大家都看看。

  compile 'com.android.support:design:23.1.1'

  

  想改变TabLayout Indicator还是有点麻烦的,主要是TabLayout没有暴露出一些东西,这就导致我们在自定义Indicator的时候不是那么的方便呢。我的想法是在TabLayout后面加一个View来跟随TabLayout自己来画Indicator,实现方法有很多我只给大家提供一个思路,下面是布局方式。

  <FrameLayout
android:layout_width="match_parent"
android:layout_height="60dp"> <com.indicator.ShapeIndicatorView    //自定义Indicator
android:id="@+id/custom_indicator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
app:fullColor="@android:color/holo_blue_dark"
/> <android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.design.widget.TabLayout> </FrameLayout>

用一个FrameLayout包含一个TabLayout与一个自定义的Indicator, ShapeIndicatorView的代码包含三个部分,设置TabLayout,设置ViewPager,然后将TabLayout与ViewPager组合起来。

 下面代码说明它们是如何组合的,其实TabLayout有一个setupWithViewPater方法可以直接设置ViewPager但这样会产生一个问题,TabLayout内部会为ViewPager添加一个自身的OnViewPagerScrollListener,而我们自己定义的也会添加一个listener这就会导致有一些冲突我的解决办法是不为TabLayout设置ViewPager将ViewPager设置在自定义的View由我们管理TabLayout与ViewPager的切换工作。

  mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter); TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
ShapeIndicatorView shapeIndicatorView = (ShapeIndicatorView) findViewById(R.id.custom_indicator); tabLayout.setTabsFromPagerAdapter(mViewPager.getAdapter());
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); shapeIndicatorView.setupWithTabLayout(tabLayout);
shapeIndicatorView.setupWithViewPager(mViewPager);

准备好上面的工作后再看ShapeIndicatorView内部的代码。首先将TabLayout原生的Indicator的颜色设置为不可见,然后设置个listener监听Tab切换事件, 最后要添加一个全局的滚动listener如果TabLayout的Mode是SCROLLABLE的话这是有必要的,因为我们的Indicator也要跟直滚动。

 public void setupWithTabLayout(final TabLayout tableLayout){
mTabLayout = tableLayout; tableLayout.setSelectedTabIndicatorColor(Color.TRANSPARENT);
tableLayout.setOnTabSelectedListener(this); tableLayout.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
if (mTabLayout.getScrollX() != getScrollX())
scrollTo(mTabLayout.getScrollX(), mTabLayout.getScrollY());
}
}); ViewCompat.setElevation(this, ViewCompat.getElevation(mTabLayout));
tableLayout.post(new Runnable() {
@Override
public void run() {
if (mTabLayout.getTabCount() > 0)
onTabSelected(mTabLayout.getTabAt(0)); }
}); //清除Tab background
for(int tab = 0; tab < tableLayout.getTabCount() ; tab++){
View tabView = getTabViewByPosition(tab);
tabView.setBackgroundResource(0);
}
}

  

效果图

  

还有的一些就是协作方法就不贴出来呢,有兴趣的可以看源码。

https://github.com/yjwfn/tablayoutindicator.git

最新文章

  1. 基于正则的INI读写工具类,支持加密解密
  2. js中的forEach
  3. 关于清除丢失贴图与IES文件
  4. paip.输出内容替换在Apache 过滤器filter的设置
  5. HDU 4893 Wow! Such Sequence!(2014 Multi-University Training Contest 3)
  6. C++之STL之string
  7. win7中protel99添加元件库
  8. [Unity菜鸟] Time
  9. ios NavBar+TarBar技巧
  10. The partner transaction manager has disabled its support for remote/network transactions.
  11. Spring注解:@Resource、@PreConstruct、@PreDestroy、@Component
  12. 最受欢迎的Web开发工具
  13. go-common-pool设计原理分析
  14. Vue directive 回调运用
  15. C#程序编写规范
  16. java,sort的数组降序
  17. ZOL 3977. Pointers
  18. [转]10 Awesome Indicator Applets for Ubuntu’s Unity Desktop
  19. 在 .NET 项目中集成 SwaggerUI(2018.9.30)
  20. Cassandra基础2

热门文章

  1. webrtc笔记(1): 基于coturn项目的stun/turn服务器搭建
  2. Leetcode之二分法专题-287. 寻找重复数(Find the Duplicate Number)
  3. Mac os 下 python爬虫相关的库和软件的安装
  4. macbook 安装redis流程及问题总结
  5. JDK中的Proxy技术实现AOP功能
  6. CF_EDU51 E. Vasya and Big Integers
  7. 杭电多校第九场 D Rikka with Stone-Paper-Scissors 数学
  8. codeforces 768 C. Jon Snow and his Favourite Number(思维+暴力)
  9. Java 编程语言中很少被人了解的特性-statement label
  10. Microsoft Office 365的安装