前言:
這個(gè)是與上篇 ViewPager指示器(三角形) 一個(gè)版本做的效果主要的滑動(dòng)原理大致相同囚似,話(huà)不多說(shuō)先看效果弱睦。
藍(lán)色滑塊.gif
- 這個(gè)用TabLayout是無(wú)法實(shí)現(xiàn)的只能自己寫(xiě)自定義的,當(dāng)時(shí)在網(wǎng)上找了很多都不是自己想要的,后再在github上找到一個(gè)跟我要的很相近的就拿過(guò)來(lái)改了改濒蒋。
實(shí)現(xiàn)思路
- 我是用的HorizontalScrollView惕味,在HorizontalScrollView添加一個(gè)LinearLayout楼誓,文字和動(dòng)態(tài)圖我寫(xiě)了一個(gè)itemView,再把itemView Add到LinearLayout里。
1.先把ItemView寫(xiě)好名挥。
2.ItemView添加到LinearLayout容器里慌随。
3.獲取當(dāng)Item的寬高,繪制藍(lán)色背景躺同。
4.計(jì)算滑動(dòng)距離并在ViewPager的滑動(dòng)事件調(diào)用阁猜。
實(shí)現(xiàn)步驟 (主要為核心代碼,全部代碼在最后面)
1. ItemView里多了個(gè)顏色漸變的方法蹋艺,沒(méi)有什么需要說(shuō)的直接貼代碼了剃袍。
public class BusinessTabItemView extends LinearLayout {
private Context mContext;
private GifImageView iv_icon;
private TextView tv_name;
private static final int COLOR_DEFAULT = Color.parseColor("#555555");
private static final int COLOR_SELECT = Color.parseColor("#ffffff");
public BusinessTabItemView(Context context) {
super(context);
mContext=context;
init(context);
}
/**
* 初始化
* @param context
*/
private void init(@NonNull final Context context) {
inflate(context, R.layout.item_business_tab, this);
iv_icon = findViewById(R.id.iv_icon);
tv_name = findViewById(R.id.tv_name);
}
/**
* 設(shè)置數(shù)據(jù)
* @param tag
* @param isChoice
*/
public void setData(BusinessTag tag, boolean isChoice){
tv_name.setText(tag.name);
if (isChoice){
tv_name.setTextColor(ContextCompat.getColor(mContext,R.color.white));
iv_icon.setVisibility(VISIBLE);
iv_icon.setImageResource(getGifIconID(tag.code));
GifDrawable gifDrawable = (GifDrawable) iv_icon.getDrawable();
gifDrawable.setLoopCount(1);
}else {
iv_icon.setVisibility(GONE);
tv_name.setTextColor(ContextCompat.getColor(mContext,R.color.common_text_55));
}
}
/**
* 設(shè)置選中
* @param tag
* @param isChoice
*/
public void setChoice(BusinessTag tag, boolean isChoice){
if (isChoice){
tv_name.setTextColor(ContextCompat.getColor(mContext,R.color.white));
iv_icon.setVisibility(VISIBLE);
iv_icon.setImageResource(getGifIconID(tag.code));
GifDrawable gifDrawable = (GifDrawable) iv_icon.getDrawable();
gifDrawable.setLoopCount(1);
}else {
iv_icon.setVisibility(GONE);
tv_name.setTextColor(ContextCompat.getColor(mContext,R.color.common_text_55));
}
}
/**
* 通過(guò)code獲取GifIcon的id
*
* @param code
* @return
*/
private int getGifIconID(String code) {
int icon = R.drawable.business_tag_qcp;
switch (code) {
case GlobalUrlConfig.businesscode_qcp:
//汽車(chē)票
icon = R.drawable.gif_business_tag_qcp;
break;
case GlobalUrlConfig.businesscode_zx:
//專(zhuān)線(xiàn)
icon = R.drawable.gif_business_tag_dzzx;
break;
case GlobalUrlConfig.businesscode_hcp:
//火車(chē)票
icon = R.drawable.gif_business_tag_hcp;
break;
default:
break;
}
return icon;
}
/**
* 設(shè)置字體漸變
* @param progress
*/
public void setProgress(float progress) {
if (progress == 0) {
tv_name.setVisibility(VISIBLE);
}
tv_name.setTextColor(evaluate(progress, COLOR_DEFAULT, COLOR_SELECT));
}
/**
* 顏色漸變器,返回漸變的顏色值
*
* @param fraction 進(jìn)度
* @param startValue
* @param endValue
* @return
*/
private int evaluate(float fraction, int startValue, int endValue) {
int startInt = (Integer) startValue;
float startA = ((startInt >> 24) & 0xff) / 255.0f;
float startR = ((startInt >> 16) & 0xff) / 255.0f;
float startG = ((startInt >> 8) & 0xff) / 255.0f;
float startB = (startInt & 0xff) / 255.0f;
int endInt = (Integer) endValue;
float endA = ((endInt >> 24) & 0xff) / 255.0f;
float endR = ((endInt >> 16) & 0xff) / 255.0f;
float endG = ((endInt >> 8) & 0xff) / 255.0f;
float endB = (endInt & 0xff) / 255.0f;
// convert from sRGB to linear
startR = (float) Math.pow(startR, 2.2);
startG = (float) Math.pow(startG, 2.2);
startB = (float) Math.pow(startB, 2.2);
endR = (float) Math.pow(endR, 2.2);
endG = (float) Math.pow(endG, 2.2);
endB = (float) Math.pow(endB, 2.2);
// compute the interpolated color in linear space
float a = startA + fraction * (endA - startA);
float r = startR + fraction * (endR - startR);
float g = startG + fraction * (endG - startG);
float b = startB + fraction * (endB - startB);
// convert back to sRGB in the [0..255] range
a = a * 255.0f;
r = (float) Math.pow(r, 1.0 / 2.2) * 255.0f;
g = (float) Math.pow(g, 1.0 / 2.2) * 255.0f;
b = (float) Math.pow(b, 1.0 / 2.2) * 255.0f;
return Math.round(a) << 24 | Math.round(r) << 16 | Math.round(g) << 8 | Math.round(b);
}
}
2. 添加ItemView
private void addItem(List<BusinessTag> list) {
for (int i = 0; i < list.size(); i++) {
BusinessTag businessTag = list.get(i);
BusinessTabItemView myLinearLayout = new BusinessTabItemView(getContext());
myLinearLayout.setGravity(Gravity.CENTER);
myLinearLayout.setData(businessTag, i == 0 ? true : false);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
myLinearLayout.setLayoutParams(layoutParams);
addTab(i, myLinearLayout);
}
}
/**
* 添加到容器
* @param position
* @param 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, 0);
tabsContainer.addView(tab, position, shouldExpand ? expandedTabLayoutParams : defaultTabLayoutParams);
}
上面是項(xiàng)目中需要的效果捎谨,如果只是展示一個(gè)TextView或者是一個(gè)Icon加TextView的話(huà)請(qǐng)看下面:
/**
* 帶icon的Item
* @param position
* @param title
* @param drawableId
*/
private void addTextTab(final int position, String title, int drawableId) {
TextView tab = new TextView(getContext());
tab.setText(title);
tab.setGravity(Gravity.CENTER);
tab.setSingleLine();
Drawable drawable = getContext().getDrawable(drawableId);
if (drawable != null) {
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getIntrinsicHeight());
tab.setCompoundDrawables(drawable, null, null, null);
tab.setCompoundDrawablePadding(20);
}
addTab(position, tab);
}
private void addTextTab(final int position, String title) {
TextView tab = new TextView(getContext());
tab.setText(title);
tab.setGravity(Gravity.CENTER);
tab.setSingleLine();
tab.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
addTab(position, tab);
}
3.繪制藍(lán)色背景
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (isInEditMode() || tabCount == 0) {
return;
}
final int height = getHeight();
// default: line below current tab
View currentTab = tabsContainer.getChildAt(currentPosition);
float lineLeft = currentTab.getLeft();
float lineRight = currentTab.getRight();
//設(shè)置為時(shí)權(quán)重 要減去item本身和兩邊padding再除以2得到左右編劇偏移量
int side = (currentTab.getWidth() - Util.dp2px(getContext(), 75) - tabPadding * 2) / 2;
//最小邊距
int leastSide = Util.dp2px(getContext(), 5);
//如果小于最小邊距就等于最小邊距
if (side < leastSide) {
side = leastSide;
}
// 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);
final float nextTabLeft = nextTab.getLeft();
final float nextTabRight = nextTab.getRight();
lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft);
lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight);
}
//繪制背景
canvas.drawRoundRect(lineLeft + side, bgTopBottomMargin, lineRight - side, height - bgTopBottomMargin, radius, radius, dividerPaint);
//canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);
}
- canvas.drawRoundRect(lineLeft , top, lineRight , height , radius, radius, dividerPaint);
top:繪制的高度起始點(diǎn)
left:繪制寬的結(jié)束點(diǎn)
bottom:繪制的高度結(jié)束點(diǎn)
rx:x軸弧度
ry:y軸弧度
paint:畫(huà)筆
int side = (currentTab.getWidth() - Util.dp2px(getContext(), 75) - tabPadding * 2) / 2;
side item寬減(item里text加圖片的寬)減兩邊padding再除以二得到左右邊距移量民效,目的是讓左右邊距加大,藍(lán)色背景繪制窄一點(diǎn)涛救,充滿(mǎn)的話(huà)效果不好畏邢。
4. 計(jì)算滑動(dòng)距離并在ViewPager的滑動(dòng)事件調(diào)用
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);
}
}
private class PageListener implements ViewPager.OnPageChangeListener {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
currentPosition = position;
currentPositionOffset = positionOffset;
scrollToChild(position, (int) (positionOffset * tabsContainer.getChildAt(position).getWidth()));
invalidate();
if (delegatePageListener != null) {
delegatePageListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
//控制文字顏色
if (positionOffset > 0) {
BusinessTabItemView left = (BusinessTabItemView) tabsContainer.getChildAt(position);
BusinessTabItemView right = (BusinessTabItemView) tabsContainer.getChildAt(position + 1);
left.setProgress((1 - positionOffset));
right.setProgress(positionOffset);
}
}
@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) {
if (delegatePageListener != null) {
delegatePageListener.onPageSelected(position);
}
for (int i = 0; i < tagList.size(); i++) {
BusinessTag businessTag = tagList.get(i);
BusinessTabItemView childAt = (BusinessTabItemView) tabsContainer.getChildAt(i);
childAt.setChoice(businessTag, i == position ? true : false);
}
}
}
- onPageSelected()里的代碼可以不用
SlidingTabView全部代碼
public class SlidingTabView extends HorizontalScrollView {
/**
* Item之間的間距
*/
private int margins;
public interface IconTabProvider {
public int getPageIconResId(int position);
}
// @formatter:off
private static final int[] ATTRS = new int[]{android.R.attr.textSize, android.R.attr.textColor};
// @formatter:on
private LinearLayout.LayoutParams defaultTabLayoutParams;
private LinearLayout.LayoutParams expandedTabLayoutParams;
private final PageListener pageListener = new PageListener();
public ViewPager.OnPageChangeListener delegatePageListener;
private LinearLayout tabsContainer;
private ViewPager pager;
private int tabCount;
private int currentPosition = 0;
private float currentPositionOffset = 0f;
private Paint dividerPaint;
private int dividerColor = 0x1A000000;
private boolean shouldExpand = false;
private int scrollOffset = 52;
private int radius = Util.dp2px(getContext(), 10);
private int tabPadding = 24;
private int dividerWidth = 1;
private int lastScrollX = 0;
private int bgTopBottomMargin = 10;
private Locale locale;
/**
* 業(yè)務(wù)數(shù)據(jù)
*/
private List<BusinessTag> tagList;
public SlidingTabView(Context context) {
this(context, null);
}
public SlidingTabView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SlidingTabView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFillViewport(true);
setWillNotDraw(false);
tabsContainer = new LinearLayout(context);
tabsContainer.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
tabsContainer.setLayoutParams(layoutParams);
addView(tabsContainer);
DisplayMetrics dm = getResources().getDisplayMetrics();
scrollOffset = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, scrollOffset, dm);
tabPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, tabPadding, dm);
dividerWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dividerWidth, dm);
bgTopBottomMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, bgTopBottomMargin, dm);
// get system attrs (android:textSize and android:textColor)
TypedArray a = context.obtainStyledAttributes(attrs, ATTRS);
a.recycle();
// get custom attrs
a = context.obtainStyledAttributes(attrs, R.styleable.BusinessSlidingTabView);
//是否需要鋪滿(mǎn) 必須為T(mén)rue
shouldExpand = a.getBoolean(R.styleable.BusinessSlidingTabView_pstsShouldExpand, shouldExpand);
scrollOffset = a.getDimensionPixelSize(R.styleable.BusinessSlidingTabView_pstsScrollOffset, scrollOffset);
//弧度
radius = a.getDimensionPixelSize(R.styleable.BusinessSlidingTabView_pstsRadius, radius);
//背景色
dividerColor = a.getColor(R.styleable.BusinessSlidingTabView_pstsDividerColor, dividerColor);
// padding
tabPadding = a.getDimensionPixelSize(R.styleable.BusinessSlidingTabView_pstsTabPaddingLeftRight, tabPadding);
a.recycle();
dividerPaint = new Paint();
dividerPaint.setAntiAlias(true);
dividerPaint.setStrokeWidth(dividerWidth);
dividerPaint.setColor(dividerColor);
defaultTabLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
expandedTabLayoutParams = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f);
if (locale == null) {
locale = getResources().getConfiguration().locale;
}
}
/**
* 與ViewPager關(guān)聯(lián)
* @param pager
* @param list
*/
public void setViewPager(ViewPager pager, List<BusinessTag> list) {
this.pager = pager;
tagList = list;
if (pager.getAdapter() == null) {
throw new IllegalStateException("ViewPager does not have adapter instance.");
}
pager.addOnPageChangeListener(pageListener);
notifyDataSetChanged(list);
}
public void setOnPageChangeListener(ViewPager.OnPageChangeListener listener) {
this.delegatePageListener = listener;
}
/**
* 添加刷新
* @param list
*/
public void notifyDataSetChanged(List<BusinessTag> list) {
tabsContainer.removeAllViews();
tabCount = list.size();
addItem(list);
//updateTabStyles();
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, int drawableId) {
TextView tab = new TextView(getContext());
tab.setText(title);
tab.setGravity(Gravity.CENTER);
tab.setSingleLine();
Drawable drawable = getContext().getDrawable(drawableId);
if (drawable != null) {
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getIntrinsicHeight());
tab.setCompoundDrawables(drawable, null, null, null);
tab.setCompoundDrawablePadding(20);
}
addTab(position, tab);
}
/**
* 添加Item
* @param list
*/
private void addItem(List<BusinessTag> list) {
for (int i = 0; i < list.size(); i++) {
BusinessTag businessTag = list.get(i);
BusinessTabItemView myLinearLayout = new BusinessTabItemView(getContext());
myLinearLayout.setGravity(Gravity.CENTER);
myLinearLayout.setData(businessTag, i == 0 ? true : false);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// layoutParams.setMargins(100,0,100,0);
myLinearLayout.setLayoutParams(layoutParams);
addTab(i, myLinearLayout);
}
}
private void addTextTab(final int position, String title) {
TextView tab = new TextView(getContext());
tab.setText(title);
tab.setGravity(Gravity.CENTER);
tab.setSingleLine();
tab.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
addTab(position, tab);
}
/**
* 添加到容器
* @param position
* @param 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, 0);
tabsContainer.addView(tab, position, shouldExpand ? expandedTabLayoutParams : defaultTabLayoutParams);
}
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);
if (isInEditMode() || tabCount == 0) {
return;
}
final int height = getHeight();
// default: line below current tab
View currentTab = tabsContainer.getChildAt(currentPosition);
float lineLeft = currentTab.getLeft();
float lineRight = currentTab.getRight();
//設(shè)置為時(shí)權(quán)重 要減去item本身和兩邊padding再除以2得到左右編劇偏移量
int side = (currentTab.getWidth() - Util.dp2px(getContext(), 75) - tabPadding * 2) / 2;
//最小邊距
int leastSide = Util.dp2px(getContext(), 5);
//如果小于最小邊距就等于最小邊距
if (side < leastSide) {
side = leastSide;
}
// 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);
final float nextTabLeft = nextTab.getLeft();
final float nextTabRight = nextTab.getRight();
lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft);
lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight);
Log.e("zby", "左:"+lineLeft+"----"+"右:"+lineRight);
}
//繪制背景
canvas.drawRoundRect(lineLeft , 0, lineRight , height , radius, radius, dividerPaint);
//canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);
}
private class PageListener implements ViewPager.OnPageChangeListener {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
currentPosition = position;
currentPositionOffset = positionOffset;
scrollToChild(position, (int) (positionOffset * tabsContainer.getChildAt(position).getWidth()));
invalidate();
if (delegatePageListener != null) {
delegatePageListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
//控制文字顏色
if (positionOffset > 0) {
BusinessTabItemView left = (BusinessTabItemView) tabsContainer.getChildAt(position);
BusinessTabItemView right = (BusinessTabItemView) tabsContainer.getChildAt(position + 1);
left.setProgress((1 - positionOffset));
right.setProgress(positionOffset);
}
}
@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) {
if (delegatePageListener != null) {
delegatePageListener.onPageSelected(position);
}
for (int i = 0; i < tagList.size(); i++) {
BusinessTag businessTag = tagList.get(i);
BusinessTabItemView childAt = (BusinessTabItemView) tabsContainer.getChildAt(i);
childAt.setChoice(businessTag, i == position ? true : false);
}
}
}
public int setChoice(String code){
int choice = 0;
if (tagList == null || tagList.size() <= 0) {
choice = 0;
} else {
try {
choice = tagList.indexOf(new BusinessTag(code));
} catch (Exception e) {
e.printStackTrace();
}
if (choice < 0) {
choice = 0;
}
}
return choice;
}
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 setScrollOffset(int scrollOffsetPx) {
this.scrollOffset = scrollOffsetPx;
invalidate();
}
public int getScrollOffset() {
return scrollOffset;
}
public void setShouldExpand(boolean shouldExpand) {
this.shouldExpand = shouldExpand;
requestLayout();
}
public boolean getShouldExpand() {
return shouldExpand;
}
public int getTabPaddingLeftRight() {
return tabPadding;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
SavedState savedState = (SavedState) state;
super.onRestoreInstanceState(savedState.getSuperState());
currentPosition = savedState.currentPosition;
requestLayout();
}
@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState savedState = new SavedState(superState);
savedState.currentPosition = currentPosition;
return savedState;
}
static class SavedState extends BaseSavedState {
int currentPosition;
public SavedState(Parcelable superState) {
super(superState);
}
private SavedState(Parcel in) {
super(in);
currentPosition = in.readInt();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeInt(currentPosition);
}
public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
@Override
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
@Override
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
}
XML布局
pstsDividerColor:背景色
pstsRadius:背景弧度
pstsTabPaddingLeftRight:左右邊距
pstsShouldExpand:是否設(shè)權(quán)重 (建議必須設(shè))
<cn.nova.phone.common.view.BusinessSlidingTabView
android:id="@+id/businessTagView"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="center"
android:background="@color/white"
app:pstsDividerColor="@color/blue"
app:pstsRadius="15dp"
app:pstsShouldExpand="true"
app:pstsTabPaddingLeftRight="5dp" />
調(diào)用一行代碼搞定
businessTagView.setViewPager(vp_main,tagList);