PagerSlidingTabStrip源码分析

来源:互联网 发布:淘宝店更换类目影响 编辑:程序博客网 时间:2024/06/04 00:57
/** * 这个方法可以为某一个值 * 赋值 */DisplayMetrics dm = getResources().getDisplayMetrics();
scrollOffset = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, scrollOffset, dm);indicatorHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, indicatorHeight, dm);underlineHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, underlineHeight, dm);dividerPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dividerPadding, dm);tabPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, tabPadding, dm);dividerWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dividerWidth, dm);tabTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, tabTextSize, dm);

/** * 取出xml中配置的属性 */a = context.obtainStyledAttributes(attrs, R.styleable.PagerSlidingTabStrip);

indicatorColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsIndicatorColor, indicatorColor);underlineColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsUnderlineColor, underlineColor);dividerColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsDividerColor, dividerColor);indicatorHeight = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsIndicatorHeight, indicatorHeight);underlineHeight = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsUnderlineHeight, underlineHeight);dividerPadding = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsDividerPadding, dividerPadding);tabPadding = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsTabPaddingLeftRight, tabPadding);tabBackgroundResId = a.getResourceId(R.styleable.PagerSlidingTabStrip_pstsTabBackground, tabBackgroundResId);shouldExpand = a.getBoolean(R.styleable.PagerSlidingTabStrip_pstsShouldExpand, shouldExpand);scrollOffset = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsScrollOffset, scrollOffset);textAllCaps = a.getBoolean(R.styleable.PagerSlidingTabStrip_pstsTextAllCaps, textAllCaps);

//初始化画笔,用于,一个矩形rectPaint = new Paint();rectPaint.setAntiAlias(true);rectPaint.setStyle(Style.FILL);

/** * 绑定一个listener  版定之后,外部不能重复版定,重复版定,这个就会失效了 */pager.setOnPageChangeListener(pageListener);

/** * 得到视图树的监听者,,得到一个空间布局完成 */getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {   @SuppressWarnings("deprecation")   @SuppressLint("NewApi")   @Override   public void onGlobalLayout() {      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {         getViewTreeObserver().removeGlobalOnLayoutListener(this);      } else {         getViewTreeObserver().removeOnGlobalLayoutListener(this);      }      currentPosition = pager.getCurrentItem();      scrollToChild(currentPosition, 0);   }});
/** * 修改样式_只是默认时候的字体颜色以及字体大小 * 修改样式,,修改的只是默认时候的字体颜色和默认大小 */private void updateTabStyles() {   for (int i = 0; i < tabCount; i++) {      View v = tabsContainer.getChildAt(i);      v.setBackgroundResource(tabBackgroundResId);      if (v instanceof TextView) {         TextView tab = (TextView) v;         tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, tabTextSize);         tab.setTypeface(tabTypeface, tabTypefaceStyle);         tab.setTextColor(tabTextColor);         // setAllCaps() is only available from API 14, so the upper case is made manually if we are on a         // pre-ICS-build         if (textAllCaps) {            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {               tab.setAllCaps(true);            } else {               tab.setText(tab.getText().toString().toUpperCase(locale));            }         }      }   }}

/** * 绘制 矩形 */canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);
下面是参考别人的分析

用法

对于这个项目的执行工作看到了sample/文件夹。

  1. 包括在以下依赖build.gradle文件。
compile 'com.jpardogo.materialtabstrip:library:1.1.1'

或库中添加作为项目。我试图发送一个拉入请求,但看起来像原开发商不能维持下去了。

  1. 包括PagerSlidingTabStrip在布局小部件。这通常应放在上面的ViewPager它代表。
<com.astuetz.PagerSlidingTabStrip    android:id="@+id/tabs"    android:layout_width="match_parent"    android:layout_height="?attr/actionBarSize"    android:background="?attr/colorPrimary" />
  1. onCreate方法(或onCreateView为一个片段),结合小窗口的ViewPager
// Initialize the ViewPager and set an adapterViewPager pager = (ViewPager) findViewById(R.id.pager);pager.setAdapter(new TestAdapter(getSupportFragmentManager()));// Bind the tabs to the ViewPagerPagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);tabs.setViewPager(pager);

这就是所有你需要做的,但如果你想使用自己的选项卡,然后...

  1. 如果您的适配器实现接口CustomTabProvider可以粘贴您的自定义选项卡视图/秒。

    • 在情况下,视图返回包含ID R.id.psts_tab_title,该视图应该是一个TextView 和将被用于放置在标题和设置视图状态(按压/选择/默认值)。

    • 如果你不希望库管理您的TextView标题标签做的,使用不同的ID不是R.id.psts_tab_title你的标签布局。

    • 该接口提供了标签的选择和去除选择回调也是如此。

    • 如果您的适配器没有实现接口CustomTabProvider的默认选项卡将被使用,这是一个TextView带有IDR.id.psts_tab_title)。

  2. (可选)如果您使用OnPageChangeListener你的看法寻呼机,你应该将其设置在小部件,而不是直接在寻呼机。

// continued from abovetabs.setOnPageChangeListener(mPageChangeListener);

定制

从主题:

  • android:textColorPrimary 值(从你的主题)将如果值不上的XML布局定义被自动应用到该标签的文本颜色,underlineColor,dividerColor和indicatorColor(与150阿尔法255α和非选定标签选择的选项卡),。

注意事项的一些原生属性:

  • android:paddingLeftandroid:paddingRight布局填充。如果同时应用,他们应该是平衡的。检查问题#69获取更多信息。

自定义属性:

  • pstsIndicatorColor滑动指示剂变色。textColorPrimary将它的默认颜色值。
  • pstsIndicatorHeight滑动指示器的高度。
  • pstsUnderlineColor上的视图的底部的全宽线的颜色。textColorPrimary将它的默认颜色值。
  • pstsUnderlineHeight 上的视图的底部的全宽线的高度。
  • pstsDividerColor标签之间的分界线的颜色。textColorPrimary将它的默认颜色值。
  • pstsDividerWidth 分隔线的笔划宽度,缺省值为0。
  • pstsDividerPadding 分频器的顶部和底部填充。
  • pstsShouldExpand 如果设置为true,每个选项卡被赋予相同的权重,默认为false。
  • pstsScrollOffset 滚动偏移所选标签的。
  • pstsPaddingMiddle 如果为真,的选项卡视图(像报摊谷歌应用程序)的中间开始。
  • pstsTabPaddingLeftRight 每个选项卡的左右填充。
  • pstsTabBackground 每个选项卡的背景绘制的,应该是一个StateListDrawable。
  • pstsTabTextSize 标签文字大小(SP)。
  • pstsTabTextColor标签文本颜色,它们可以是一种颜色(文本颜色不会改变)或每个状态的颜色的选择:按压(标签按下),选择(选项卡激活),默认(活动非活动)。国家在选择的顺序很重要。检查问题#68获取更多信息。
  • pstsTabTextStyle 设置文本样式,默认为正常的API 21,大胆的在旧的API。
  • pstsTabTextAllCaps 如果为真,所有的选项卡标题将是大写,默认值是true。
  • pstsTabTextAlpha设置非选定的标签文本的Alpha透明度。范围0..255。150是它的默认值。将不如果使用pstsTabTextColor在布局定义。如果pstsTabTextColorNOT定义,将施加到非选择的选项卡。
  • pstsTabTextFontFamily设置字体系列名称。默认sans-serif-medium的API 21,sans-serif在旧的API。

几乎所有的属性都有其各自的getter和setter方法在运行时改变它们。动态地改变pstsTabTextFontFamilypstsTabTextStyle您可以拨打:

  • public void setTypeface(Typeface typeface, int style)它可用于在默认选项卡来定义自定义字体。否则,你可以使用自定义选项卡用CustomTabProvider
原创粉丝点击