PageSlidingTab的改进,项目中可以直接使用。,聪明人已经收藏了

if (locale == null) {

locale = getResources().getConfiguration().locale;

}

}

public void setShadowWidth(int width){

mShadowHeight = width;

}

public void setShadowWidthRes(int resId) {

setShadowWidth((int) getResources().getDimension(resId));

}

public void setViewPager(ViewPager pager) {

this.pager = pager;

if (pager.getAdapter() == null) {

throw new IllegalStateException(“ViewPager does not have adapter instance.”);

}

pager.setOnPageChangeListener(pageListener);

notifyDataSetChanged();

}

public void setOnPageChangeListener(OnPageChangeListener listener) {

this.delegatePageListener = listener;

}

public void notifyDataSetChanged() {

tabsContainer.removeAllViews();

tabCount = pager.getAdapter().getCount();

for (int i = 0; i < tabCount; i++) {

if (pager.getAdapter() instanceof IconTabProvider) {

addIconTab(i, ((IconTabProvider) pager.getAdapter()).getPageIconResId(i));

} else {

addTextTab(i, pager.getAdapter().getPageTitle(i).toString());

}

}

updateTabStyles();

//ViewTreeObserver Draw/Layout观察者

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 addTextTab(final int position, String title) {

TextView tab = new TextView(getContext());

tab.setText(title);

tab.setGravity(Gravity.CENTER);

tab.setSingleLine();

addTab(position, tab);

}

private void addIconTab(final int position, int resId) {

ImageButton tab = new ImageButton(getContext());

tab.setImageResource(resId);

addTab(position, tab);

}

private void addTab(final int position, View tab) {

tab.setFocusable(true);

tab.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

pager.setCurrentItem(position);

}

});

tab.setPadding(tabPadding, 0, tabPadding, tabPaddingBottom);

tabsContainer.addView(tab, position, shouldExpand ? expandedTabLayoutParams : defaultTabLayoutParams);

}

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;

//TextView的setTextSize(单位, 数值);

tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, tabTextSize);

tab.setTypeface(tabTypeface, tabTypefaceStyle);

// 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));

}

}

//addd by linghu

if(i == selectedPosition){

tab.setTextColor(tabTextSelectedColor);

}else{

tab.setTextColor(tabTextColor);

}

}

}

}

/**

* _滑动到__position__位置的__Tab__项,_offset__偏移处。

* @param position

* @param offset

*/

private void scrollToChild(int position, int offset) {

if (tabCount == 0) {

return;

}

int newScrollX = tabsContainer.getChildAt(position).getLeft() + offset;

if (position > 0 || offset > 0) {

newScrollX -= scrollOffset;

}

if (newScrollX != lastScrollX) {

lastScrollX = newScrollX;

scrollTo(newScrollX, 0);

}

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

//isInEditMode() eclipse或studio编辑器在布局显示过程中

if (isInEditMode() || tabCount == 0) {

return;

}

final int height = getHeight();

// draw indicator line

rectPaint.setColor(indicatorColor);

// default: line below current tab

View currentTab = tabsContainer.getChildAt(currentPosition);

float lineLeft = currentTab.getLeft();

float lineRight = currentTab.getRight();

// if there is an offset, start interpolating left and right coordinates between current and next tab

if (currentPositionOffset > 0f && currentPosition < tabCount - 1) {

View nextTab = tabsContainer.getChildAt(currentPosition + 1);

float nextTabLeft = nextTab.getLeft();

float nextTabRight = nextTab.getRight();

lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft);

lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight);

}

//绘制底部的指示线,是一个长条的矩形

canvas.drawRect(lineLeft, height-mShadowHeight - indicatorHeight, lineRight, height-mShadowHeight, rectPaint);

// draw underline, 指示器下面的横线

// rectPaint.setColor(underlineColor);

// canvas.drawRect(0, height - mShadowHeight - underlineHeight, tabsContainer.getWidth(), height - mShadowHeight, rectPaint);

// draw divider, Tab之间的分割线

// dividerPaint.setColor(dividerColor);

// for (int i = 0; i < tabCount - 1; i++) {

// View tab = tabsContainer.getChildAt(i);

// canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding, dividerPaint);

// }

}

private class PageListener implements OnPageChangeListener {

@Override

public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

currentPosition = position;

currentPositionOffset = positionOffset;

//滚动过程中该控件滚动到目标Tab的相应位置

scrollToChild(position, (int) (positionOffset * tabsContainer.getChildAt(position).getWidth()));

invalidate();

if (delegatePageListener != null) {

delegatePageListener.onPageScrolled(position, positionOffset, positionOffsetPixels);

}

}

@Override

public void onPageScrollStateChanged(int state) {

if (state == ViewPager.SCROLL_STATE_IDLE) {

scrollToChild(pager.getCurrentItem(), 0);

}

if (delegatePageListener != null) {

delegatePageListener.onPageScrollStateChanged(state);

}

}

@Override

public void onPageSelected(int position) {

selectedPosition = position;

updateTabStyles();

if (delegatePageListener != null) {

delegatePageListener.onPageSelected(position);

}

}

}

//added by linghu

public void setSelectedTextColor(int textColor) {

this.tabTextSelectedColor = textColor;

updateTabStyles();

}

//added by linghu

public void setSelectedTextColorResource(int resId) {

this.tabTextSelectedColor = getResources().getColor(resId);

updateTabStyles();

}

//added by linghu

public int getSelectedTextColor() {

return tabTextSelectedColor;

}

public void setIndicatorColor(int indicatorColor) {

this.indicatorColor = indicatorColor;

invalidate();

}

public void setIndicatorColorResource(int resId) {

this.indicatorColor = getResources().getColor(resId);

invalidate();

}

public int getIndicatorColor() {

return this.indicatorColor;

}

public void setIndicatorHeight(int indicatorLineHeightPx) {

this.indicatorHeight = indicatorLineHeightPx;

invalidate();

}

public int getIndicatorHeight() {

return indicatorHeight;

}

public void setUnderlineColor(int underlineColor) {

this.underlineColor = underlineColor;

invalidate();

}

public void setUnderlineColorResource(int resId) {

this.underlineColor = getResources().getColor(resId);

invalidate();

}

public int getUnderlineColor() {

return underlineColor;

}

public void setDividerColor(int dividerColor) {

this.dividerColor = dividerColor;

invalidate();

}

public void setDividerColorResource(int resId) {

this.dividerColor = getResources().getColor(resId);

invalidate();

}

public int getDividerColor() {

return dividerColor;

}

public void setUnderlineHeight(int underlineHeightPx) {

this.underlineHeight = underlineHeightPx;

invalidate();

}

public int getUnderlineHeight() {

写在最后

在技术领域内,没有任何一门课程可以让你学完后一劳永逸,再好的课程也只能是“师傅领进门,修行靠个人”。“学无止境”这句话,在任何技术领域,都不只是良好的习惯,更是程序员和工程师们不被时代淘汰、获得更好机会和发展的必要前提。

如果你觉得自己学习效率低,缺乏正确的指导,可以加入资源丰富,学习氛围浓厚的技术圈一起学习交流吧

加入我们吧!群内有许多来自一线的技术大牛,也有在小厂或外包公司奋斗的码农,我们致力打造一个平等,高质量的Android交流圈子,不一定能短期就让每个人的技术突飞猛进,但从长远来说,眼光,格局,长远发展的方向才是最重要的。

35岁中年危机大多是因为被短期的利益牵着走,过早压榨掉了价值,如果能一开始就树立一个正确的长远的职业规划。35岁后的你只会比周围的人更值钱。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值