From f8c9237dbb3ef83aae406cfa0216176a5fb9ca53 Mon Sep 17 00:00:00 2001 From: Fanhuitong <981964879@qq.com> Date: Fri, 28 Jun 2024 09:57:18 +0800 Subject: [PATCH] =?UTF-8?q?version:5.8=20fix:=20update:=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=8B=A8=E6=89=93=E7=94=B5=E8=AF=9D=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FlycoTabLayoutZ_Lib/.gitignore | 1 + FlycoTabLayoutZ_Lib/build.gradle | 38 + FlycoTabLayoutZ_Lib/proguard-rules.pro | 17 + .../src/main/AndroidManifest.xml | 3 + .../com/flyco/tablayout/CommonTabLayout.java | 985 +++++++++++++ .../com/flyco/tablayout/SegmentTabLayout.java | 775 +++++++++++ .../tablayout/SlidingScaleTabLayout.java | 1216 +++++++++++++++++ .../com/flyco/tablayout/SlidingTabLayout.java | 1011 ++++++++++++++ .../tablayout/listener/CustomTabEntity.java | 13 + .../listener/OnTabSelectListener.java | 6 + .../transformer/ExtendTransformer.java | 52 + .../transformer/ITabScaleTransformer.java | 6 + .../transformer/IViewPagerTransformer.java | 12 + .../transformer/TabScaleTransformer.java | 110 ++ .../utils/FragmentChangeManager.java | 55 + .../flyco/tablayout/utils/UnreadMsgUtils.java | 58 + .../com/flyco/tablayout/utils/ViewUtils.java | 39 + .../com/flyco/tablayout/widget/MsgView.java | 157 +++ .../src/main/res/layout/layout_scale_tab.xml | 38 + .../src/main/res/layout/layout_tab.xml | 31 + .../src/main/res/layout/layout_tab_bottom.xml | 45 + .../src/main/res/layout/layout_tab_left.xml | 44 + .../src/main/res/layout/layout_tab_right.xml | 45 + .../main/res/layout/layout_tab_segment.xml | 39 + .../src/main/res/layout/layout_tab_top.xml | 44 + .../src/main/res/values/attrs.xml | 319 +++++ app/build.gradle | 6 +- app/src/main/AndroidManifest.xml | 1 + .../activity/contact/AddContactActivity.java | 10 +- .../activity/contact/AddContactViewModel.java | 10 + .../aios/activity/phone/PhoneActivity.java | 17 +- .../aios/adapter/CallRecordAdapter.java | 16 +- .../fragment/control/ControlFragment.java | 35 +- .../phone/contact/ContactFragment.java | 41 +- .../fragment/phone/dialer/DialerFragment.java | 11 +- .../phone/dialer/DialerViewModel.java | 7 +- .../fragment/phone/record/RecordFragment.java | 10 +- .../res/drawable-hdpi/add_contact_back.png | Bin 0 -> 2332 bytes .../add_contact_confirm_background.xml | 13 + .../add_wechat_contact_background.xml | 13 + .../res/drawable/bt_dialer_background.xml | 6 + .../drawable/bt_dialer_other_background.xml | 6 + .../main/res/drawable/contact_adapter_bg.xml | 13 + .../res/drawable/contact_add_contact_bg.xml | 18 + app/src/main/res/drawable/dialer_call_bg.xml | 9 +- .../res/drawable/seekbar_progress_default.xml | 2 +- .../main/res/layout-land/activity_phone.xml | 17 + .../main/res/layout-port/activity_phone.xml | 27 +- .../main/res/layout-port/fragment_home.xml | 1 - .../main/res/layout/activity_add_contact.xml | 363 +++-- .../layout/activity_add_wechat_contact.xml | 474 ++++--- app/src/main/res/layout/fragment_contact.xml | 124 +- .../main/res/layout/fragment_dialer_new.xml | 525 +++++++ app/src/main/res/layout/fragment_record.xml | 4 +- app/src/main/res/layout/item_call_record.xml | 43 +- app/src/main/res/layout/item_contact.xml | 35 +- .../res/layout/phone_fragment_control.xml | 3 +- settings.gradle | 2 +- 58 files changed, 6537 insertions(+), 484 deletions(-) create mode 100644 FlycoTabLayoutZ_Lib/.gitignore create mode 100644 FlycoTabLayoutZ_Lib/build.gradle create mode 100644 FlycoTabLayoutZ_Lib/proguard-rules.pro create mode 100644 FlycoTabLayoutZ_Lib/src/main/AndroidManifest.xml create mode 100644 FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/CommonTabLayout.java create mode 100644 FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/SegmentTabLayout.java create mode 100644 FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/SlidingScaleTabLayout.java create mode 100644 FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java create mode 100644 FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/listener/CustomTabEntity.java create mode 100644 FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/listener/OnTabSelectListener.java create mode 100644 FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/transformer/ExtendTransformer.java create mode 100644 FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/transformer/ITabScaleTransformer.java create mode 100644 FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/transformer/IViewPagerTransformer.java create mode 100644 FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/transformer/TabScaleTransformer.java create mode 100644 FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/utils/FragmentChangeManager.java create mode 100644 FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/utils/UnreadMsgUtils.java create mode 100644 FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/utils/ViewUtils.java create mode 100644 FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/widget/MsgView.java create mode 100644 FlycoTabLayoutZ_Lib/src/main/res/layout/layout_scale_tab.xml create mode 100644 FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab.xml create mode 100644 FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_bottom.xml create mode 100644 FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_left.xml create mode 100644 FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_right.xml create mode 100644 FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_segment.xml create mode 100644 FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_top.xml create mode 100644 FlycoTabLayoutZ_Lib/src/main/res/values/attrs.xml create mode 100644 app/src/main/java/com/uiuios/aios/activity/contact/AddContactViewModel.java create mode 100644 app/src/main/res/drawable-hdpi/add_contact_back.png create mode 100644 app/src/main/res/drawable/add_contact_confirm_background.xml create mode 100644 app/src/main/res/drawable/add_wechat_contact_background.xml create mode 100644 app/src/main/res/drawable/bt_dialer_background.xml create mode 100644 app/src/main/res/drawable/bt_dialer_other_background.xml create mode 100644 app/src/main/res/drawable/contact_adapter_bg.xml create mode 100644 app/src/main/res/drawable/contact_add_contact_bg.xml create mode 100644 app/src/main/res/layout/fragment_dialer_new.xml diff --git a/FlycoTabLayoutZ_Lib/.gitignore b/FlycoTabLayoutZ_Lib/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/FlycoTabLayoutZ_Lib/.gitignore @@ -0,0 +1 @@ +/build diff --git a/FlycoTabLayoutZ_Lib/build.gradle b/FlycoTabLayoutZ_Lib/build.gradle new file mode 100644 index 0000000..e21b40c --- /dev/null +++ b/FlycoTabLayoutZ_Lib/build.gradle @@ -0,0 +1,38 @@ +apply plugin: 'com.android.library' +//apply plugin: 'com.novoda.bintray-release' + +version = "1.3.3" +android { + compileSdkVersion 28 + buildToolsVersion "28.0.3" + + defaultConfig { + minSdkVersion 17 + targetSdkVersion 28 + versionCode 5 + versionName version + } + buildTypes { + U807Debug {} + U807Release {} + iPlay50SEDebug {} + iPlay50SERelease {} + debug {} + release {} + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation 'androidx.legacy:legacy-support-v4:1.0.0' +} + +//// jcenter发布的信息 +//publish { +// userOrg = 'lizp' // 创建repo的位置 +// groupId = 'com.lzp' // 引用的分组名称 +// artifactId = 'FlycoTabLayoutZ'//项目名称 +// publishVersion = version//版本号 +// desc = '在FlycoTabLayout的基础上,扩展出SlidingScaleTabLayout,实现滑动可以改变tab字体的大小的切换效果' +// website = 'https://github.com/li504799868/FlycoTabLayoutZ' +//} \ No newline at end of file diff --git a/FlycoTabLayoutZ_Lib/proguard-rules.pro b/FlycoTabLayoutZ_Lib/proguard-rules.pro new file mode 100644 index 0000000..48100e3 --- /dev/null +++ b/FlycoTabLayoutZ_Lib/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /Users/lihui/work/AndroidStudio/sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/FlycoTabLayoutZ_Lib/src/main/AndroidManifest.xml b/FlycoTabLayoutZ_Lib/src/main/AndroidManifest.xml new file mode 100644 index 0000000..65a3365 --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/AndroidManifest.xml @@ -0,0 +1,3 @@ + + diff --git a/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/CommonTabLayout.java b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/CommonTabLayout.java new file mode 100644 index 0000000..58f5e0b --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/CommonTabLayout.java @@ -0,0 +1,985 @@ +package com.flyco.tablayout; + +import android.animation.TypeEvaluator; +import android.animation.ValueAnimator; +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Path; +import android.graphics.Rect; +import android.graphics.drawable.GradientDrawable; +import android.os.Bundle; +import android.os.Parcelable; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; +import android.util.AttributeSet; +import android.util.SparseArray; +import android.util.TypedValue; +import android.view.Gravity; +import android.view.View; +import android.view.ViewGroup; +import android.view.animation.OvershootInterpolator; +import android.widget.FrameLayout; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.TextView; + +import com.flyco.tablayout.listener.CustomTabEntity; +import com.flyco.tablayout.listener.OnTabSelectListener; +import com.flyco.tablayout.utils.FragmentChangeManager; +import com.flyco.tablayout.utils.UnreadMsgUtils; +import com.flyco.tablayout.widget.MsgView; + +import java.util.ArrayList; + +/** + * 没有继承HorizontalScrollView不能滑动,对于ViewPager无依赖 + */ +public class CommonTabLayout extends FrameLayout implements ValueAnimator.AnimatorUpdateListener { + private Context mContext; + private ArrayList mTabEntitys = new ArrayList<>(); + private LinearLayout mTabsContainer; + private int mCurrentTab; + private int mLastTab; + private int mTabCount; + /** + * 用于绘制显示器 + */ + private Rect mIndicatorRect = new Rect(); + private GradientDrawable mIndicatorDrawable = new GradientDrawable(); + + private Paint mRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + private Paint mDividerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + private Paint mTrianglePaint = new Paint(Paint.ANTI_ALIAS_FLAG); + private Path mTrianglePath = new Path(); + private static final int STYLE_NORMAL = 0; + private static final int STYLE_TRIANGLE = 1; + private static final int STYLE_BLOCK = 2; + private int mIndicatorStyle = STYLE_NORMAL; + + private float mTabPadding; + private boolean mTabSpaceEqual; + private float mTabWidth; + + /** + * indicator + */ + private int mIndicatorColor; + private float mIndicatorHeight; + private float mIndicatorWidth; + private float mIndicatorCornerRadius; + private float mIndicatorMarginLeft; + private float mIndicatorMarginTop; + private float mIndicatorMarginRight; + private float mIndicatorMarginBottom; + private long mIndicatorAnimDuration; + private boolean mIndicatorAnimEnable; + private boolean mIndicatorBounceEnable; + private int mIndicatorGravity; + + /** + * underline + */ + private int mUnderlineColor; + private float mUnderlineHeight; + private int mUnderlineGravity; + + /** + * divider + */ + private int mDividerColor; + private float mDividerWidth; + private float mDividerPadding; + + /** + * title + */ + private static final int TEXT_BOLD_NONE = 0; + private static final int TEXT_BOLD_WHEN_SELECT = 1; + private static final int TEXT_BOLD_BOTH = 2; + private float mTextsize; + private int mTextSelectColor; + private int mTextUnselectColor; + private int mTextBold; + private boolean mTextAllCaps; + + /** + * icon + */ + private boolean mIconVisible; + private int mIconGravity; + private float mIconWidth; + private float mIconHeight; + private float mIconMargin; + + private int mHeight; + + /** + * anim + */ + private ValueAnimator mValueAnimator; + private OvershootInterpolator mInterpolator = new OvershootInterpolator(1.5f); + + private FragmentChangeManager mFragmentChangeManager; + + public CommonTabLayout(Context context) { + this(context, null, 0); + } + + public CommonTabLayout(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public CommonTabLayout(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + setWillNotDraw(false);//重写onDraw方法,需要调用这个方法来清除flag + setClipChildren(false); + setClipToPadding(false); + + this.mContext = context; + mTabsContainer = new LinearLayout(context); + addView(mTabsContainer); + + obtainAttributes(context, attrs); + + //get layout_height + String height = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_height"); + + //create ViewPager + if (height.equals(ViewGroup.LayoutParams.MATCH_PARENT + "")) { + } else if (height.equals(ViewGroup.LayoutParams.WRAP_CONTENT + "")) { + } else { + int[] systemAttrs = {android.R.attr.layout_height}; + TypedArray a = context.obtainStyledAttributes(attrs, systemAttrs); + mHeight = a.getDimensionPixelSize(0, ViewGroup.LayoutParams.WRAP_CONTENT); + a.recycle(); + } + + mValueAnimator = ValueAnimator.ofObject(new PointEvaluator(), mLastP, mCurrentP); + mValueAnimator.addUpdateListener(this); + } + + private void obtainAttributes(Context context, AttributeSet attrs) { + TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CommonTabLayout); + + mIndicatorStyle = ta.getInt(R.styleable.CommonTabLayout_tl_indicator_style, 0); + mIndicatorColor = ta.getColor(R.styleable.CommonTabLayout_tl_indicator_color, Color.parseColor(mIndicatorStyle == STYLE_BLOCK ? "#4B6A87" : "#ffffff")); + mIndicatorHeight = ta.getDimension(R.styleable.CommonTabLayout_tl_indicator_height, + dp2px(mIndicatorStyle == STYLE_TRIANGLE ? 4 : (mIndicatorStyle == STYLE_BLOCK ? -1 : 2))); + mIndicatorWidth = ta.getDimension(R.styleable.CommonTabLayout_tl_indicator_width, dp2px(mIndicatorStyle == STYLE_TRIANGLE ? 10 : -1)); + mIndicatorCornerRadius = ta.getDimension(R.styleable.CommonTabLayout_tl_indicator_corner_radius, dp2px(mIndicatorStyle == STYLE_BLOCK ? -1 : 0)); + mIndicatorMarginLeft = ta.getDimension(R.styleable.CommonTabLayout_tl_indicator_margin_left, dp2px(0)); + mIndicatorMarginTop = ta.getDimension(R.styleable.CommonTabLayout_tl_indicator_margin_top, dp2px(mIndicatorStyle == STYLE_BLOCK ? 7 : 0)); + mIndicatorMarginRight = ta.getDimension(R.styleable.CommonTabLayout_tl_indicator_margin_right, dp2px(0)); + mIndicatorMarginBottom = ta.getDimension(R.styleable.CommonTabLayout_tl_indicator_margin_bottom, dp2px(mIndicatorStyle == STYLE_BLOCK ? 7 : 0)); + mIndicatorAnimEnable = ta.getBoolean(R.styleable.CommonTabLayout_tl_indicator_anim_enable, true); + mIndicatorBounceEnable = ta.getBoolean(R.styleable.CommonTabLayout_tl_indicator_bounce_enable, true); + mIndicatorAnimDuration = ta.getInt(R.styleable.CommonTabLayout_tl_indicator_anim_duration, -1); + mIndicatorGravity = ta.getInt(R.styleable.CommonTabLayout_tl_indicator_gravity, Gravity.BOTTOM); + + mUnderlineColor = ta.getColor(R.styleable.CommonTabLayout_tl_underline_color, Color.parseColor("#ffffff")); + mUnderlineHeight = ta.getDimension(R.styleable.CommonTabLayout_tl_underline_height, dp2px(0)); + mUnderlineGravity = ta.getInt(R.styleable.CommonTabLayout_tl_underline_gravity, Gravity.BOTTOM); + + mDividerColor = ta.getColor(R.styleable.CommonTabLayout_tl_divider_color, Color.parseColor("#ffffff")); + mDividerWidth = ta.getDimension(R.styleable.CommonTabLayout_tl_divider_width, dp2px(0)); + mDividerPadding = ta.getDimension(R.styleable.CommonTabLayout_tl_divider_padding, dp2px(12)); + + mTextsize = ta.getDimension(R.styleable.CommonTabLayout_tl_textSize, sp2px(13f)); + mTextSelectColor = ta.getColor(R.styleable.CommonTabLayout_tl_textSelectColor, Color.parseColor("#ffffff")); + mTextUnselectColor = ta.getColor(R.styleable.CommonTabLayout_tl_textUnSelectColor, Color.parseColor("#AAffffff")); + mTextBold = ta.getInt(R.styleable.CommonTabLayout_tl_textBold, TEXT_BOLD_NONE); + mTextAllCaps = ta.getBoolean(R.styleable.CommonTabLayout_tl_textAllCaps, false); + + mIconVisible = ta.getBoolean(R.styleable.CommonTabLayout_tl_iconVisible, true); + mIconGravity = ta.getInt(R.styleable.CommonTabLayout_tl_iconGravity, Gravity.TOP); + mIconWidth = ta.getDimension(R.styleable.CommonTabLayout_tl_iconWidth, dp2px(0)); + mIconHeight = ta.getDimension(R.styleable.CommonTabLayout_tl_iconHeight, dp2px(0)); + mIconMargin = ta.getDimension(R.styleable.CommonTabLayout_tl_iconMargin, dp2px(2.5f)); + + mTabSpaceEqual = ta.getBoolean(R.styleable.CommonTabLayout_tl_tab_space_equal, true); + mTabWidth = ta.getDimension(R.styleable.CommonTabLayout_tl_tab_width, dp2px(-1)); + mTabPadding = ta.getDimension(R.styleable.CommonTabLayout_tl_tab_padding, mTabSpaceEqual || mTabWidth > 0 ? dp2px(0) : dp2px(10)); + + ta.recycle(); + } + + public void setTabData(ArrayList tabEntitys) { + if (tabEntitys == null || tabEntitys.size() == 0) { + throw new IllegalStateException("TabEntitys can not be NULL or EMPTY !"); + } + + this.mTabEntitys.clear(); + this.mTabEntitys.addAll(tabEntitys); + + notifyDataSetChanged(); + } + + /** + * 关联数据支持同时切换fragments + */ + public void setTabData(ArrayList tabEntitys, FragmentActivity fa, int containerViewId, ArrayList fragments) { + mFragmentChangeManager = new FragmentChangeManager(fa.getSupportFragmentManager(), containerViewId, fragments); + setTabData(tabEntitys); + } + + /** + * 更新数据 + */ + public void notifyDataSetChanged() { + mTabsContainer.removeAllViews(); + this.mTabCount = mTabEntitys.size(); + View tabView; + for (int i = 0; i < mTabCount; i++) { + if (mIconGravity == Gravity.LEFT) { + tabView = View.inflate(mContext, R.layout.layout_tab_left, null); + } else if (mIconGravity == Gravity.RIGHT) { + tabView = View.inflate(mContext, R.layout.layout_tab_right, null); + } else if (mIconGravity == Gravity.BOTTOM) { + tabView = View.inflate(mContext, R.layout.layout_tab_bottom, null); + } else { + tabView = View.inflate(mContext, R.layout.layout_tab_top, null); + } + + tabView.setTag(i); + addTab(i, tabView); + } + + updateTabStyles(); + } + + /** + * 创建并添加tab + */ + private void addTab(final int position, View tabView) { + TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title); + tv_tab_title.setText(mTabEntitys.get(position).getTabTitle()); + ImageView iv_tab_icon = (ImageView) tabView.findViewById(R.id.iv_tab_icon); + iv_tab_icon.setImageResource(mTabEntitys.get(position).getTabUnselectedIcon()); + + tabView.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + int position = (Integer) v.getTag(); + if (mCurrentTab != position) { + setCurrentTab(position); + if (mListener != null) { + mListener.onTabSelect(position); + } + } else { + if (mListener != null) { + mListener.onTabReselect(position); + } + } + } + }); + + /** 每一个Tab的布局参数 */ + LinearLayout.LayoutParams lp_tab = mTabSpaceEqual ? + new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f) : + new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); + if (mTabWidth > 0) { + lp_tab = new LinearLayout.LayoutParams((int) mTabWidth, LayoutParams.MATCH_PARENT); + } + mTabsContainer.addView(tabView, position, lp_tab); + } + + private void updateTabStyles() { + for (int i = 0; i < mTabCount; i++) { + View tabView = mTabsContainer.getChildAt(i); + tabView.setPadding((int) mTabPadding, 0, (int) mTabPadding, 0); + TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title); + tv_tab_title.setTextColor(i == mCurrentTab ? mTextSelectColor : mTextUnselectColor); + tv_tab_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextsize); +// tv_tab_title.setPadding((int) mTabPadding, 0, (int) mTabPadding, 0); + if (mTextAllCaps) { + tv_tab_title.setText(tv_tab_title.getText().toString().toUpperCase()); + } + + if (mTextBold == TEXT_BOLD_BOTH) { + tv_tab_title.getPaint().setFakeBoldText(true); + } + // 被选中设置为粗体 + else if (mTextBold == TEXT_BOLD_WHEN_SELECT && i == mCurrentTab) { + tv_tab_title.getPaint().setFakeBoldText(true); + } else if (mTextBold == TEXT_BOLD_NONE) { + tv_tab_title.getPaint().setFakeBoldText(false); + } + + ImageView iv_tab_icon = (ImageView) tabView.findViewById(R.id.iv_tab_icon); + if (mIconVisible) { + iv_tab_icon.setVisibility(View.VISIBLE); + CustomTabEntity tabEntity = mTabEntitys.get(i); + iv_tab_icon.setImageResource(i == mCurrentTab ? tabEntity.getTabSelectedIcon() : tabEntity.getTabUnselectedIcon()); + LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( + mIconWidth <= 0 ? LinearLayout.LayoutParams.WRAP_CONTENT : (int) mIconWidth, + mIconHeight <= 0 ? LinearLayout.LayoutParams.WRAP_CONTENT : (int) mIconHeight); + if (mIconGravity == Gravity.LEFT) { + lp.rightMargin = (int) mIconMargin; + } else if (mIconGravity == Gravity.RIGHT) { + lp.leftMargin = (int) mIconMargin; + } else if (mIconGravity == Gravity.BOTTOM) { + lp.topMargin = (int) mIconMargin; + } else { + lp.bottomMargin = (int) mIconMargin; + } + + iv_tab_icon.setLayoutParams(lp); + } else { + iv_tab_icon.setVisibility(View.GONE); + } + } + } + + private void updateTabSelection(int position) { + for (int i = 0; i < mTabCount; ++i) { + View tabView = mTabsContainer.getChildAt(i); + final boolean isSelect = i == position; + TextView tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title); + tab_title.setTextColor(isSelect ? mTextSelectColor : mTextUnselectColor); + ImageView iv_tab_icon = (ImageView) tabView.findViewById(R.id.iv_tab_icon); + CustomTabEntity tabEntity = mTabEntitys.get(i); + iv_tab_icon.setImageResource(isSelect ? tabEntity.getTabSelectedIcon() : tabEntity.getTabUnselectedIcon()); + if (mTextBold == TEXT_BOLD_WHEN_SELECT) { + tab_title.getPaint().setFakeBoldText(isSelect); + } + } + } + + private void calcOffset() { + final View currentTabView = mTabsContainer.getChildAt(this.mCurrentTab); + mCurrentP.left = currentTabView.getLeft(); + mCurrentP.right = currentTabView.getRight(); + + final View lastTabView = mTabsContainer.getChildAt(this.mLastTab); + mLastP.left = lastTabView.getLeft(); + mLastP.right = lastTabView.getRight(); + +// Log.d("AAA", "mLastP--->" + mLastP.left + "&" + mLastP.right); +// Log.d("AAA", "mCurrentP--->" + mCurrentP.left + "&" + mCurrentP.right); + if (mLastP.left == mCurrentP.left && mLastP.right == mCurrentP.right) { + invalidate(); + } else { + mValueAnimator.setObjectValues(mLastP, mCurrentP); + if (mIndicatorBounceEnable) { + mValueAnimator.setInterpolator(mInterpolator); + } + + if (mIndicatorAnimDuration < 0) { + mIndicatorAnimDuration = mIndicatorBounceEnable ? 500 : 250; + } + mValueAnimator.setDuration(mIndicatorAnimDuration); + mValueAnimator.start(); + } + } + + private void calcIndicatorRect() { + View currentTabView = mTabsContainer.getChildAt(this.mCurrentTab); + float left = currentTabView.getLeft(); + float right = currentTabView.getRight(); + + mIndicatorRect.left = (int) left; + mIndicatorRect.right = (int) right; + + if (mIndicatorWidth < 0) { //indicatorWidth小于0时,原jpardogo's PagerSlidingTabStrip + + } else {//indicatorWidth大于0时,圆角矩形以及三角形 + float indicatorLeft = currentTabView.getLeft() + (currentTabView.getWidth() - mIndicatorWidth) / 2; + + mIndicatorRect.left = (int) indicatorLeft; + mIndicatorRect.right = (int) (mIndicatorRect.left + mIndicatorWidth); + } + } + + @Override + public void onAnimationUpdate(ValueAnimator animation) { + View currentTabView = mTabsContainer.getChildAt(this.mCurrentTab); + IndicatorPoint p = (IndicatorPoint) animation.getAnimatedValue(); + mIndicatorRect.left = (int) p.left; + mIndicatorRect.right = (int) p.right; + + if (mIndicatorWidth < 0) { //indicatorWidth小于0时,原jpardogo's PagerSlidingTabStrip + + } else {//indicatorWidth大于0时,圆角矩形以及三角形 + float indicatorLeft = p.left + (currentTabView.getWidth() - mIndicatorWidth) / 2; + + mIndicatorRect.left = (int) indicatorLeft; + mIndicatorRect.right = (int) (mIndicatorRect.left + mIndicatorWidth); + } + invalidate(); + } + + private boolean mIsFirstDraw = true; + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + + if (isInEditMode() || mTabCount <= 0) { + return; + } + + int height = getHeight(); + int paddingLeft = getPaddingLeft(); + // draw divider + if (mDividerWidth > 0) { + mDividerPaint.setStrokeWidth(mDividerWidth); + mDividerPaint.setColor(mDividerColor); + for (int i = 0; i < mTabCount - 1; i++) { + View tab = mTabsContainer.getChildAt(i); + canvas.drawLine(paddingLeft + tab.getRight(), mDividerPadding, paddingLeft + tab.getRight(), height - mDividerPadding, mDividerPaint); + } + } + + // draw underline + if (mUnderlineHeight > 0) { + mRectPaint.setColor(mUnderlineColor); + if (mUnderlineGravity == Gravity.BOTTOM) { + canvas.drawRect(paddingLeft, height - mUnderlineHeight, mTabsContainer.getWidth() + paddingLeft, height, mRectPaint); + } else { + canvas.drawRect(paddingLeft, 0, mTabsContainer.getWidth() + paddingLeft, mUnderlineHeight, mRectPaint); + } + } + + //draw indicator line + if (mIndicatorAnimEnable) { + if (mIsFirstDraw) { + mIsFirstDraw = false; + calcIndicatorRect(); + } + } else { + calcIndicatorRect(); + } + + + if (mIndicatorStyle == STYLE_TRIANGLE) { + if (mIndicatorHeight > 0) { + mTrianglePaint.setColor(mIndicatorColor); + mTrianglePath.reset(); + mTrianglePath.moveTo(paddingLeft + mIndicatorRect.left, height); + mTrianglePath.lineTo(paddingLeft + mIndicatorRect.left / 2 + mIndicatorRect.right / 2, height - mIndicatorHeight); + mTrianglePath.lineTo(paddingLeft + mIndicatorRect.right, height); + mTrianglePath.close(); + canvas.drawPath(mTrianglePath, mTrianglePaint); + } + } else if (mIndicatorStyle == STYLE_BLOCK) { + if (mIndicatorHeight < 0) { + mIndicatorHeight = height - mIndicatorMarginTop - mIndicatorMarginBottom; + } else { + + } + + if (mIndicatorHeight > 0) { + if (mIndicatorCornerRadius < 0 || mIndicatorCornerRadius > mIndicatorHeight / 2) { + mIndicatorCornerRadius = mIndicatorHeight / 2; + } + + mIndicatorDrawable.setColor(mIndicatorColor); + mIndicatorDrawable.setBounds(paddingLeft + (int) mIndicatorMarginLeft + mIndicatorRect.left, + (int) mIndicatorMarginTop, (int) (paddingLeft + mIndicatorRect.right - mIndicatorMarginRight), + (int) (mIndicatorMarginTop + mIndicatorHeight)); + mIndicatorDrawable.setCornerRadius(mIndicatorCornerRadius); + mIndicatorDrawable.draw(canvas); + } + } else { + /* mRectPaint.setColor(mIndicatorColor); + calcIndicatorRect(); + canvas.drawRect(getPaddingLeft() + mIndicatorRect.left, getHeight() - mIndicatorHeight, + mIndicatorRect.right + getPaddingLeft(), getHeight(), mRectPaint);*/ + + if (mIndicatorHeight > 0) { + mIndicatorDrawable.setColor(mIndicatorColor); + if (mIndicatorGravity == Gravity.BOTTOM) { + mIndicatorDrawable.setBounds(paddingLeft + (int) mIndicatorMarginLeft + mIndicatorRect.left, + height - (int) mIndicatorHeight - (int) mIndicatorMarginBottom, + paddingLeft + mIndicatorRect.right - (int) mIndicatorMarginRight, + height - (int) mIndicatorMarginBottom); + } else { + mIndicatorDrawable.setBounds(paddingLeft + (int) mIndicatorMarginLeft + mIndicatorRect.left, + (int) mIndicatorMarginTop, + paddingLeft + mIndicatorRect.right - (int) mIndicatorMarginRight, + (int) mIndicatorHeight + (int) mIndicatorMarginTop); + } + mIndicatorDrawable.setCornerRadius(mIndicatorCornerRadius); + mIndicatorDrawable.draw(canvas); + } + } + } + + //setter and getter + public void setCurrentTab(int currentTab) { + mLastTab = this.mCurrentTab; + this.mCurrentTab = currentTab; + updateTabSelection(currentTab); + if (mFragmentChangeManager != null) { + mFragmentChangeManager.setFragments(currentTab); + } + if (mIndicatorAnimEnable) { + calcOffset(); + } else { + invalidate(); + } + } + + public void setIndicatorStyle(int indicatorStyle) { + this.mIndicatorStyle = indicatorStyle; + invalidate(); + } + + public void setTabPadding(float tabPadding) { + this.mTabPadding = dp2px(tabPadding); + updateTabStyles(); + } + + public void setTabSpaceEqual(boolean tabSpaceEqual) { + this.mTabSpaceEqual = tabSpaceEqual; + updateTabStyles(); + } + + public void setTabWidth(float tabWidth) { + this.mTabWidth = dp2px(tabWidth); + updateTabStyles(); + } + + public void setIndicatorColor(int indicatorColor) { + this.mIndicatorColor = indicatorColor; + invalidate(); + } + + public void setIndicatorHeight(float indicatorHeight) { + this.mIndicatorHeight = dp2px(indicatorHeight); + invalidate(); + } + + public void setIndicatorWidth(float indicatorWidth) { + this.mIndicatorWidth = dp2px(indicatorWidth); + invalidate(); + } + + public void setIndicatorCornerRadius(float indicatorCornerRadius) { + this.mIndicatorCornerRadius = dp2px(indicatorCornerRadius); + invalidate(); + } + + public void setIndicatorGravity(int indicatorGravity) { + this.mIndicatorGravity = indicatorGravity; + invalidate(); + } + + public void setIndicatorMargin(float indicatorMarginLeft, float indicatorMarginTop, + float indicatorMarginRight, float indicatorMarginBottom) { + this.mIndicatorMarginLeft = dp2px(indicatorMarginLeft); + this.mIndicatorMarginTop = dp2px(indicatorMarginTop); + this.mIndicatorMarginRight = dp2px(indicatorMarginRight); + this.mIndicatorMarginBottom = dp2px(indicatorMarginBottom); + invalidate(); + } + + public void setIndicatorAnimDuration(long indicatorAnimDuration) { + this.mIndicatorAnimDuration = indicatorAnimDuration; + } + + public void setIndicatorAnimEnable(boolean indicatorAnimEnable) { + this.mIndicatorAnimEnable = indicatorAnimEnable; + } + + public void setIndicatorBounceEnable(boolean indicatorBounceEnable) { + this.mIndicatorBounceEnable = indicatorBounceEnable; + } + + public void setUnderlineColor(int underlineColor) { + this.mUnderlineColor = underlineColor; + invalidate(); + } + + public void setUnderlineHeight(float underlineHeight) { + this.mUnderlineHeight = dp2px(underlineHeight); + invalidate(); + } + + public void setUnderlineGravity(int underlineGravity) { + this.mUnderlineGravity = underlineGravity; + invalidate(); + } + + public void setDividerColor(int dividerColor) { + this.mDividerColor = dividerColor; + invalidate(); + } + + public void setDividerWidth(float dividerWidth) { + this.mDividerWidth = dp2px(dividerWidth); + invalidate(); + } + + public void setDividerPadding(float dividerPadding) { + this.mDividerPadding = dp2px(dividerPadding); + invalidate(); + } + + public void setTextsize(float textsize) { + this.mTextsize = sp2px(textsize); + updateTabStyles(); + } + + public void setTextSelectColor(int textSelectColor) { + this.mTextSelectColor = textSelectColor; + updateTabStyles(); + } + + public void setTextUnselectColor(int textUnselectColor) { + this.mTextUnselectColor = textUnselectColor; + updateTabStyles(); + } + + public void setTextBold(int textBold) { + this.mTextBold = textBold; + updateTabStyles(); + } + + public void setIconVisible(boolean iconVisible) { + this.mIconVisible = iconVisible; + updateTabStyles(); + } + + public void setIconGravity(int iconGravity) { + this.mIconGravity = iconGravity; + notifyDataSetChanged(); + } + + public void setIconWidth(float iconWidth) { + this.mIconWidth = dp2px(iconWidth); + updateTabStyles(); + } + + public void setIconHeight(float iconHeight) { + this.mIconHeight = dp2px(iconHeight); + updateTabStyles(); + } + + public void setIconMargin(float iconMargin) { + this.mIconMargin = dp2px(iconMargin); + updateTabStyles(); + } + + public void setTextAllCaps(boolean textAllCaps) { + this.mTextAllCaps = textAllCaps; + updateTabStyles(); + } + + + public int getTabCount() { + return mTabCount; + } + + public int getCurrentTab() { + return mCurrentTab; + } + + public int getIndicatorStyle() { + return mIndicatorStyle; + } + + public float getTabPadding() { + return mTabPadding; + } + + public boolean isTabSpaceEqual() { + return mTabSpaceEqual; + } + + public float getTabWidth() { + return mTabWidth; + } + + public int getIndicatorColor() { + return mIndicatorColor; + } + + public float getIndicatorHeight() { + return mIndicatorHeight; + } + + public float getIndicatorWidth() { + return mIndicatorWidth; + } + + public float getIndicatorCornerRadius() { + return mIndicatorCornerRadius; + } + + public float getIndicatorMarginLeft() { + return mIndicatorMarginLeft; + } + + public float getIndicatorMarginTop() { + return mIndicatorMarginTop; + } + + public float getIndicatorMarginRight() { + return mIndicatorMarginRight; + } + + public float getIndicatorMarginBottom() { + return mIndicatorMarginBottom; + } + + public long getIndicatorAnimDuration() { + return mIndicatorAnimDuration; + } + + public boolean isIndicatorAnimEnable() { + return mIndicatorAnimEnable; + } + + public boolean isIndicatorBounceEnable() { + return mIndicatorBounceEnable; + } + + public int getUnderlineColor() { + return mUnderlineColor; + } + + public float getUnderlineHeight() { + return mUnderlineHeight; + } + + public int getDividerColor() { + return mDividerColor; + } + + public float getDividerWidth() { + return mDividerWidth; + } + + public float getDividerPadding() { + return mDividerPadding; + } + + public float getTextsize() { + return mTextsize; + } + + public int getTextSelectColor() { + return mTextSelectColor; + } + + public int getTextUnselectColor() { + return mTextUnselectColor; + } + + public int getTextBold() { + return mTextBold; + } + + public boolean isTextAllCaps() { + return mTextAllCaps; + } + + public int getIconGravity() { + return mIconGravity; + } + + public float getIconWidth() { + return mIconWidth; + } + + public float getIconHeight() { + return mIconHeight; + } + + public float getIconMargin() { + return mIconMargin; + } + + public boolean isIconVisible() { + return mIconVisible; + } + + + public ImageView getIconView(int tab) { + View tabView = mTabsContainer.getChildAt(tab); + ImageView iv_tab_icon = (ImageView) tabView.findViewById(R.id.iv_tab_icon); + return iv_tab_icon; + } + + public TextView getTitleView(int tab) { + View tabView = mTabsContainer.getChildAt(tab); + TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title); + return tv_tab_title; + } + + //setter and getter + + // show MsgTipView + private Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + private SparseArray mInitSetMap = new SparseArray<>(); + + /** + * 显示未读消息 + * + * @param position 显示tab位置 + * @param num num小于等于0显示红点,num大于0显示数字 + */ + public void showMsg(int position, int num) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + + View tabView = mTabsContainer.getChildAt(position); + MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip); + if (tipView != null) { + UnreadMsgUtils.show(tipView, num); + + if (mInitSetMap.get(position) != null && mInitSetMap.get(position)) { + return; + } + + if (!mIconVisible) { + setMsgMargin(position, 2, 2); + } else { + setMsgMargin(position, 0, + mIconGravity == Gravity.LEFT || mIconGravity == Gravity.RIGHT ? 4 : 0); + } + + mInitSetMap.put(position, true); + } + } + + /** + * 显示未读红点 + * + * @param position 显示tab位置 + */ + public void showDot(int position) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + showMsg(position, 0); + } + + public void hideMsg(int position) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + + View tabView = mTabsContainer.getChildAt(position); + MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip); + if (tipView != null) { + tipView.setVisibility(View.GONE); + } + } + + /** + * 设置提示红点偏移,注意 + * 1.控件为固定高度:参照点为tab内容的右上角 + * 2.控件高度不固定(WRAP_CONTENT):参照点为tab内容的右上角,此时高度已是红点的最高显示范围,所以这时bottomPadding其实就是topPadding + */ + public void setMsgMargin(int position, float leftPadding, float bottomPadding) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + View tabView = mTabsContainer.getChildAt(position); + MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip); + if (tipView != null) { + TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title); + mTextPaint.setTextSize(mTextsize); + float textWidth = mTextPaint.measureText(tv_tab_title.getText().toString()); + float textHeight = mTextPaint.descent() - mTextPaint.ascent(); + MarginLayoutParams lp = (MarginLayoutParams) tipView.getLayoutParams(); + + float iconH = mIconHeight; + float margin = 0; + if (mIconVisible) { + if (iconH <= 0) { + iconH = mContext.getResources().getDrawable(mTabEntitys.get(position).getTabSelectedIcon()).getIntrinsicHeight(); + } + margin = mIconMargin; + } + + if (mIconGravity == Gravity.TOP || mIconGravity == Gravity.BOTTOM) { + lp.leftMargin = dp2px(leftPadding); + lp.topMargin = mHeight > 0 ? (int) (mHeight - textHeight - iconH - margin) / 2 - dp2px(bottomPadding) : dp2px(bottomPadding); + } else { + lp.leftMargin = dp2px(leftPadding); + lp.topMargin = mHeight > 0 ? (int) (mHeight - Math.max(textHeight, iconH)) / 2 - dp2px(bottomPadding) : dp2px(bottomPadding); + } + + tipView.setLayoutParams(lp); + } + } + + /** + * 当前类只提供了少许设置未读消息属性的方法,可以通过该方法获取MsgView对象从而各种设置 + */ + public MsgView getMsgView(int position) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + View tabView = mTabsContainer.getChildAt(position); + MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip); + return tipView; + } + + private OnTabSelectListener mListener; + + public void setOnTabSelectListener(OnTabSelectListener listener) { + this.mListener = listener; + } + + + @Override + protected Parcelable onSaveInstanceState() { + Bundle bundle = new Bundle(); + bundle.putParcelable("instanceState", super.onSaveInstanceState()); + bundle.putInt("mCurrentTab", mCurrentTab); + return bundle; + } + + @Override + protected void onRestoreInstanceState(Parcelable state) { + if (state instanceof Bundle) { + Bundle bundle = (Bundle) state; + mCurrentTab = bundle.getInt("mCurrentTab"); + state = bundle.getParcelable("instanceState"); + if (mCurrentTab != 0 && mTabsContainer.getChildCount() > 0) { + updateTabSelection(mCurrentTab); + } + } + super.onRestoreInstanceState(state); + } + + class IndicatorPoint { + public float left; + public float right; + } + + private IndicatorPoint mCurrentP = new IndicatorPoint(); + private IndicatorPoint mLastP = new IndicatorPoint(); + + class PointEvaluator implements TypeEvaluator { + @Override + public IndicatorPoint evaluate(float fraction, IndicatorPoint startValue, IndicatorPoint endValue) { + float left = startValue.left + fraction * (endValue.left - startValue.left); + float right = startValue.right + fraction * (endValue.right - startValue.right); + IndicatorPoint point = new IndicatorPoint(); + point.left = left; + point.right = right; + return point; + } + } + + + protected int dp2px(float dp) { + final float scale = mContext.getResources().getDisplayMetrics().density; + return (int) (dp * scale + 0.5f); + } + + protected int sp2px(float sp) { + final float scale = this.mContext.getResources().getDisplayMetrics().scaledDensity; + return (int) (sp * scale + 0.5f); + } + +} diff --git a/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/SegmentTabLayout.java b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/SegmentTabLayout.java new file mode 100644 index 0000000..98ff89a --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/SegmentTabLayout.java @@ -0,0 +1,775 @@ +package com.flyco.tablayout; + +import android.animation.TypeEvaluator; +import android.animation.ValueAnimator; +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Rect; +import android.graphics.drawable.GradientDrawable; +import android.os.Bundle; +import android.os.Parcelable; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; +import android.util.AttributeSet; +import android.util.SparseArray; +import android.util.TypedValue; +import android.view.View; +import android.view.ViewGroup; +import android.view.animation.OvershootInterpolator; +import android.widget.FrameLayout; +import android.widget.LinearLayout; +import android.widget.TextView; + +import com.flyco.tablayout.listener.OnTabSelectListener; +import com.flyco.tablayout.utils.FragmentChangeManager; +import com.flyco.tablayout.utils.UnreadMsgUtils; +import com.flyco.tablayout.widget.MsgView; + +import java.util.ArrayList; + +public class SegmentTabLayout extends FrameLayout implements ValueAnimator.AnimatorUpdateListener { + private Context mContext; + private String[] mTitles; + private LinearLayout mTabsContainer; + private int mCurrentTab; + private int mLastTab; + private int mTabCount; + /** + * 用于绘制显示器 + */ + private Rect mIndicatorRect = new Rect(); + private GradientDrawable mIndicatorDrawable = new GradientDrawable(); + private GradientDrawable mRectDrawable = new GradientDrawable(); + + private Paint mDividerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + + private float mTabPadding; + private boolean mTabSpaceEqual; + private float mTabWidth; + + /** + * indicator + */ + private int mIndicatorColor; + private float mIndicatorHeight; + private float mIndicatorCornerRadius; + private float mIndicatorMarginLeft; + private float mIndicatorMarginTop; + private float mIndicatorMarginRight; + private float mIndicatorMarginBottom; + private long mIndicatorAnimDuration; + private boolean mIndicatorAnimEnable; + private boolean mIndicatorBounceEnable; + + /** + * divider + */ + private int mDividerColor; + private float mDividerWidth; + private float mDividerPadding; + + /** + * title + */ + private static final int TEXT_BOLD_NONE = 0; + private static final int TEXT_BOLD_WHEN_SELECT = 1; + private static final int TEXT_BOLD_BOTH = 2; + private float mTextsize; + private int mTextSelectColor; + private int mTextUnselectColor; + private int mTextBold; + private boolean mTextAllCaps; + + private int mBarColor; + private int mBarStrokeColor; + private float mBarStrokeWidth; + + private int mHeight; + + /** + * anim + */ + private ValueAnimator mValueAnimator; + private OvershootInterpolator mInterpolator = new OvershootInterpolator(0.8f); + + private FragmentChangeManager mFragmentChangeManager; + private float[] mRadiusArr = new float[8]; + + public SegmentTabLayout(Context context) { + this(context, null, 0); + } + + public SegmentTabLayout(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public SegmentTabLayout(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + setWillNotDraw(false);//重写onDraw方法,需要调用这个方法来清除flag + setClipChildren(false); + setClipToPadding(false); + + this.mContext = context; + mTabsContainer = new LinearLayout(context); + addView(mTabsContainer); + + obtainAttributes(context, attrs); + + //get layout_height + String height = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_height"); + + //create ViewPager + if (height.equals(ViewGroup.LayoutParams.MATCH_PARENT + "")) { + } else if (height.equals(ViewGroup.LayoutParams.WRAP_CONTENT + "")) { + } else { + int[] systemAttrs = {android.R.attr.layout_height}; + TypedArray a = context.obtainStyledAttributes(attrs, systemAttrs); + mHeight = a.getDimensionPixelSize(0, ViewGroup.LayoutParams.WRAP_CONTENT); + a.recycle(); + } + + mValueAnimator = ValueAnimator.ofObject(new PointEvaluator(), mLastP, mCurrentP); + mValueAnimator.addUpdateListener(this); + } + + private void obtainAttributes(Context context, AttributeSet attrs) { + TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SegmentTabLayout); + + mIndicatorColor = ta.getColor(R.styleable.SegmentTabLayout_tl_indicator_color, Color.parseColor("#222831")); + mIndicatorHeight = ta.getDimension(R.styleable.SegmentTabLayout_tl_indicator_height, -1); + mIndicatorCornerRadius = ta.getDimension(R.styleable.SegmentTabLayout_tl_indicator_corner_radius, -1); + mIndicatorMarginLeft = ta.getDimension(R.styleable.SegmentTabLayout_tl_indicator_margin_left, dp2px(0)); + mIndicatorMarginTop = ta.getDimension(R.styleable.SegmentTabLayout_tl_indicator_margin_top, 0); + mIndicatorMarginRight = ta.getDimension(R.styleable.SegmentTabLayout_tl_indicator_margin_right, dp2px(0)); + mIndicatorMarginBottom = ta.getDimension(R.styleable.SegmentTabLayout_tl_indicator_margin_bottom, 0); + mIndicatorAnimEnable = ta.getBoolean(R.styleable.SegmentTabLayout_tl_indicator_anim_enable, false); + mIndicatorBounceEnable = ta.getBoolean(R.styleable.SegmentTabLayout_tl_indicator_bounce_enable, true); + mIndicatorAnimDuration = ta.getInt(R.styleable.SegmentTabLayout_tl_indicator_anim_duration, -1); + + mDividerColor = ta.getColor(R.styleable.SegmentTabLayout_tl_divider_color, mIndicatorColor); + mDividerWidth = ta.getDimension(R.styleable.SegmentTabLayout_tl_divider_width, dp2px(1)); + mDividerPadding = ta.getDimension(R.styleable.SegmentTabLayout_tl_divider_padding, 0); + + mTextsize = ta.getDimension(R.styleable.SegmentTabLayout_tl_textSize, sp2px(13f)); + mTextSelectColor = ta.getColor(R.styleable.SegmentTabLayout_tl_textSelectColor, Color.parseColor("#ffffff")); + mTextUnselectColor = ta.getColor(R.styleable.SegmentTabLayout_tl_textUnSelectColor, mIndicatorColor); + mTextBold = ta.getInt(R.styleable.SegmentTabLayout_tl_textBold, TEXT_BOLD_NONE); + mTextAllCaps = ta.getBoolean(R.styleable.SegmentTabLayout_tl_textAllCaps, false); + + mTabSpaceEqual = ta.getBoolean(R.styleable.SegmentTabLayout_tl_tab_space_equal, true); + mTabWidth = ta.getDimension(R.styleable.SegmentTabLayout_tl_tab_width, dp2px(-1)); + mTabPadding = ta.getDimension(R.styleable.SegmentTabLayout_tl_tab_padding, mTabSpaceEqual || mTabWidth > 0 ? dp2px(0) : dp2px(10)); + + mBarColor = ta.getColor(R.styleable.SegmentTabLayout_tl_bar_color, Color.TRANSPARENT); + mBarStrokeColor = ta.getColor(R.styleable.SegmentTabLayout_tl_bar_stroke_color, mIndicatorColor); + mBarStrokeWidth = ta.getDimension(R.styleable.SegmentTabLayout_tl_bar_stroke_width, dp2px(1)); + + ta.recycle(); + } + + public void setTabData(String[] titles) { + if (titles == null || titles.length == 0) { + throw new IllegalStateException("Titles can not be NULL or EMPTY !"); + } + + this.mTitles = titles; + + notifyDataSetChanged(); + } + + /** + * 关联数据支持同时切换fragments + */ + public void setTabData(String[] titles, FragmentActivity fa, int containerViewId, ArrayList fragments) { + mFragmentChangeManager = new FragmentChangeManager(fa.getSupportFragmentManager(), containerViewId, fragments); + setTabData(titles); + } + + /** + * 更新数据 + */ + public void notifyDataSetChanged() { + mTabsContainer.removeAllViews(); + this.mTabCount = mTitles.length; + View tabView; + for (int i = 0; i < mTabCount; i++) { + tabView = View.inflate(mContext, R.layout.layout_tab_segment, null); + tabView.setTag(i); + addTab(i, tabView); + } + + updateTabStyles(); + } + + /** + * 创建并添加tab + */ + private void addTab(final int position, View tabView) { + TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title); + tv_tab_title.setText(mTitles[position]); + + tabView.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + int position = (Integer) v.getTag(); + if (mCurrentTab != position) { + setCurrentTab(position); + if (mListener != null) { + mListener.onTabSelect(position); + } + } else { + if (mListener != null) { + mListener.onTabReselect(position); + } + } + } + }); + + /** 每一个Tab的布局参数 */ + LinearLayout.LayoutParams lp_tab = mTabSpaceEqual ? + new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f) : + new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); + if (mTabWidth > 0) { + lp_tab = new LinearLayout.LayoutParams((int) mTabWidth, LayoutParams.MATCH_PARENT); + } + mTabsContainer.addView(tabView, position, lp_tab); + } + + private void updateTabStyles() { + for (int i = 0; i < mTabCount; i++) { + View tabView = mTabsContainer.getChildAt(i); + tabView.setPadding((int) mTabPadding, 0, (int) mTabPadding, 0); + TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title); + tv_tab_title.setTextColor(i == mCurrentTab ? mTextSelectColor : mTextUnselectColor); + tv_tab_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextsize); +// tv_tab_title.setPadding((int) mTabPadding, 0, (int) mTabPadding, 0); + if (mTextAllCaps) { + tv_tab_title.setText(tv_tab_title.getText().toString().toUpperCase()); + } + + if (mTextBold == TEXT_BOLD_BOTH) { + tv_tab_title.getPaint().setFakeBoldText(true); + } + // 被选中设置为粗体 + else if (mTextBold == TEXT_BOLD_WHEN_SELECT && i == mCurrentTab) { + tv_tab_title.getPaint().setFakeBoldText(true); + } else if (mTextBold == TEXT_BOLD_NONE) { + tv_tab_title.getPaint().setFakeBoldText(false); + } + } + } + + private void updateTabSelection(int position) { + for (int i = 0; i < mTabCount; ++i) { + View tabView = mTabsContainer.getChildAt(i); + final boolean isSelect = i == position; + TextView tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title); + tab_title.setTextColor(isSelect ? mTextSelectColor : mTextUnselectColor); + if (mTextBold == TEXT_BOLD_WHEN_SELECT) { + tab_title.getPaint().setFakeBoldText(isSelect); + } + } + } + + private void calcOffset() { + final View currentTabView = mTabsContainer.getChildAt(this.mCurrentTab); + mCurrentP.left = currentTabView.getLeft(); + mCurrentP.right = currentTabView.getRight(); + + final View lastTabView = mTabsContainer.getChildAt(this.mLastTab); + mLastP.left = lastTabView.getLeft(); + mLastP.right = lastTabView.getRight(); + +// Log.d("AAA", "mLastP--->" + mLastP.left + "&" + mLastP.right); +// Log.d("AAA", "mCurrentP--->" + mCurrentP.left + "&" + mCurrentP.right); + if (mLastP.left == mCurrentP.left && mLastP.right == mCurrentP.right) { + invalidate(); + } else { + mValueAnimator.setObjectValues(mLastP, mCurrentP); + if (mIndicatorBounceEnable) { + mValueAnimator.setInterpolator(mInterpolator); + } + + if (mIndicatorAnimDuration < 0) { + mIndicatorAnimDuration = mIndicatorBounceEnable ? 500 : 250; + } + mValueAnimator.setDuration(mIndicatorAnimDuration); + mValueAnimator.start(); + } + } + + private void calcIndicatorRect() { + View currentTabView = mTabsContainer.getChildAt(this.mCurrentTab); + float left = currentTabView.getLeft(); + float right = currentTabView.getRight(); + + mIndicatorRect.left = (int) left; + mIndicatorRect.right = (int) right; + + if (!mIndicatorAnimEnable) { + if (mCurrentTab == 0) { + /**The corners are ordered top-left, top-right, bottom-right, bottom-left*/ + mRadiusArr[0] = mIndicatorCornerRadius; + mRadiusArr[1] = mIndicatorCornerRadius; + mRadiusArr[2] = 0; + mRadiusArr[3] = 0; + mRadiusArr[4] = 0; + mRadiusArr[5] = 0; + mRadiusArr[6] = mIndicatorCornerRadius; + mRadiusArr[7] = mIndicatorCornerRadius; + } else if (mCurrentTab == mTabCount - 1) { + /**The corners are ordered top-left, top-right, bottom-right, bottom-left*/ + mRadiusArr[0] = 0; + mRadiusArr[1] = 0; + mRadiusArr[2] = mIndicatorCornerRadius; + mRadiusArr[3] = mIndicatorCornerRadius; + mRadiusArr[4] = mIndicatorCornerRadius; + mRadiusArr[5] = mIndicatorCornerRadius; + mRadiusArr[6] = 0; + mRadiusArr[7] = 0; + } else { + /**The corners are ordered top-left, top-right, bottom-right, bottom-left*/ + mRadiusArr[0] = 0; + mRadiusArr[1] = 0; + mRadiusArr[2] = 0; + mRadiusArr[3] = 0; + mRadiusArr[4] = 0; + mRadiusArr[5] = 0; + mRadiusArr[6] = 0; + mRadiusArr[7] = 0; + } + } else { + /**The corners are ordered top-left, top-right, bottom-right, bottom-left*/ + mRadiusArr[0] = mIndicatorCornerRadius; + mRadiusArr[1] = mIndicatorCornerRadius; + mRadiusArr[2] = mIndicatorCornerRadius; + mRadiusArr[3] = mIndicatorCornerRadius; + mRadiusArr[4] = mIndicatorCornerRadius; + mRadiusArr[5] = mIndicatorCornerRadius; + mRadiusArr[6] = mIndicatorCornerRadius; + mRadiusArr[7] = mIndicatorCornerRadius; + } + } + + @Override + public void onAnimationUpdate(ValueAnimator animation) { + IndicatorPoint p = (IndicatorPoint) animation.getAnimatedValue(); + mIndicatorRect.left = (int) p.left; + mIndicatorRect.right = (int) p.right; + invalidate(); + } + + private boolean mIsFirstDraw = true; + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + + if (isInEditMode() || mTabCount <= 0) { + return; + } + + int height = getHeight(); + int paddingLeft = getPaddingLeft(); + + if (mIndicatorHeight < 0) { + mIndicatorHeight = height - mIndicatorMarginTop - mIndicatorMarginBottom; + } + + if (mIndicatorCornerRadius < 0 || mIndicatorCornerRadius > mIndicatorHeight / 2) { + mIndicatorCornerRadius = mIndicatorHeight / 2; + } + + //draw rect + mRectDrawable.setColor(mBarColor); + mRectDrawable.setStroke((int) mBarStrokeWidth, mBarStrokeColor); + mRectDrawable.setCornerRadius(mIndicatorCornerRadius); + mRectDrawable.setBounds(getPaddingLeft(), getPaddingTop(), getWidth() - getPaddingRight(), getHeight() - getPaddingBottom()); + mRectDrawable.draw(canvas); + + // draw divider + if (!mIndicatorAnimEnable && mDividerWidth > 0) { + mDividerPaint.setStrokeWidth(mDividerWidth); + mDividerPaint.setColor(mDividerColor); + for (int i = 0; i < mTabCount - 1; i++) { + View tab = mTabsContainer.getChildAt(i); + canvas.drawLine(paddingLeft + tab.getRight(), mDividerPadding, paddingLeft + tab.getRight(), height - mDividerPadding, mDividerPaint); + } + } + + + //draw indicator line + if (mIndicatorAnimEnable) { + if (mIsFirstDraw) { + mIsFirstDraw = false; + calcIndicatorRect(); + } + } else { + calcIndicatorRect(); + } + + mIndicatorDrawable.setColor(mIndicatorColor); + mIndicatorDrawable.setBounds(paddingLeft + (int) mIndicatorMarginLeft + mIndicatorRect.left, + (int) mIndicatorMarginTop, (int) (paddingLeft + mIndicatorRect.right - mIndicatorMarginRight), + (int) (mIndicatorMarginTop + mIndicatorHeight)); + mIndicatorDrawable.setCornerRadii(mRadiusArr); + mIndicatorDrawable.draw(canvas); + + } + + //setter and getter + public void setCurrentTab(int currentTab) { + mLastTab = this.mCurrentTab; + this.mCurrentTab = currentTab; + updateTabSelection(currentTab); + if (mFragmentChangeManager != null) { + mFragmentChangeManager.setFragments(currentTab); + } + if (mIndicatorAnimEnable) { + calcOffset(); + } else { + invalidate(); + } + } + + public void setTabPadding(float tabPadding) { + this.mTabPadding = dp2px(tabPadding); + updateTabStyles(); + } + + public void setTabSpaceEqual(boolean tabSpaceEqual) { + this.mTabSpaceEqual = tabSpaceEqual; + updateTabStyles(); + } + + public void setTabWidth(float tabWidth) { + this.mTabWidth = dp2px(tabWidth); + updateTabStyles(); + } + + public void setIndicatorColor(int indicatorColor) { + this.mIndicatorColor = indicatorColor; + invalidate(); + } + + public void setIndicatorHeight(float indicatorHeight) { + this.mIndicatorHeight = dp2px(indicatorHeight); + invalidate(); + } + + public void setIndicatorCornerRadius(float indicatorCornerRadius) { + this.mIndicatorCornerRadius = dp2px(indicatorCornerRadius); + invalidate(); + } + + public void setIndicatorMargin(float indicatorMarginLeft, float indicatorMarginTop, + float indicatorMarginRight, float indicatorMarginBottom) { + this.mIndicatorMarginLeft = dp2px(indicatorMarginLeft); + this.mIndicatorMarginTop = dp2px(indicatorMarginTop); + this.mIndicatorMarginRight = dp2px(indicatorMarginRight); + this.mIndicatorMarginBottom = dp2px(indicatorMarginBottom); + invalidate(); + } + + public void setIndicatorAnimDuration(long indicatorAnimDuration) { + this.mIndicatorAnimDuration = indicatorAnimDuration; + } + + public void setIndicatorAnimEnable(boolean indicatorAnimEnable) { + this.mIndicatorAnimEnable = indicatorAnimEnable; + } + + public void setIndicatorBounceEnable(boolean indicatorBounceEnable) { + this.mIndicatorBounceEnable = indicatorBounceEnable; + } + + public void setDividerColor(int dividerColor) { + this.mDividerColor = dividerColor; + invalidate(); + } + + public void setDividerWidth(float dividerWidth) { + this.mDividerWidth = dp2px(dividerWidth); + invalidate(); + } + + public void setDividerPadding(float dividerPadding) { + this.mDividerPadding = dp2px(dividerPadding); + invalidate(); + } + + public void setTextsize(float textsize) { + this.mTextsize = sp2px(textsize); + updateTabStyles(); + } + + public void setTextSelectColor(int textSelectColor) { + this.mTextSelectColor = textSelectColor; + updateTabStyles(); + } + + public void setTextUnselectColor(int textUnselectColor) { + this.mTextUnselectColor = textUnselectColor; + updateTabStyles(); + } + + public void setTextBold(int textBold) { + this.mTextBold = textBold; + updateTabStyles(); + } + + public void setTextAllCaps(boolean textAllCaps) { + this.mTextAllCaps = textAllCaps; + updateTabStyles(); + } + + public int getTabCount() { + return mTabCount; + } + + public int getCurrentTab() { + return mCurrentTab; + } + + public float getTabPadding() { + return mTabPadding; + } + + public boolean isTabSpaceEqual() { + return mTabSpaceEqual; + } + + public float getTabWidth() { + return mTabWidth; + } + + public int getIndicatorColor() { + return mIndicatorColor; + } + + public float getIndicatorHeight() { + return mIndicatorHeight; + } + + public float getIndicatorCornerRadius() { + return mIndicatorCornerRadius; + } + + public float getIndicatorMarginLeft() { + return mIndicatorMarginLeft; + } + + public float getIndicatorMarginTop() { + return mIndicatorMarginTop; + } + + public float getIndicatorMarginRight() { + return mIndicatorMarginRight; + } + + public float getIndicatorMarginBottom() { + return mIndicatorMarginBottom; + } + + public long getIndicatorAnimDuration() { + return mIndicatorAnimDuration; + } + + public boolean isIndicatorAnimEnable() { + return mIndicatorAnimEnable; + } + + public boolean isIndicatorBounceEnable() { + return mIndicatorBounceEnable; + } + + public int getDividerColor() { + return mDividerColor; + } + + public float getDividerWidth() { + return mDividerWidth; + } + + public float getDividerPadding() { + return mDividerPadding; + } + + public float getTextsize() { + return mTextsize; + } + + public int getTextSelectColor() { + return mTextSelectColor; + } + + public int getTextUnselectColor() { + return mTextUnselectColor; + } + + public int getTextBold() { + return mTextBold; + } + + public boolean isTextAllCaps() { + return mTextAllCaps; + } + + public TextView getTitleView(int tab) { + View tabView = mTabsContainer.getChildAt(tab); + TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title); + return tv_tab_title; + } + + //setter and getter + // show MsgTipView + private Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + private SparseArray mInitSetMap = new SparseArray<>(); + + /** + * 显示未读消息 + * + * @param position 显示tab位置 + * @param num num小于等于0显示红点,num大于0显示数字 + */ + public void showMsg(int position, int num) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + + View tabView = mTabsContainer.getChildAt(position); + MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip); + if (tipView != null) { + UnreadMsgUtils.show(tipView, num); + + if (mInitSetMap.get(position) != null && mInitSetMap.get(position)) { + return; + } + + setMsgMargin(position, 2, 2); + + mInitSetMap.put(position, true); + } + } + + /** + * 显示未读红点 + * + * @param position 显示tab位置 + */ + public void showDot(int position) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + showMsg(position, 0); + } + + public void hideMsg(int position) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + + View tabView = mTabsContainer.getChildAt(position); + MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip); + if (tipView != null) { + tipView.setVisibility(View.GONE); + } + } + + /** + * 设置提示红点偏移,注意 + * 1.控件为固定高度:参照点为tab内容的右上角 + * 2.控件高度不固定(WRAP_CONTENT):参照点为tab内容的右上角,此时高度已是红点的最高显示范围,所以这时bottomPadding其实就是topPadding + */ + public void setMsgMargin(int position, float leftPadding, float bottomPadding) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + View tabView = mTabsContainer.getChildAt(position); + MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip); + if (tipView != null) { + TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title); + mTextPaint.setTextSize(mTextsize); + float textWidth = mTextPaint.measureText(tv_tab_title.getText().toString()); + float textHeight = mTextPaint.descent() - mTextPaint.ascent(); + MarginLayoutParams lp = (MarginLayoutParams) tipView.getLayoutParams(); + + lp.leftMargin = dp2px(leftPadding); + lp.topMargin = mHeight > 0 ? (int) (mHeight - textHeight) / 2 - dp2px(bottomPadding) : dp2px(bottomPadding); + + tipView.setLayoutParams(lp); + } + } + + /** + * 当前类只提供了少许设置未读消息属性的方法,可以通过该方法获取MsgView对象从而各种设置 + */ + public MsgView getMsgView(int position) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + View tabView = mTabsContainer.getChildAt(position); + MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip); + return tipView; + } + + private OnTabSelectListener mListener; + + public void setOnTabSelectListener(OnTabSelectListener listener) { + this.mListener = listener; + } + + @Override + protected Parcelable onSaveInstanceState() { + Bundle bundle = new Bundle(); + bundle.putParcelable("instanceState", super.onSaveInstanceState()); + bundle.putInt("mCurrentTab", mCurrentTab); + return bundle; + } + + @Override + protected void onRestoreInstanceState(Parcelable state) { + if (state instanceof Bundle) { + Bundle bundle = (Bundle) state; + mCurrentTab = bundle.getInt("mCurrentTab"); + state = bundle.getParcelable("instanceState"); + if (mCurrentTab != 0 && mTabsContainer.getChildCount() > 0) { + updateTabSelection(mCurrentTab); + } + } + super.onRestoreInstanceState(state); + } + + class IndicatorPoint { + public float left; + public float right; + } + + private IndicatorPoint mCurrentP = new IndicatorPoint(); + private IndicatorPoint mLastP = new IndicatorPoint(); + + class PointEvaluator implements TypeEvaluator { + @Override + public IndicatorPoint evaluate(float fraction, IndicatorPoint startValue, IndicatorPoint endValue) { + float left = startValue.left + fraction * (endValue.left - startValue.left); + float right = startValue.right + fraction * (endValue.right - startValue.right); + IndicatorPoint point = new IndicatorPoint(); + point.left = left; + point.right = right; + return point; + } + } + + protected int dp2px(float dp) { + final float scale = mContext.getResources().getDisplayMetrics().density; + return (int) (dp * scale + 0.5f); + } + + protected int sp2px(float sp) { + final float scale = this.mContext.getResources().getDisplayMetrics().scaledDensity; + return (int) (sp * scale + 0.5f); + } +} diff --git a/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/SlidingScaleTabLayout.java b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/SlidingScaleTabLayout.java new file mode 100644 index 0000000..dd437b7 --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/SlidingScaleTabLayout.java @@ -0,0 +1,1216 @@ +package com.flyco.tablayout; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Path; +import android.graphics.Rect; +import android.graphics.drawable.GradientDrawable; +import android.os.Bundle; +import android.os.Parcelable; +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentPagerAdapter; +import androidx.viewpager.widget.PagerAdapter; +import androidx.viewpager.widget.ViewPager; +import android.text.TextUtils; +import android.util.AttributeSet; +import android.util.SparseBooleanArray; +import android.util.TypedValue; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.HorizontalScrollView; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.RelativeLayout; +import android.widget.TextView; + +import com.flyco.tablayout.listener.OnTabSelectListener; +import com.flyco.tablayout.transformer.ExtendTransformer; +import com.flyco.tablayout.transformer.ITabScaleTransformer; +import com.flyco.tablayout.transformer.IViewPagerTransformer; +import com.flyco.tablayout.transformer.TabScaleTransformer; +import com.flyco.tablayout.utils.UnreadMsgUtils; +import com.flyco.tablayout.utils.ViewUtils; +import com.flyco.tablayout.widget.MsgView; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * 滑动切换TabLayout,tab的文字大小会发生变化 + */ +public class SlidingScaleTabLayout extends HorizontalScrollView implements ViewPager.OnPageChangeListener { + + private static final int TOP = 0; + private static final int BOTTOM = 1; + private static final int CENTER = 2; + private static final int LEFT = 0; + private static final int RIGHT = 1; + + private Context mContext; + private ViewPager mViewPager; + private ArrayList mTitles; + private LinearLayout mTabsContainer; + private int mCurrentTab; + private float mCurrentPositionOffset; + private int mTabCount; + /** + * 用于绘制显示器 + */ + private Rect mIndicatorRect = new Rect(); + /** + * 用于实现滚动居中 + */ + private Rect mTabRect = new Rect(); + private GradientDrawable mIndicatorDrawable = new GradientDrawable(); + + private Paint mRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + private Paint mDividerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + private Paint mTrianglePaint = new Paint(Paint.ANTI_ALIAS_FLAG); + private Path mTrianglePath = new Path(); + private static final int STYLE_NORMAL = 0; + private static final int STYLE_TRIANGLE = 1; + private static final int STYLE_BLOCK = 2; + private int mIndicatorStyle = STYLE_NORMAL; + + private float mTabPadding; + private boolean mTabSpaceEqual; + private float mTabWidth; + + /** + * indicator + */ + private int mIndicatorColor; + private float mIndicatorHeight; + private float mIndicatorWidth; + private float mIndicatorCornerRadius; + private float mIndicatorMarginLeft; + private float mIndicatorMarginTop; + private float mIndicatorMarginRight; + private float mIndicatorMarginBottom; + private int mIndicatorGravity; + private boolean mIndicatorWidthEqualTitle; + + /** + * underline + */ + private int mUnderlineColor; + private float mUnderlineHeight; + private int mUnderlineGravity; + + /** + * divider + */ + private int mDividerColor; + private float mDividerWidth; + private float mDividerPadding; + + /** + * title + */ + private static final int TEXT_BOLD_NONE = 0; + private static final int TEXT_BOLD_WHEN_SELECT = 1; + private static final int TEXT_BOLD_BOTH = 2; + private float mTextSelectSize; + private float mTextUnSelectSize; + private int mTextSelectColor; + private int mTextUnSelectColor; + private int mTextBold; + private boolean mTextAllCaps; + + private int mLastScrollX; + private int mHeight; + private boolean mSnapOnTabClick; + + /** + * tab的上下间距 + */ + private int mTabMarginTop; + private int mTabMarginBottom; + + private int mTabMsgMarginTop; + private int mTabMsgMarginRight; + private int mTabDotMarginTop; + private int mTabDotMarginRight; + private int mTabBackgroundId; + + private boolean openDmg = true; + + /** + * tab中的内容的位置 + */ + private int mTabHorizontalGravity; + + private int mTabVerticalGravity; + + private ITabScaleTransformer iTabScaleTransformer; + + private ExtendTransformer extendTransformer; + + public SlidingScaleTabLayout(Context context) { + this(context, null, 0); + } + + public SlidingScaleTabLayout(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public SlidingScaleTabLayout(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + setFillViewport(true);//设置滚动视图是否可以伸缩其内容以填充视口 + setWillNotDraw(false);//重写onDraw方法,需要调用这个方法来清除flag + setClipChildren(false); + setClipToPadding(false); + + this.mContext = context; + mTabsContainer = new LinearLayout(context); + addView(mTabsContainer); + + obtainAttributes(context, attrs); + + //get layout_height + String height = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_height"); + + if (height.equals(ViewGroup.LayoutParams.MATCH_PARENT + "")) { + } else if (height.equals(ViewGroup.LayoutParams.WRAP_CONTENT + "")) { + } else { + int[] systemAttrs = {android.R.attr.layout_height}; + TypedArray a = context.obtainStyledAttributes(attrs, systemAttrs); + mHeight = a.getDimensionPixelSize(0, ViewGroup.LayoutParams.WRAP_CONTENT); + a.recycle(); + } + } + + private void obtainAttributes(Context context, AttributeSet attrs) { + TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SlidingScaleTabLayout); + + mIndicatorStyle = ta.getInt(R.styleable.SlidingScaleTabLayout_tl_indicator_style, STYLE_NORMAL); + mIndicatorColor = ta.getColor(R.styleable.SlidingScaleTabLayout_tl_indicator_color, Color.parseColor(mIndicatorStyle == STYLE_BLOCK ? "#4B6A87" : "#ffffff")); + mIndicatorHeight = ta.getDimension(R.styleable.SlidingScaleTabLayout_tl_indicator_height, + dp2px(mIndicatorStyle == STYLE_TRIANGLE ? 4 : (mIndicatorStyle == STYLE_BLOCK ? -1 : 2))); + mIndicatorWidth = ta.getDimension(R.styleable.SlidingScaleTabLayout_tl_indicator_width, dp2px(mIndicatorStyle == STYLE_TRIANGLE ? 10 : -1)); + mIndicatorCornerRadius = ta.getDimension(R.styleable.SlidingScaleTabLayout_tl_indicator_corner_radius, dp2px(mIndicatorStyle == STYLE_BLOCK ? -1 : 0)); + mIndicatorMarginLeft = ta.getDimension(R.styleable.SlidingScaleTabLayout_tl_indicator_margin_left, dp2px(0)); + mIndicatorMarginTop = ta.getDimension(R.styleable.SlidingScaleTabLayout_tl_indicator_margin_top, dp2px(mIndicatorStyle == STYLE_BLOCK ? 7 : 0)); + mIndicatorMarginRight = ta.getDimension(R.styleable.SlidingScaleTabLayout_tl_indicator_margin_right, dp2px(0)); + mIndicatorMarginBottom = ta.getDimension(R.styleable.SlidingScaleTabLayout_tl_indicator_margin_bottom, dp2px(mIndicatorStyle == STYLE_BLOCK ? 7 : 0)); + mIndicatorGravity = ta.getInt(R.styleable.SlidingScaleTabLayout_tl_indicator_gravity, Gravity.BOTTOM); + mIndicatorWidthEqualTitle = ta.getBoolean(R.styleable.SlidingScaleTabLayout_tl_indicator_width_equal_title, false); + + mUnderlineColor = ta.getColor(R.styleable.SlidingScaleTabLayout_tl_underline_color, Color.parseColor("#ffffff")); + mUnderlineHeight = ta.getDimension(R.styleable.SlidingScaleTabLayout_tl_underline_height, dp2px(0)); + mUnderlineGravity = ta.getInt(R.styleable.SlidingScaleTabLayout_tl_underline_gravity, Gravity.BOTTOM); + + mDividerColor = ta.getColor(R.styleable.SlidingScaleTabLayout_tl_divider_color, Color.parseColor("#ffffff")); + mDividerWidth = ta.getDimension(R.styleable.SlidingScaleTabLayout_tl_divider_width, dp2px(0)); + mDividerPadding = ta.getDimension(R.styleable.SlidingScaleTabLayout_tl_divider_padding, dp2px(12)); + + mTextUnSelectSize = ta.getDimension(R.styleable.SlidingScaleTabLayout_tl_textUnSelectSize, sp2px(14)); + // 被选中的文字大小,默认额未选中的大小一样 + mTextSelectSize = ta.getDimension(R.styleable.SlidingScaleTabLayout_tl_textSelectSize, mTextUnSelectSize); + + mTextSelectColor = ta.getColor(R.styleable.SlidingScaleTabLayout_tl_textSelectColor, Color.parseColor("#ffffff")); + mTextUnSelectColor = ta.getColor(R.styleable.SlidingScaleTabLayout_tl_textUnSelectColor, Color.parseColor("#AAffffff")); + mTextBold = ta.getInt(R.styleable.SlidingScaleTabLayout_tl_textBold, TEXT_BOLD_NONE); + mTextAllCaps = ta.getBoolean(R.styleable.SlidingScaleTabLayout_tl_textAllCaps, false); + + mTabSpaceEqual = ta.getBoolean(R.styleable.SlidingScaleTabLayout_tl_tab_space_equal, false); + mTabWidth = ta.getDimension(R.styleable.SlidingScaleTabLayout_tl_tab_width, dp2px(-1)); + mTabPadding = ta.getDimension(R.styleable.SlidingScaleTabLayout_tl_tab_padding, mTabSpaceEqual || mTabWidth > 0 ? dp2px(0) : dp2px(20)); + // 得到设置的上下间距和gravity + mTabMarginTop = ta.getDimensionPixelSize(R.styleable.SlidingScaleTabLayout_tl_tab_marginTop, 0); + mTabMarginBottom = ta.getDimensionPixelSize(R.styleable.SlidingScaleTabLayout_tl_tab_marginBottom, 0); + + mTabHorizontalGravity = ta.getInt(R.styleable.SlidingScaleTabLayout_tl_tab_horizontal_gravity, CENTER); + mTabVerticalGravity = ta.getInt(R.styleable.SlidingScaleTabLayout_tl_tab_vertical_gravity, CENTER); + + mTabMsgMarginTop = (int) ta.getDimension(R.styleable.SlidingScaleTabLayout_tl_tab_msg_marginTop, 0f); + mTabMsgMarginRight = (int) ta.getDimension(R.styleable.SlidingScaleTabLayout_tl_tab_msg_marginRight, 0f); + + mTabDotMarginTop = (int) ta.getDimension(R.styleable.SlidingScaleTabLayout_tl_tab_dot_marginTop, 0f); + mTabDotMarginRight = (int) ta.getDimension(R.styleable.SlidingScaleTabLayout_tl_tab_dot_marginRight, 0f); + mTabBackgroundId = ta.getResourceId(R.styleable.SlidingScaleTabLayout_tl_tab_background, 0); + openDmg = ta.getBoolean(R.styleable.SlidingScaleTabLayout_tl_openTextDmg, false); + ta.recycle(); + + iTabScaleTransformer = new TabScaleTransformer(this, mTextSelectSize, mTextUnSelectSize, openDmg); + } + + /** + * 关联ViewPager + */ + public void setViewPager(ViewPager vp) { + if (vp == null || vp.getAdapter() == null) { + throw new IllegalStateException("ViewPager or ViewPager adapter can not be NULL !"); + } + + this.mViewPager = vp; + + initViewPagerListener(); + } + + /** + * 设置标题,不关联ViewPager + */ + public void setTitle(String[] titles) { + mTitles = new ArrayList<>(); + Collections.addAll(mTitles, titles); + initViewPagerListener(); + } + + + /** + * 关联ViewPager,用于不想在ViewPager适配器中设置titles数据的情况 + */ + public void setViewPager(ViewPager vp, String[] titles) { + if (vp == null || vp.getAdapter() == null) { + throw new IllegalStateException("ViewPager or ViewPager adapter can not be NULL !"); + } + + if (titles == null || titles.length == 0) { + throw new IllegalStateException("Titles can not be EMPTY !"); + } + + if (titles.length != vp.getAdapter().getCount()) { + throw new IllegalStateException("Titles length must be the same as the page count !"); + } + + this.mViewPager = vp; + mTitles = new ArrayList<>(); + Collections.addAll(mTitles, titles); + + initViewPagerListener(); + } + + /** + * 关联ViewPager,用于连适配器都不想自己实例化的情况 + */ + public void setViewPager(ViewPager vp, String[] titles, FragmentActivity fa, ArrayList fragments) { + if (vp == null) { + throw new IllegalStateException("ViewPager can not be NULL !"); + } + + if (titles == null || titles.length == 0) { + throw new IllegalStateException("Titles can not be EMPTY !"); + } + + this.mViewPager = vp; + this.mViewPager.setAdapter(new InnerPagerAdapter(fa.getSupportFragmentManager(), fragments, titles)); + initViewPagerListener(); + } + + private void initViewPagerListener() { + if (this.mViewPager != null) { + this.mViewPager.removeOnPageChangeListener(this); + this.mViewPager.addOnPageChangeListener(this); + initTransformer(); + } + notifyDataSetChanged(); + } + + private void initTransformer() { + // 如果选中状态的文字大小和未选中状态的文字大小是不同的,开启缩放 + if (mTextUnSelectSize != mTextSelectSize) { + extendTransformer = new ExtendTransformer(); + this.mViewPager.setPageTransformer(true, extendTransformer); + } + } + + /** + * 更新数据 + */ + public void notifyDataSetChanged() { + mTabsContainer.removeAllViews(); + this.mTabCount = mTitles == null ? mViewPager.getAdapter().getCount() : mTitles.size(); + View tabView; + for (int i = 0; i < mTabCount; i++) { + tabView = LayoutInflater.from(mContext).inflate(R.layout.layout_scale_tab, mTabsContainer, false); + TextView title = tabView.findViewById(R.id.tv_tab_title); + // 设置tab的位置信息 + setTabLayoutParams(title); + CharSequence pageTitle = mTitles == null ? mViewPager.getAdapter().getPageTitle(i) : mTitles.get(i); + addTab(i, pageTitle.toString(), tabView); + } + + updateTabStyles(); + } + + private void setTabLayoutParams(TextView title) { + RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) title.getLayoutParams(); + params.topMargin = mTabMarginTop; + params.bottomMargin = mTabMarginBottom; + + if (mTabVerticalGravity == TOP) { + params.addRule(RelativeLayout.ALIGN_PARENT_TOP); + } else if (mTabVerticalGravity == BOTTOM) { + params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); + } else { + params.addRule(RelativeLayout.CENTER_VERTICAL); + } + + if (mTabHorizontalGravity == LEFT) { + params.addRule(RelativeLayout.ALIGN_PARENT_LEFT); + } else if (mTabHorizontalGravity == RIGHT) { + params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); + } else { + params.addRule(RelativeLayout.CENTER_HORIZONTAL); + } + + + title.setLayoutParams(params); + + if (isDmgOpen()) { + ImageView imageView = (ImageView) ViewUtils.findBrotherView(title, R.id.tv_tab_title_dmg, 3); + if (imageView == null) return; + params = (RelativeLayout.LayoutParams) imageView.getLayoutParams(); + params.topMargin = mTabMarginTop; + params.bottomMargin = mTabMarginBottom; + // 调整镜像的问题 + if (mTabVerticalGravity == TOP) { + params.addRule(RelativeLayout.ALIGN_PARENT_TOP); + imageView.setScaleType(ImageView.ScaleType.FIT_START); + } else if (mTabVerticalGravity == BOTTOM) { + params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); + imageView.setScaleType(ImageView.ScaleType.FIT_END); + } else { + params.addRule(RelativeLayout.CENTER_VERTICAL); + imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); + } + + if (mTabHorizontalGravity == LEFT) { + params.addRule(RelativeLayout.ALIGN_PARENT_LEFT); + } else if (mTabHorizontalGravity == RIGHT) { + params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); + } else { + params.addRule(RelativeLayout.CENTER_HORIZONTAL); + } + + imageView.setLayoutParams(params); + } + + } + + /** + * 如果文字的大小没有变化,不需要开启镜像,请注意 + */ + private boolean isDmgOpen() { + return openDmg && mTextSelectSize != mTextUnSelectSize; + } + + /** + * 创建并添加tab + */ + private void addTab(final int position, String title, View tabView) { + TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title); + if (tv_tab_title != null) { +// tv_tab_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, position == mCurrentTab ? mTextSelectSize : mTextUnSelectSize); + tv_tab_title.setText(title); + // 设置tab背景 + if (mTabBackgroundId != 0){ + tv_tab_title.setBackgroundResource(mTabBackgroundId); + } +// if (TextUtils.isEmpty(title)) { +// tabView.setVisibility(View.GONE); +// } else { +// tabView.setVisibility(View.VISIBLE); +// } + } + + tabView.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + int position = mTabsContainer.indexOfChild(v); + if (position != -1) { + setCurrentTab(position); + } + } + }); + + /** 每一个Tab的布局参数 */ + LinearLayout.LayoutParams lp_tab = mTabSpaceEqual ? + new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f) : + new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); + if (mTabWidth > 0) { + lp_tab = new LinearLayout.LayoutParams((int) mTabWidth, LayoutParams.MATCH_PARENT); + } + mTabsContainer.addView(tabView, position, lp_tab); + } + + private void updateTabStyles() { + for (int i = 0; i < mTabCount; i++) { + View v = mTabsContainer.getChildAt(i); +// v.setPadding((int) mTabPadding, v.getPaddingTop(), (int) mTabPadding, v.getPaddingBottom()); + TextView tv_tab_title = (TextView) v.findViewById(R.id.tv_tab_title); + if (tv_tab_title != null) { + v.setPadding((int) mTabPadding, 0, (int) mTabPadding, 0); + tv_tab_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, i == mCurrentTab ? mTextSelectSize : mTextUnSelectSize); + tv_tab_title.setTextColor(i == mCurrentTab ? mTextSelectColor : mTextUnSelectColor); + // 设置选中状态 + tv_tab_title.setSelected(i == mCurrentTab); + + if (mTextAllCaps) { + tv_tab_title.setText(tv_tab_title.getText().toString().toUpperCase()); + } + + if (mTextBold == TEXT_BOLD_BOTH) { + tv_tab_title.getPaint().setFakeBoldText(true); + } + // 被选中设置为粗体 + else if (mTextBold == TEXT_BOLD_WHEN_SELECT) { + tv_tab_title.getPaint().setFakeBoldText(i == mCurrentTab); + } else if (mTextBold == TEXT_BOLD_NONE) { + tv_tab_title.getPaint().setFakeBoldText(false); + } + + if (isDmgOpen()) { + generateTitleDmg(v, tv_tab_title, i); + } + } + } + + } + + private void generateTitleDmg(View tabView, TextView textView, int position) { + // 空字符串不能做镜像,否则会引发空指针 + if (TextUtils.isEmpty(textView.getText())) { + return; + } + + // 如果需要开启镜像,需要把所有的字设置为选中的字体 +// textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextUnSelectSize); +// ImageView imageView = tabView.findViewById(R.id.tv_tab_title_dmg); +// imageView.setImageBitmap(ViewUtils.generateViewCacheBitmap(textView)); +// imageView.setMaxWidth(imageView.getDrawable().getIntrinsicWidth()); + + + ImageView imageView = tabView.findViewById(R.id.tv_tab_title_dmg); + // 如果需要开启镜像,需要把所有的字设置为选中的字体 + if (mTextSelectSize >= mTextUnSelectSize){ + textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSelectSize); + imageView.setImageBitmap(ViewUtils.generateViewCacheBitmap(textView)); + int drawableWidth = imageView.getDrawable().getIntrinsicWidth(); + imageView.setMinimumWidth((int) (drawableWidth * mTextUnSelectSize / mTextSelectSize)); + imageView.setMaxWidth(drawableWidth); + } + else{ + textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextUnSelectSize); + imageView.setImageBitmap(ViewUtils.generateViewCacheBitmap(textView)); + int drawableWidth = imageView.getDrawable().getIntrinsicWidth(); + imageView.setMinimumWidth((int) (drawableWidth * mTextSelectSize / mTextUnSelectSize)); + imageView.setMaxWidth(drawableWidth); + } + +// iTabScaleTransformer.setNormalWidth(position, imageView.getDrawable().getIntrinsicWidth(), position == mViewPager.getCurrentItem()); + textView.setVisibility(View.GONE); + } + + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + /** + * position:当前View的位置 + * mCurrentPositionOffset:当前View的偏移量比例.[0,1) + */ + this.mCurrentTab = position; + this.mCurrentPositionOffset = positionOffset; + iTabScaleTransformer.onPageScrolled(position, positionOffset, positionOffsetPixels); + scrollToCurrentTab(); + invalidate(); +// Log.i("onPageScrolled", "mCurrentTab:" + mCurrentTab + " positionOffset:" + positionOffset); + if (this.mCurrentPositionOffset == 0) { + updateTabSelection(mCurrentTab); + } + } + + @Override + public void onPageSelected(int position) { + + } + + @Override + public void onPageScrollStateChanged(int state) { +// if (state == ViewPager.SCROLL_STATE_IDLE) { +// updateTabSelection(mCurrentTab); +// } + } + + /** + * HorizontalScrollView滚到当前tab,并且居中显示 + */ + private void scrollToCurrentTab() { + if (mTabCount <= 0) { + return; + } + + int offset = (int) (mCurrentPositionOffset * mTabsContainer.getChildAt(mCurrentTab).getWidth()); + /**当前Tab的left+当前Tab的Width乘以positionOffset*/ + int newScrollX = mTabsContainer.getChildAt(mCurrentTab).getLeft() + offset; + + if (mCurrentTab > 0 || offset > 0) { + /**HorizontalScrollView移动到当前tab,并居中*/ + newScrollX -= getWidth() / 2 - getPaddingLeft(); + calcIndicatorRect(); + newScrollX += ((mTabRect.right - mTabRect.left) / 2); + } + + if (newScrollX != mLastScrollX) { + mLastScrollX = newScrollX; + /** scrollTo(int x,int y):x,y代表的不是坐标点,而是偏移量 + * x:表示离起始位置的x水平方向的偏移量 + * y:表示离起始位置的y垂直方向的偏移量 + */ + scrollTo(newScrollX, 0); + } + } + + private void updateTabSelection(int position) { + for (int i = 0; i < mTabCount; ++i) { + View tabView = mTabsContainer.getChildAt(i); + final boolean isSelect = i == position; + final TextView tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title); + + if (tab_title != null) { + tab_title.setTextColor(isSelect ? mTextSelectColor : mTextUnSelectColor); + // 设置选中状态 + tab_title.setSelected(isSelect); + + if (mTextBold == TEXT_BOLD_BOTH) { + tab_title.getPaint().setFakeBoldText(true); + } + // 被选中设置为粗体 + else if (mTextBold == TEXT_BOLD_WHEN_SELECT && i == position) { + tab_title.getPaint().setFakeBoldText(true); + } else { + tab_title.getPaint().setFakeBoldText(false); + } + if (isDmgOpen() && (mTextSelectColor != mTextUnSelectColor || mTextBold == TEXT_BOLD_WHEN_SELECT)) { + tab_title.setVisibility(View.VISIBLE); + generateTitleDmg(tabView, tab_title, i); + } else { + final int finalI = i; + tab_title.post(new Runnable() { + @Override + public void run() { + tab_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, finalI == mCurrentTab ? mTextSelectSize : mTextUnSelectSize); + tab_title.requestLayout(); + } + }); + } + } + } + } + + private float margin; + + private void calcIndicatorRect() { + View currentTabView = mTabsContainer.getChildAt(this.mCurrentTab); + float left = currentTabView.getLeft(); + float right = currentTabView.getRight(); + + //for mIndicatorWidthEqualTitle + if (mIndicatorStyle == STYLE_NORMAL && mIndicatorWidthEqualTitle) { + TextView tab_title = currentTabView.findViewById(R.id.tv_tab_title); + float textWidth = mTextPaint.measureText(tab_title.getText().toString()); + margin = (right - left - textWidth) / 2; + } + + if (this.mCurrentTab < mTabCount - 1) { + View nextTabView = mTabsContainer.getChildAt(this.mCurrentTab + 1); + float nextTabLeft = nextTabView.getLeft(); + float nextTabRight = nextTabView.getRight(); + + left = left + mCurrentPositionOffset * (nextTabLeft - left); + right = right + mCurrentPositionOffset * (nextTabRight - right); + + //for mIndicatorWidthEqualTitle + if (mIndicatorStyle == STYLE_NORMAL && mIndicatorWidthEqualTitle) { + TextView next_tab_title = nextTabView.findViewById(R.id.tv_tab_title); + float nextTextWidth = mTextPaint.measureText(next_tab_title.getText().toString()); + float nextMargin = (nextTabRight - nextTabLeft - nextTextWidth) / 2; + margin = margin + mCurrentPositionOffset * (nextMargin - margin); + } + } + + mIndicatorRect.left = (int) left; + mIndicatorRect.right = (int) right; + //for mIndicatorWidthEqualTitle + if (mIndicatorStyle == STYLE_NORMAL && mIndicatorWidthEqualTitle) { + mIndicatorRect.left = (int) (left + margin - 1); + mIndicatorRect.right = (int) (right - margin - 1); + } + + mTabRect.left = (int) left; + mTabRect.right = (int) right; + + if (mIndicatorWidth < 0) { //indicatorWidth小于0时,原jpardogo's PagerSlidingTabStrip + + } else {//indicatorWidth大于0时,圆角矩形以及三角形 + float indicatorLeft = currentTabView.getLeft() + (currentTabView.getWidth() - mIndicatorWidth) / 2; + + if (this.mCurrentTab < mTabCount - 1) { + View nextTab = mTabsContainer.getChildAt(this.mCurrentTab + 1); + indicatorLeft = indicatorLeft + mCurrentPositionOffset * (currentTabView.getWidth() / 2 + nextTab.getWidth() / 2); + } + + mIndicatorRect.left = (int) indicatorLeft; + mIndicatorRect.right = (int) (mIndicatorRect.left + mIndicatorWidth); + } + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + + if (isInEditMode() || mTabCount <= 0) { + return; + } + + int height = getHeight(); + int paddingLeft = getPaddingLeft(); + // draw divider + if (mDividerWidth > 0) { + mDividerPaint.setStrokeWidth(mDividerWidth); + mDividerPaint.setColor(mDividerColor); + for (int i = 0; i < mTabCount - 1; i++) { + View tab = mTabsContainer.getChildAt(i); + canvas.drawLine(paddingLeft + tab.getRight(), mDividerPadding, paddingLeft + tab.getRight(), height - mDividerPadding, mDividerPaint); + } + } + + // draw underline + if (mUnderlineHeight > 0) { + mRectPaint.setColor(mUnderlineColor); + if (mUnderlineGravity == Gravity.BOTTOM) { + canvas.drawRect(paddingLeft, height - mUnderlineHeight, mTabsContainer.getWidth() + paddingLeft, height, mRectPaint); + } else { + canvas.drawRect(paddingLeft, 0, mTabsContainer.getWidth() + paddingLeft, mUnderlineHeight, mRectPaint); + } + } + + //draw indicator line + + calcIndicatorRect(); + if (mIndicatorStyle == STYLE_TRIANGLE) { + if (mIndicatorHeight > 0) { + mTrianglePaint.setColor(mIndicatorColor); + mTrianglePath.reset(); + mTrianglePath.moveTo(paddingLeft + mIndicatorRect.left, height); + mTrianglePath.lineTo(paddingLeft + mIndicatorRect.left / 2 + mIndicatorRect.right / 2, height - mIndicatorHeight); + mTrianglePath.lineTo(paddingLeft + mIndicatorRect.right, height); + mTrianglePath.close(); + canvas.drawPath(mTrianglePath, mTrianglePaint); + } + } else if (mIndicatorStyle == STYLE_BLOCK) { + if (mIndicatorHeight < 0) { + mIndicatorHeight = height - mIndicatorMarginTop - mIndicatorMarginBottom; + } else { + + } + + if (mIndicatorHeight > 0) { + if (mIndicatorCornerRadius < 0 || mIndicatorCornerRadius > mIndicatorHeight / 2) { + mIndicatorCornerRadius = mIndicatorHeight / 2; + } + + mIndicatorDrawable.setColor(mIndicatorColor); + mIndicatorDrawable.setBounds(paddingLeft + (int) mIndicatorMarginLeft + mIndicatorRect.left, + (int) mIndicatorMarginTop, (int) (paddingLeft + mIndicatorRect.right - mIndicatorMarginRight), + (int) (mIndicatorMarginTop + mIndicatorHeight)); + mIndicatorDrawable.setCornerRadius(mIndicatorCornerRadius); + mIndicatorDrawable.draw(canvas); + } + } else { + /* mRectPaint.setColor(mIndicatorColor); + calcIndicatorRect(); + canvas.drawRect(getPaddingLeft() + mIndicatorRect.left, getHeight() - mIndicatorHeight, + mIndicatorRect.right + getPaddingLeft(), getHeight(), mRectPaint);*/ + + if (mIndicatorHeight > 0) { + mIndicatorDrawable.setColor(mIndicatorColor); + + if (mIndicatorGravity == Gravity.BOTTOM) { + mIndicatorDrawable.setBounds(paddingLeft + (int) mIndicatorMarginLeft + mIndicatorRect.left, + height - (int) mIndicatorHeight - (int) mIndicatorMarginBottom, + paddingLeft + mIndicatorRect.right - (int) mIndicatorMarginRight, + height - (int) mIndicatorMarginBottom); + } else { + mIndicatorDrawable.setBounds(paddingLeft + (int) mIndicatorMarginLeft + mIndicatorRect.left, + (int) mIndicatorMarginTop, + paddingLeft + mIndicatorRect.right - (int) mIndicatorMarginRight, + (int) mIndicatorHeight + (int) mIndicatorMarginTop); + } + mIndicatorDrawable.setCornerRadius(mIndicatorCornerRadius); + mIndicatorDrawable.draw(canvas); + } + } + } + + //setter and getter + public void setCurrentTab(int currentTab) { + setCurrentTab(currentTab, !mSnapOnTabClick); + } + + public void setCurrentTab(int currentTab, boolean smoothScroll) { + if (mCurrentTab != currentTab) { + this.mCurrentTab = currentTab; + if (mViewPager != null) { + mViewPager.setCurrentItem(currentTab, smoothScroll); + } + + if (mListener != null) { + mListener.onTabSelect(currentTab); + } + } else { + if (mListener != null) { + mListener.onTabReselect(currentTab); + } + } + } + + public void setIndicatorStyle(int indicatorStyle) { + this.mIndicatorStyle = indicatorStyle; + invalidate(); + } + + public void setTabPadding(float tabPadding) { + this.mTabPadding = dp2px(tabPadding); + updateTabStyles(); + } + + public void setTabSpaceEqual(boolean tabSpaceEqual) { + this.mTabSpaceEqual = tabSpaceEqual; + updateTabStyles(); + } + + public void setTabWidth(float tabWidth) { + this.mTabWidth = dp2px(tabWidth); + updateTabStyles(); + } + + public void setIndicatorColor(int indicatorColor) { + this.mIndicatorColor = indicatorColor; + invalidate(); + } + + public void setIndicatorHeight(float indicatorHeight) { + this.mIndicatorHeight = dp2px(indicatorHeight); + invalidate(); + } + + public void setIndicatorWidth(float indicatorWidth) { + this.mIndicatorWidth = dp2px(indicatorWidth); + invalidate(); + } + + public void setIndicatorCornerRadius(float indicatorCornerRadius) { + this.mIndicatorCornerRadius = dp2px(indicatorCornerRadius); + invalidate(); + } + + public void setIndicatorGravity(int indicatorGravity) { + this.mIndicatorGravity = indicatorGravity; + invalidate(); + } + + public void setIndicatorMargin(float indicatorMarginLeft, float indicatorMarginTop, + float indicatorMarginRight, float indicatorMarginBottom) { + this.mIndicatorMarginLeft = dp2px(indicatorMarginLeft); + this.mIndicatorMarginTop = dp2px(indicatorMarginTop); + this.mIndicatorMarginRight = dp2px(indicatorMarginRight); + this.mIndicatorMarginBottom = dp2px(indicatorMarginBottom); + invalidate(); + } + + public void setIndicatorWidthEqualTitle(boolean indicatorWidthEqualTitle) { + this.mIndicatorWidthEqualTitle = indicatorWidthEqualTitle; + invalidate(); + } + + public void setUnderlineColor(int underlineColor) { + this.mUnderlineColor = underlineColor; + invalidate(); + } + + public void setUnderlineHeight(float underlineHeight) { + this.mUnderlineHeight = dp2px(underlineHeight); + invalidate(); + } + + public void setUnderlineGravity(int underlineGravity) { + this.mUnderlineGravity = underlineGravity; + invalidate(); + } + + public void setDividerColor(int dividerColor) { + this.mDividerColor = dividerColor; + invalidate(); + } + + public void setDividerWidth(float dividerWidth) { + this.mDividerWidth = dp2px(dividerWidth); + invalidate(); + } + + public void setDividerPadding(float dividerPadding) { + this.mDividerPadding = dp2px(dividerPadding); + invalidate(); + } + + public void setTextSelectsize(float textsize) { + this.mTextSelectSize = sp2px(textsize); + initTransformer(); + updateTabStyles(); + } + + public void setTextUnselectSize(int textSize) { + this.mTextUnSelectSize = textSize; + initTransformer(); + updateTabStyles(); + } + + public void setTextSelectColor(int textSelectColor) { + this.mTextSelectColor = textSelectColor; + updateTabStyles(); + } + + public void setTextUnselectColor(int textUnselectColor) { + this.mTextUnSelectColor = textUnselectColor; + updateTabStyles(); + } + + public void setTextBold(int textBold) { + this.mTextBold = textBold; + updateTabStyles(); + } + + public void setTextAllCaps(boolean textAllCaps) { + this.mTextAllCaps = textAllCaps; + updateTabStyles(); + } + + public void setSnapOnTabClick(boolean snapOnTabClick) { + mSnapOnTabClick = snapOnTabClick; + } + + + public int getTabCount() { + return mTabCount; + } + + public int getCurrentTab() { + return mCurrentTab; + } + + public int getIndicatorStyle() { + return mIndicatorStyle; + } + + public float getTabPadding() { + return mTabPadding; + } + + public boolean isTabSpaceEqual() { + return mTabSpaceEqual; + } + + public float getTabWidth() { + return mTabWidth; + } + + public int getIndicatorColor() { + return mIndicatorColor; + } + + public float getIndicatorHeight() { + return mIndicatorHeight; + } + + public float getIndicatorWidth() { + return mIndicatorWidth; + } + + public float getIndicatorCornerRadius() { + return mIndicatorCornerRadius; + } + + public float getIndicatorMarginLeft() { + return mIndicatorMarginLeft; + } + + public float getIndicatorMarginTop() { + return mIndicatorMarginTop; + } + + public float getIndicatorMarginRight() { + return mIndicatorMarginRight; + } + + public float getIndicatorMarginBottom() { + return mIndicatorMarginBottom; + } + + public int getUnderlineColor() { + return mUnderlineColor; + } + + public float getUnderlineHeight() { + return mUnderlineHeight; + } + + public int getDividerColor() { + return mDividerColor; + } + + public float getDividerWidth() { + return mDividerWidth; + } + + public float getDividerPadding() { + return mDividerPadding; + } + + public float getTextSelectSize() { + return mTextSelectSize; + } + + public float getTextUnselectSize() { + return mTextUnSelectSize; + } + + public int getTextSelectColor() { + return mTextSelectColor; + } + + public int getTextUnselectColor() { + return mTextUnSelectColor; + } + + public int getTextBold() { + return mTextBold; + } + + public boolean isTextAllCaps() { + return mTextAllCaps; + } + + public void addViewPagerTransformer(IViewPagerTransformer transformer) { + this.extendTransformer.addViewPagerTransformer(transformer); + } + + public void removeViewPagerTransformer(IViewPagerTransformer transformer) { + this.extendTransformer.removeViewPagerTransformer(transformer); + } + + public List getTransformers() { + return extendTransformer.getTransformers(); + } + + public void setTransformers(List transformers) { + this.extendTransformer.setTransformers(transformers); + } + + public TextView getTitleView(int tab) { + View tabView = mTabsContainer.getChildAt(tab); + return (TextView) tabView.findViewById(R.id.tv_tab_title); + } + + //setter and getter + + // show MsgTipView + private Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + private SparseBooleanArray mInitSetMap = new SparseBooleanArray(); + + /** + * 显示未读消息 + * + * @param position 显示tab位置 + * @param num num小于等于0显示红点,num大于0显示数字 + */ + public void showMsg(int position, int num) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + + View tabView = mTabsContainer.getChildAt(position); + MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip); + if (tipView != null) { + UnreadMsgUtils.show(tipView, num); + + RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tipView.getLayoutParams(); + if (openDmg) { + params.addRule(RelativeLayout.ALIGN_END, R.id.tv_tab_title_dmg); + params.addRule(RelativeLayout.ALIGN_TOP, R.id.tv_tab_title_dmg); + } else { + params.addRule(RelativeLayout.ALIGN_END, R.id.tv_tab_title); + params.addRule(RelativeLayout.ALIGN_TOP, R.id.tv_tab_title); + } + + // 红点的位置 + if (num <= 0) { + params.topMargin = mTabDotMarginTop; + params.rightMargin = mTabDotMarginRight; + } + // 未读数的位置 + else { + params.topMargin = mTabMsgMarginTop; + params.rightMargin = mTabMsgMarginRight; + } + + tipView.setLayoutParams(params); + if (mInitSetMap.get(position)) { + return; + } + mInitSetMap.put(position, true); + } + } + + /** + * 显示未读红点 + * + * @param position 显示tab位置 + */ + public void showDot(int position) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + showMsg(position, 0); + } + + /** + * 隐藏未读消息 + */ + public void hideMsg(int position) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + + View tabView = mTabsContainer.getChildAt(position); + MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip); + if (tipView != null) { + tipView.setVisibility(View.GONE); + } + } + + /** + * 当前类只提供了少许设置未读消息属性的方法,可以通过该方法获取MsgView对象从而各种设置 + */ + public MsgView getMsgView(int position) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + View tabView = mTabsContainer.getChildAt(position); + return (MsgView) tabView.findViewById(R.id.rtv_msg_tip); + } + + /** + * 当前类只提供了少许设置未读消息属性的方法,可以通过该方法获取MsgView对象从而各种设置 + */ + public TextView getTitle(int position) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + View tabView = mTabsContainer.getChildAt(position); + if (tabView == null) { + return null; + } + return (TextView) tabView.findViewById(R.id.tv_tab_title); + } + + public ImageView getDmgView(int position) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + View tabView = mTabsContainer.getChildAt(position); + if (tabView == null) { + return null; + } + +// if (tabView.getVisibility() != View.GONE) { +// return null; +// } + + return (ImageView) tabView.findViewById(R.id.tv_tab_title_dmg); + } + + private OnTabSelectListener mListener; + + public void setOnTabSelectListener(OnTabSelectListener listener) { + this.mListener = listener; + } + + class InnerPagerAdapter extends FragmentPagerAdapter { + private ArrayList fragments; + private String[] titles; + + public InnerPagerAdapter(FragmentManager fm, ArrayList fragments, String[] titles) { + super(fm); + this.fragments = fragments; + this.titles = titles; + } + + @Override + public int getCount() { + return fragments.size(); + } + + @Override + public CharSequence getPageTitle(int position) { + return titles[position]; + } + + @Override + public Fragment getItem(int position) { + return fragments.get(position); + } + + @Override + public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { + // 覆写destroyItem并且空实现,这样每个Fragment中的视图就不会被销毁 + // super.destroyItem(container, position, object); + } + + @Override + public int getItemPosition(@NonNull Object object) { + return PagerAdapter.POSITION_NONE; + } + } + + @Override + protected Parcelable onSaveInstanceState() { + Bundle bundle = new Bundle(); + bundle.putParcelable("instanceState", super.onSaveInstanceState()); + bundle.putInt("mCurrentTab", mCurrentTab); + return bundle; + } + + @Override + protected void onRestoreInstanceState(Parcelable state) { + if (state instanceof Bundle) { + Bundle bundle = (Bundle) state; + mCurrentTab = bundle.getInt("mCurrentTab"); + state = bundle.getParcelable("instanceState"); + if (mCurrentTab != 0 && mTabsContainer.getChildCount() > 0) { + updateTabSelection(mCurrentTab); + scrollToCurrentTab(); + } + } + super.onRestoreInstanceState(state); + } + + protected int dp2px(float dp) { + final float scale = mContext.getResources().getDisplayMetrics().density; + return (int) (dp * scale + 0.5f); + } + + protected int sp2px(float sp) { + final float scale = this.mContext.getResources().getDisplayMetrics().scaledDensity; + return (int) (sp * scale + 0.5f); + } +} diff --git a/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java new file mode 100644 index 0000000..d1f9330 --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java @@ -0,0 +1,1011 @@ +package com.flyco.tablayout; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Path; +import android.graphics.Rect; +import android.graphics.drawable.GradientDrawable; +import android.os.Bundle; +import android.os.Parcelable; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentPagerAdapter; +import androidx.viewpager.widget.PagerAdapter; +import androidx.viewpager.widget.ViewPager; +import android.util.AttributeSet; +import android.util.SparseBooleanArray; +import android.util.TypedValue; +import android.view.Gravity; +import android.view.View; +import android.view.ViewGroup; +import android.widget.HorizontalScrollView; +import android.widget.LinearLayout; +import android.widget.RelativeLayout; +import android.widget.TextView; + +import com.flyco.tablayout.listener.OnTabSelectListener; +import com.flyco.tablayout.utils.UnreadMsgUtils; +import com.flyco.tablayout.widget.MsgView; + +import java.util.ArrayList; +import java.util.Collections; + +/** + * 滑动TabLayout,对于ViewPager的依赖性强 + */ +public class SlidingTabLayout extends HorizontalScrollView implements ViewPager.OnPageChangeListener { + + private static final int TOP = 0; + private static final int BOTTOM = 1; + private static final int CENTER = 2; + + private Context mContext; + private ViewPager mViewPager; + private ArrayList mTitles; + private LinearLayout mTabsContainer; + private int mCurrentTab; + private float mCurrentPositionOffset; + private int mTabCount; + /** + * 用于绘制显示器 + */ + private Rect mIndicatorRect = new Rect(); + /** + * 用于实现滚动居中 + */ + private Rect mTabRect = new Rect(); + private GradientDrawable mIndicatorDrawable = new GradientDrawable(); + + private Paint mRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + private Paint mDividerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + private Paint mTrianglePaint = new Paint(Paint.ANTI_ALIAS_FLAG); + private Path mTrianglePath = new Path(); + private static final int STYLE_NORMAL = 0; + private static final int STYLE_TRIANGLE = 1; + private static final int STYLE_BLOCK = 2; + private int mIndicatorStyle = STYLE_NORMAL; + + private float mTabPadding; + private boolean mTabSpaceEqual; + private float mTabWidth; + + /** + * indicator + */ + private int mIndicatorColor; + private float mIndicatorHeight; + private float mIndicatorWidth; + private float mIndicatorCornerRadius; + private float mIndicatorMarginLeft; + private float mIndicatorMarginTop; + private float mIndicatorMarginRight; + private float mIndicatorMarginBottom; + private int mIndicatorGravity; + private boolean mIndicatorWidthEqualTitle; + + /** + * underline + */ + private int mUnderlineColor; + private float mUnderlineHeight; + private int mUnderlineGravity; + + /** + * divider + */ + private int mDividerColor; + private float mDividerWidth; + private float mDividerPadding; + + /** + * title + */ + private static final int TEXT_BOLD_NONE = 0; + private static final int TEXT_BOLD_WHEN_SELECT = 1; + private static final int TEXT_BOLD_BOTH = 2; + private float mTextSelectSize; + private float mTextUnSelectSize; + private int mTextSelectColor; + private int mTextUnSelectColor; + private int mTextBold; + private boolean mTextAllCaps; + + private int mLastScrollX; + private int mHeight; + private boolean mSnapOnTabClick; + + /** + * tab的上下间距 + */ + private int mTabMarginTop; + private int mTabMarginBottom; + + /** + * tab摆放的位置,目前只支持top和bottom + */ + private int mTabGravity; + + public SlidingTabLayout(Context context) { + this(context, null, 0); + } + + public SlidingTabLayout(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public SlidingTabLayout(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + setFillViewport(true);//设置滚动视图是否可以伸缩其内容以填充视口 + setWillNotDraw(false);//重写onDraw方法,需要调用这个方法来清除flag + setClipChildren(false); + setClipToPadding(false); + + this.mContext = context; + mTabsContainer = new LinearLayout(context); + addView(mTabsContainer); + + obtainAttributes(context, attrs); + + //get layout_height + String height = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_height"); + + if (height.equals(ViewGroup.LayoutParams.MATCH_PARENT + "")) { + } else if (height.equals(ViewGroup.LayoutParams.WRAP_CONTENT + "")) { + } else { + int[] systemAttrs = {android.R.attr.layout_height}; + TypedArray a = context.obtainStyledAttributes(attrs, systemAttrs); + mHeight = a.getDimensionPixelSize(0, ViewGroup.LayoutParams.WRAP_CONTENT); + a.recycle(); + } + } + + private void obtainAttributes(Context context, AttributeSet attrs) { + TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SlidingTabLayout); + + mIndicatorStyle = ta.getInt(R.styleable.SlidingTabLayout_tl_indicator_style, STYLE_NORMAL); + mIndicatorColor = ta.getColor(R.styleable.SlidingTabLayout_tl_indicator_color, Color.parseColor(mIndicatorStyle == STYLE_BLOCK ? "#4B6A87" : "#ffffff")); + mIndicatorHeight = ta.getDimension(R.styleable.SlidingTabLayout_tl_indicator_height, + dp2px(mIndicatorStyle == STYLE_TRIANGLE ? 4 : (mIndicatorStyle == STYLE_BLOCK ? -1 : 2))); + mIndicatorWidth = ta.getDimension(R.styleable.SlidingTabLayout_tl_indicator_width, dp2px(mIndicatorStyle == STYLE_TRIANGLE ? 10 : -1)); + mIndicatorCornerRadius = ta.getDimension(R.styleable.SlidingTabLayout_tl_indicator_corner_radius, dp2px(mIndicatorStyle == STYLE_BLOCK ? -1 : 0)); + mIndicatorMarginLeft = ta.getDimension(R.styleable.SlidingTabLayout_tl_indicator_margin_left, dp2px(0)); + mIndicatorMarginTop = ta.getDimension(R.styleable.SlidingTabLayout_tl_indicator_margin_top, dp2px(mIndicatorStyle == STYLE_BLOCK ? 7 : 0)); + mIndicatorMarginRight = ta.getDimension(R.styleable.SlidingTabLayout_tl_indicator_margin_right, dp2px(0)); + mIndicatorMarginBottom = ta.getDimension(R.styleable.SlidingTabLayout_tl_indicator_margin_bottom, dp2px(mIndicatorStyle == STYLE_BLOCK ? 7 : 0)); + mIndicatorGravity = ta.getInt(R.styleable.SlidingTabLayout_tl_indicator_gravity, Gravity.BOTTOM); + mIndicatorWidthEqualTitle = ta.getBoolean(R.styleable.SlidingTabLayout_tl_indicator_width_equal_title, false); + + mUnderlineColor = ta.getColor(R.styleable.SlidingTabLayout_tl_underline_color, Color.parseColor("#ffffff")); + mUnderlineHeight = ta.getDimension(R.styleable.SlidingTabLayout_tl_underline_height, dp2px(0)); + mUnderlineGravity = ta.getInt(R.styleable.SlidingTabLayout_tl_underline_gravity, Gravity.BOTTOM); + + mDividerColor = ta.getColor(R.styleable.SlidingTabLayout_tl_divider_color, Color.parseColor("#ffffff")); + mDividerWidth = ta.getDimension(R.styleable.SlidingTabLayout_tl_divider_width, dp2px(0)); + mDividerPadding = ta.getDimension(R.styleable.SlidingTabLayout_tl_divider_padding, dp2px(12)); + + mTextSelectSize = ta.getDimension(R.styleable.SlidingTabLayout_tl_textSelectSize, sp2px(14)); + mTextUnSelectSize = ta.getDimension(R.styleable.SlidingTabLayout_tl_textUnSelectSize, sp2px(14)); + mTextSelectColor = ta.getColor(R.styleable.SlidingTabLayout_tl_textSelectColor, Color.parseColor("#ffffff")); + mTextUnSelectColor = ta.getColor(R.styleable.SlidingTabLayout_tl_textUnSelectColor, Color.parseColor("#AAffffff")); + mTextBold = ta.getInt(R.styleable.SlidingTabLayout_tl_textBold, TEXT_BOLD_NONE); + mTextAllCaps = ta.getBoolean(R.styleable.SlidingTabLayout_tl_textAllCaps, false); + + mTabSpaceEqual = ta.getBoolean(R.styleable.SlidingTabLayout_tl_tab_space_equal, false); + mTabWidth = ta.getDimension(R.styleable.SlidingTabLayout_tl_tab_width, dp2px(-1)); + mTabPadding = ta.getDimension(R.styleable.SlidingTabLayout_tl_tab_padding, mTabSpaceEqual || mTabWidth > 0 ? dp2px(0) : dp2px(20)); + // 得到设置的上下间距和gravity + mTabMarginTop = ta.getDimensionPixelSize(R.styleable.SlidingTabLayout_tl_tab_marginTop, 0); + mTabMarginBottom = ta.getDimensionPixelSize(R.styleable.SlidingTabLayout_tl_tab_marginBottom, 0); + mTabGravity = ta.getInt(R.styleable.SlidingTabLayout_tl_tab_gravity, CENTER); + + ta.recycle(); + } + + /** + * 关联ViewPager + */ + public void setViewPager(ViewPager vp) { + if (vp == null || vp.getAdapter() == null) { + throw new IllegalStateException("ViewPager or ViewPager adapter can not be NULL !"); + } + + this.mViewPager = vp; + + this.mViewPager.removeOnPageChangeListener(this); + this.mViewPager.addOnPageChangeListener(this); + notifyDataSetChanged(); + } + + /** + * 关联ViewPager,用于不想在ViewPager适配器中设置titles数据的情况 + */ + public void setTitle(String[] titles) { + if (titles == null || titles.length == 0) { + throw new IllegalStateException("Titles can not be EMPTY !"); + } + + mTitles = new ArrayList<>(); + Collections.addAll(mTitles, titles); + notifyDataSetChanged(); + } + + /** + * 关联ViewPager,用于不想在ViewPager适配器中设置titles数据的情况 + */ + public void setViewPager(ViewPager vp, String[] titles) { + if (vp == null || vp.getAdapter() == null) { + throw new IllegalStateException("ViewPager or ViewPager adapter can not be NULL !"); + } + + if (titles == null || titles.length == 0) { + throw new IllegalStateException("Titles can not be EMPTY !"); + } + + if (titles.length != vp.getAdapter().getCount()) { + throw new IllegalStateException("Titles length must be the same as the page count !"); + } + + this.mViewPager = vp; + mTitles = new ArrayList<>(); + Collections.addAll(mTitles, titles); + + this.mViewPager.removeOnPageChangeListener(this); + this.mViewPager.addOnPageChangeListener(this); + notifyDataSetChanged(); + } + + /** + * 关联ViewPager,用于连适配器都不想自己实例化的情况 + */ + public void setViewPager(ViewPager vp, String[] titles, FragmentActivity fa, ArrayList fragments) { + if (vp == null) { + throw new IllegalStateException("ViewPager can not be NULL !"); + } + + if (titles == null || titles.length == 0) { + throw new IllegalStateException("Titles can not be EMPTY !"); + } + + this.mViewPager = vp; + this.mViewPager.setAdapter(new InnerPagerAdapter(fa.getSupportFragmentManager(), fragments, titles)); + + this.mViewPager.removeOnPageChangeListener(this); + this.mViewPager.addOnPageChangeListener(this); + notifyDataSetChanged(); + } + + /** + * 更新数据 + */ + public void notifyDataSetChanged() { + mTabsContainer.removeAllViews(); + this.mTabCount = mTitles == null ? mViewPager.getAdapter().getCount() : mTitles.size(); + View tabView; + for (int i = 0; i < mTabCount; i++) { + tabView = View.inflate(mContext, R.layout.layout_tab, null); + TextView title = tabView.findViewById(R.id.tv_tab_title); + // 设置tab的位置信息 + setTabLayoutParams(title); + CharSequence pageTitle = mTitles == null ? mViewPager.getAdapter().getPageTitle(i) : mTitles.get(i); + addTab(i, pageTitle.toString(), tabView); + } + + updateTabStyles(); + } + + private void setTabLayoutParams(TextView title) { + RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) title.getLayoutParams(); + params.topMargin = mTabMarginTop; + params.bottomMargin = mTabMarginBottom; + if (mTabGravity == TOP) { + params.addRule(RelativeLayout.ALIGN_PARENT_TOP); + } else if (mTabGravity == BOTTOM) { + params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); + } else { + params.addRule(RelativeLayout.CENTER_VERTICAL); + } + title.setLayoutParams(params); + } + + public void addNewTab(String title) { + View tabView = View.inflate(mContext, R.layout.layout_tab, null); + if (mTitles != null) { + mTitles.add(title); + } + + CharSequence pageTitle = mTitles == null ? mViewPager.getAdapter().getPageTitle(mTabCount) : mTitles.get(mTabCount); + addTab(mTabCount, pageTitle.toString(), tabView); + this.mTabCount = mTitles == null ? mViewPager.getAdapter().getCount() : mTitles.size(); + + updateTabStyles(); + } + + /** + * 创建并添加tab + */ + private void addTab(final int position, String title, View tabView) { + TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title); + if (tv_tab_title != null) { + if (title != null) tv_tab_title.setText(title); + } + + tabView.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + int position = mTabsContainer.indexOfChild(v); + if (position != -1) { + setCurrentTab(position); + } + } + }); + + /** 每一个Tab的布局参数 */ + LinearLayout.LayoutParams lp_tab = mTabSpaceEqual ? + new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f) : + new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); + if (mTabWidth > 0) { + lp_tab = new LinearLayout.LayoutParams((int) mTabWidth, LayoutParams.MATCH_PARENT); + } + mTabsContainer.addView(tabView, position, lp_tab); + } + + private void updateTabStyles() { + for (int i = 0; i < mTabCount; i++) { + View v = mTabsContainer.getChildAt(i); +// v.setPadding((int) mTabPadding, v.getPaddingTop(), (int) mTabPadding, v.getPaddingBottom()); + TextView tv_tab_title = (TextView) v.findViewById(R.id.tv_tab_title); + if (tv_tab_title != null) { + tv_tab_title.setTextColor(i == mCurrentTab ? mTextSelectColor : mTextUnSelectColor); + tv_tab_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, i == mCurrentTab ? mTextSelectSize : mTextUnSelectSize); + tv_tab_title.setPadding((int) mTabPadding, 0, (int) mTabPadding, 0); + if (mTextAllCaps) { + tv_tab_title.setText(tv_tab_title.getText().toString().toUpperCase()); + } + + if (mTextBold == TEXT_BOLD_BOTH) { + tv_tab_title.getPaint().setFakeBoldText(true); + } + // 被选中设置为粗体 + else if (mTextBold == TEXT_BOLD_WHEN_SELECT && i == mCurrentTab) { + tv_tab_title.getPaint().setFakeBoldText(true); + } else if (mTextBold == TEXT_BOLD_NONE) { + tv_tab_title.getPaint().setFakeBoldText(false); + } + } + } + } + + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + /** + * position:当前View的位置 + * mCurrentPositionOffset:当前View的偏移量比例.[0,1) + */ + this.mCurrentTab = position; + this.mCurrentPositionOffset = positionOffset; + scrollToCurrentTab(); + invalidate(); + } + + @Override + public void onPageSelected(int position) { + updateTabSelection(position); + } + + @Override + public void onPageScrollStateChanged(int state) { + } + + /** + * HorizontalScrollView滚到当前tab,并且居中显示 + */ + private void scrollToCurrentTab() { + if (mTabCount <= 0) { + return; + } + + int offset = (int) (mCurrentPositionOffset * mTabsContainer.getChildAt(mCurrentTab).getWidth()); + /**当前Tab的left+当前Tab的Width乘以positionOffset*/ + int newScrollX = mTabsContainer.getChildAt(mCurrentTab).getLeft() + offset; + + if (mCurrentTab > 0 || offset > 0) { + /**HorizontalScrollView移动到当前tab,并居中*/ + newScrollX -= getWidth() / 2 - getPaddingLeft(); + calcIndicatorRect(); + newScrollX += ((mTabRect.right - mTabRect.left) / 2); + } + + if (newScrollX != mLastScrollX) { + mLastScrollX = newScrollX; + /** scrollTo(int x,int y):x,y代表的不是坐标点,而是偏移量 + * x:表示离起始位置的x水平方向的偏移量 + * y:表示离起始位置的y垂直方向的偏移量 + */ + scrollTo(newScrollX, 0); + } + } + + private void updateTabSelection(int position) { + for (int i = 0; i < mTabCount; ++i) { + View tabView = mTabsContainer.getChildAt(i); + final boolean isSelect = i == position; + TextView tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title); + + if (tab_title != null) { + tab_title.setTextColor(isSelect ? mTextSelectColor : mTextUnSelectColor); + tab_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, isSelect ? mTextSelectSize : mTextUnSelectSize); + if (mTextBold == TEXT_BOLD_WHEN_SELECT) { + tab_title.getPaint().setFakeBoldText(isSelect); + } + } + } + } + + private float margin; + + private void calcIndicatorRect() { + View currentTabView = mTabsContainer.getChildAt(this.mCurrentTab); + float left = currentTabView.getLeft(); + float right = currentTabView.getRight(); + + //for mIndicatorWidthEqualTitle + if (mIndicatorStyle == STYLE_NORMAL && mIndicatorWidthEqualTitle) { + TextView tab_title = (TextView) currentTabView.findViewById(R.id.tv_tab_title); + mTextPaint.setTextSize(mTextSelectSize); + float textWidth = mTextPaint.measureText(tab_title.getText().toString()); + margin = (right - left - textWidth) / 2; + } + + if (this.mCurrentTab < mTabCount - 1) { + View nextTabView = mTabsContainer.getChildAt(this.mCurrentTab + 1); + float nextTabLeft = nextTabView.getLeft(); + float nextTabRight = nextTabView.getRight(); + + left = left + mCurrentPositionOffset * (nextTabLeft - left); + right = right + mCurrentPositionOffset * (nextTabRight - right); + + //for mIndicatorWidthEqualTitle + if (mIndicatorStyle == STYLE_NORMAL && mIndicatorWidthEqualTitle) { + TextView next_tab_title = (TextView) nextTabView.findViewById(R.id.tv_tab_title); + mTextPaint.setTextSize(mTextUnSelectSize); + float nextTextWidth = mTextPaint.measureText(next_tab_title.getText().toString()); + float nextMargin = (nextTabRight - nextTabLeft - nextTextWidth) / 2; + margin = margin + mCurrentPositionOffset * (nextMargin - margin); + } + } + + mIndicatorRect.left = (int) left; + mIndicatorRect.right = (int) right; + //for mIndicatorWidthEqualTitle + if (mIndicatorStyle == STYLE_NORMAL && mIndicatorWidthEqualTitle) { + mIndicatorRect.left = (int) (left + margin - 1); + mIndicatorRect.right = (int) (right - margin - 1); + } + + mTabRect.left = (int) left; + mTabRect.right = (int) right; + + if (mIndicatorWidth < 0) { //indicatorWidth小于0时,原jpardogo's PagerSlidingTabStrip + + } else {//indicatorWidth大于0时,圆角矩形以及三角形 + float indicatorLeft = currentTabView.getLeft() + (currentTabView.getWidth() - mIndicatorWidth) / 2; + + if (this.mCurrentTab < mTabCount - 1) { + View nextTab = mTabsContainer.getChildAt(this.mCurrentTab + 1); + indicatorLeft = indicatorLeft + mCurrentPositionOffset * (currentTabView.getWidth() / 2 + nextTab.getWidth() / 2); + } + + mIndicatorRect.left = (int) indicatorLeft; + mIndicatorRect.right = (int) (mIndicatorRect.left + mIndicatorWidth); + } + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + + if (isInEditMode() || mTabCount <= 0) { + return; + } + + int height = getHeight(); + int paddingLeft = getPaddingLeft(); + // draw divider + if (mDividerWidth > 0) { + mDividerPaint.setStrokeWidth(mDividerWidth); + mDividerPaint.setColor(mDividerColor); + for (int i = 0; i < mTabCount - 1; i++) { + View tab = mTabsContainer.getChildAt(i); + canvas.drawLine(paddingLeft + tab.getRight(), mDividerPadding, paddingLeft + tab.getRight(), height - mDividerPadding, mDividerPaint); + } + } + + // draw underline + if (mUnderlineHeight > 0) { + mRectPaint.setColor(mUnderlineColor); + if (mUnderlineGravity == Gravity.BOTTOM) { + canvas.drawRect(paddingLeft, height - mUnderlineHeight, mTabsContainer.getWidth() + paddingLeft, height, mRectPaint); + } else { + canvas.drawRect(paddingLeft, 0, mTabsContainer.getWidth() + paddingLeft, mUnderlineHeight, mRectPaint); + } + } + + //draw indicator line + + calcIndicatorRect(); + if (mIndicatorStyle == STYLE_TRIANGLE) { + if (mIndicatorHeight > 0) { + mTrianglePaint.setColor(mIndicatorColor); + mTrianglePath.reset(); + mTrianglePath.moveTo(paddingLeft + mIndicatorRect.left, height); + mTrianglePath.lineTo(paddingLeft + mIndicatorRect.left / 2 + mIndicatorRect.right / 2, height - mIndicatorHeight); + mTrianglePath.lineTo(paddingLeft + mIndicatorRect.right, height); + mTrianglePath.close(); + canvas.drawPath(mTrianglePath, mTrianglePaint); + } + } else if (mIndicatorStyle == STYLE_BLOCK) { + if (mIndicatorHeight < 0) { + mIndicatorHeight = height - mIndicatorMarginTop - mIndicatorMarginBottom; + } else { + + } + + if (mIndicatorHeight > 0) { + if (mIndicatorCornerRadius < 0 || mIndicatorCornerRadius > mIndicatorHeight / 2) { + mIndicatorCornerRadius = mIndicatorHeight / 2; + } + + mIndicatorDrawable.setColor(mIndicatorColor); + mIndicatorDrawable.setBounds(paddingLeft + (int) mIndicatorMarginLeft + mIndicatorRect.left, + (int) mIndicatorMarginTop, (int) (paddingLeft + mIndicatorRect.right - mIndicatorMarginRight), + (int) (mIndicatorMarginTop + mIndicatorHeight)); + mIndicatorDrawable.setCornerRadius(mIndicatorCornerRadius); + mIndicatorDrawable.draw(canvas); + } + } else { + /* mRectPaint.setColor(mIndicatorColor); + calcIndicatorRect(); + canvas.drawRect(getPaddingLeft() + mIndicatorRect.left, getHeight() - mIndicatorHeight, + mIndicatorRect.right + getPaddingLeft(), getHeight(), mRectPaint);*/ + + if (mIndicatorHeight > 0) { + mIndicatorDrawable.setColor(mIndicatorColor); + + if (mIndicatorGravity == Gravity.BOTTOM) { + mIndicatorDrawable.setBounds(paddingLeft + (int) mIndicatorMarginLeft + mIndicatorRect.left, + height - (int) mIndicatorHeight - (int) mIndicatorMarginBottom, + paddingLeft + mIndicatorRect.right - (int) mIndicatorMarginRight, + height - (int) mIndicatorMarginBottom); + } else { + mIndicatorDrawable.setBounds(paddingLeft + (int) mIndicatorMarginLeft + mIndicatorRect.left, + (int) mIndicatorMarginTop, + paddingLeft + mIndicatorRect.right - (int) mIndicatorMarginRight, + (int) mIndicatorHeight + (int) mIndicatorMarginTop); + } + mIndicatorDrawable.setCornerRadius(mIndicatorCornerRadius); + mIndicatorDrawable.draw(canvas); + } + } + } + + //setter and getter + public void setCurrentTab(int currentTab) { + setCurrentTab(currentTab, !mSnapOnTabClick); + } + + public void setCurrentTab(int currentTab, boolean smoothScroll) { + if (mCurrentTab!= currentTab) { + this.mCurrentTab = currentTab; + if (mViewPager != null){ + mViewPager.setCurrentItem(currentTab, smoothScroll); + } + + if (mListener != null) { + mListener.onTabSelect(currentTab); + } + } else { + if (mListener != null) { + mListener.onTabReselect(currentTab); + } + } + } + + public void setIndicatorStyle(int indicatorStyle) { + this.mIndicatorStyle = indicatorStyle; + invalidate(); + } + + public void setTabPadding(float tabPadding) { + this.mTabPadding = dp2px(tabPadding); + updateTabStyles(); + } + + public void setTabSpaceEqual(boolean tabSpaceEqual) { + this.mTabSpaceEqual = tabSpaceEqual; + updateTabStyles(); + } + + public void setTabWidth(float tabWidth) { + this.mTabWidth = dp2px(tabWidth); + updateTabStyles(); + } + + public void setIndicatorColor(int indicatorColor) { + this.mIndicatorColor = indicatorColor; + invalidate(); + } + + public void setIndicatorHeight(float indicatorHeight) { + this.mIndicatorHeight = dp2px(indicatorHeight); + invalidate(); + } + + public void setIndicatorWidth(float indicatorWidth) { + this.mIndicatorWidth = dp2px(indicatorWidth); + invalidate(); + } + + public void setIndicatorCornerRadius(float indicatorCornerRadius) { + this.mIndicatorCornerRadius = dp2px(indicatorCornerRadius); + invalidate(); + } + + public void setIndicatorGravity(int indicatorGravity) { + this.mIndicatorGravity = indicatorGravity; + invalidate(); + } + + public void setIndicatorMargin(float indicatorMarginLeft, float indicatorMarginTop, + float indicatorMarginRight, float indicatorMarginBottom) { + this.mIndicatorMarginLeft = dp2px(indicatorMarginLeft); + this.mIndicatorMarginTop = dp2px(indicatorMarginTop); + this.mIndicatorMarginRight = dp2px(indicatorMarginRight); + this.mIndicatorMarginBottom = dp2px(indicatorMarginBottom); + invalidate(); + } + + public void setIndicatorWidthEqualTitle(boolean indicatorWidthEqualTitle) { + this.mIndicatorWidthEqualTitle = indicatorWidthEqualTitle; + invalidate(); + } + + public void setUnderlineColor(int underlineColor) { + this.mUnderlineColor = underlineColor; + invalidate(); + } + + public void setUnderlineHeight(float underlineHeight) { + this.mUnderlineHeight = dp2px(underlineHeight); + invalidate(); + } + + public void setUnderlineGravity(int underlineGravity) { + this.mUnderlineGravity = underlineGravity; + invalidate(); + } + + public void setDividerColor(int dividerColor) { + this.mDividerColor = dividerColor; + invalidate(); + } + + public void setDividerWidth(float dividerWidth) { + this.mDividerWidth = dp2px(dividerWidth); + invalidate(); + } + + public void setDividerPadding(float dividerPadding) { + this.mDividerPadding = dp2px(dividerPadding); + invalidate(); + } + + public void setTextSelectsize(float textsize) { + this.mTextSelectSize = sp2px(textsize); + updateTabStyles(); + } + + public void setTextUnselectSize(int textSize) { + this.mTextUnSelectSize = textSize; + updateTabStyles(); + } + + public void setTextSelectColor(int textSelectColor) { + this.mTextSelectColor = textSelectColor; + updateTabStyles(); + } + + public void setTextUnselectColor(int textUnselectColor) { + this.mTextUnSelectColor = textUnselectColor; + updateTabStyles(); + } + + public void setTextBold(int textBold) { + this.mTextBold = textBold; + updateTabStyles(); + } + + public void setTextAllCaps(boolean textAllCaps) { + this.mTextAllCaps = textAllCaps; + updateTabStyles(); + } + + public void setSnapOnTabClick(boolean snapOnTabClick) { + mSnapOnTabClick = snapOnTabClick; + } + + + public int getTabCount() { + return mTabCount; + } + + public int getCurrentTab() { + return mCurrentTab; + } + + public int getIndicatorStyle() { + return mIndicatorStyle; + } + + public float getTabPadding() { + return mTabPadding; + } + + public boolean isTabSpaceEqual() { + return mTabSpaceEqual; + } + + public float getTabWidth() { + return mTabWidth; + } + + public int getIndicatorColor() { + return mIndicatorColor; + } + + public float getIndicatorHeight() { + return mIndicatorHeight; + } + + public float getIndicatorWidth() { + return mIndicatorWidth; + } + + public float getIndicatorCornerRadius() { + return mIndicatorCornerRadius; + } + + public float getIndicatorMarginLeft() { + return mIndicatorMarginLeft; + } + + public float getIndicatorMarginTop() { + return mIndicatorMarginTop; + } + + public float getIndicatorMarginRight() { + return mIndicatorMarginRight; + } + + public float getIndicatorMarginBottom() { + return mIndicatorMarginBottom; + } + + public int getUnderlineColor() { + return mUnderlineColor; + } + + public float getUnderlineHeight() { + return mUnderlineHeight; + } + + public int getDividerColor() { + return mDividerColor; + } + + public float getDividerWidth() { + return mDividerWidth; + } + + public float getDividerPadding() { + return mDividerPadding; + } + + public float getTextSelectSize() { + return mTextSelectSize; + } + + public float getTextUnselectSize() { + return mTextUnSelectSize; + } + + public int getTextSelectColor() { + return mTextSelectColor; + } + + public int getTextUnselectColor() { + return mTextUnSelectColor; + } + + public int getTextBold() { + return mTextBold; + } + + public boolean isTextAllCaps() { + return mTextAllCaps; + } + + public TextView getTitleView(int tab) { + View tabView = mTabsContainer.getChildAt(tab); + return (TextView) tabView.findViewById(R.id.tv_tab_title); + } + + //setter and getter + + // show MsgTipView + private Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + private SparseBooleanArray mInitSetMap = new SparseBooleanArray(); + + /** + * 显示未读消息 + * + * @param position 显示tab位置 + * @param num num小于等于0显示红点,num大于0显示数字 + */ + public void showMsg(int position, int num) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + + View tabView = mTabsContainer.getChildAt(position); + MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip); + if (tipView != null) { + UnreadMsgUtils.show(tipView, num); + + if (mInitSetMap.get(position)) { + return; + } + + setMsgMargin(position, 4, 2); + mInitSetMap.put(position, true); + } + } + + /** + * 显示未读红点 + * + * @param position 显示tab位置 + */ + public void showDot(int position) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + showMsg(position, 0); + } + + /** + * 隐藏未读消息 + */ + public void hideMsg(int position) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + + View tabView = mTabsContainer.getChildAt(position); + MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip); + if (tipView != null) { + tipView.setVisibility(View.GONE); + } + } + + /** + * 设置未读消息偏移,原点为文字的右上角.当控件高度固定,消息提示位置易控制,显示效果佳 + */ + public void setMsgMargin(int position, float leftPadding, float bottomPadding) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + View tabView = mTabsContainer.getChildAt(position); + MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip); + if (tipView != null) { + TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title); + mTextPaint.setTextSize(position == mCurrentTab ? mTextSelectSize : mTextUnSelectSize); + float textWidth = mTextPaint.measureText(tv_tab_title.getText().toString()); + float textHeight = mTextPaint.descent() - mTextPaint.ascent(); + MarginLayoutParams lp = (MarginLayoutParams) tipView.getLayoutParams(); + lp.leftMargin = mTabWidth >= 0 ? (int) (mTabWidth / 2 + textWidth / 2 + dp2px(leftPadding)) : (int) (mTabPadding + textWidth + dp2px(leftPadding)); + lp.topMargin = mHeight > 0 ? (int) (mHeight - textHeight) / 2 - dp2px(bottomPadding) : 0; + tipView.setLayoutParams(lp); + } + } + + /** + * 当前类只提供了少许设置未读消息属性的方法,可以通过该方法获取MsgView对象从而各种设置 + */ + public MsgView getMsgView(int position) { + if (position >= mTabCount) { + position = mTabCount - 1; + } + View tabView = mTabsContainer.getChildAt(position); + MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip); + return tipView; + } + + private OnTabSelectListener mListener; + + public void setOnTabSelectListener(OnTabSelectListener listener) { + this.mListener = listener; + } + + class InnerPagerAdapter extends FragmentPagerAdapter { + private ArrayList fragments; + private String[] titles; + + public InnerPagerAdapter(FragmentManager fm, ArrayList fragments, String[] titles) { + super(fm); + this.fragments = fragments; + this.titles = titles; + } + + @Override + public int getCount() { + return fragments.size(); + } + + @Override + public CharSequence getPageTitle(int position) { + return titles[position]; + } + + @Override + public Fragment getItem(int position) { + return fragments.get(position); + } + + @Override + public void destroyItem(ViewGroup container, int position, Object object) { + // 覆写destroyItem并且空实现,这样每个Fragment中的视图就不会被销毁 + // super.destroyItem(container, position, object); + } + + @Override + public int getItemPosition(Object object) { + return PagerAdapter.POSITION_NONE; + } + } + + @Override + protected Parcelable onSaveInstanceState() { + Bundle bundle = new Bundle(); + bundle.putParcelable("instanceState", super.onSaveInstanceState()); + bundle.putInt("mCurrentTab", mCurrentTab); + return bundle; + } + + @Override + protected void onRestoreInstanceState(Parcelable state) { + if (state instanceof Bundle) { + Bundle bundle = (Bundle) state; + mCurrentTab = bundle.getInt("mCurrentTab"); + state = bundle.getParcelable("instanceState"); + if (mCurrentTab != 0 && mTabsContainer.getChildCount() > 0) { + updateTabSelection(mCurrentTab); + scrollToCurrentTab(); + } + } + super.onRestoreInstanceState(state); + } + + protected int dp2px(float dp) { + final float scale = mContext.getResources().getDisplayMetrics().density; + return (int) (dp * scale + 0.5f); + } + + protected int sp2px(float sp) { + final float scale = this.mContext.getResources().getDisplayMetrics().scaledDensity; + return (int) (sp * scale + 0.5f); + } +} diff --git a/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/listener/CustomTabEntity.java b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/listener/CustomTabEntity.java new file mode 100644 index 0000000..ee39fe7 --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/listener/CustomTabEntity.java @@ -0,0 +1,13 @@ +package com.flyco.tablayout.listener; + +import androidx.annotation.DrawableRes; + +public interface CustomTabEntity { + String getTabTitle(); + + @DrawableRes + int getTabSelectedIcon(); + + @DrawableRes + int getTabUnselectedIcon(); +} \ No newline at end of file diff --git a/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/listener/OnTabSelectListener.java b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/listener/OnTabSelectListener.java new file mode 100644 index 0000000..edf6cdb --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/listener/OnTabSelectListener.java @@ -0,0 +1,6 @@ +package com.flyco.tablayout.listener; + +public interface OnTabSelectListener { + void onTabSelect(int position); + void onTabReselect(int position); +} \ No newline at end of file diff --git a/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/transformer/ExtendTransformer.java b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/transformer/ExtendTransformer.java new file mode 100644 index 0000000..ce40c95 --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/transformer/ExtendTransformer.java @@ -0,0 +1,52 @@ +package com.flyco.tablayout.transformer; + +import androidx.annotation.NonNull; +import androidx.viewpager.widget.ViewPager; +import android.view.View; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by li.zhipeng on 2019/1/3. + *

+ * tab切换的 + */ +public class ExtendTransformer implements ViewPager.PageTransformer { + + private ArrayList transformers = new ArrayList<>(); + + + public ExtendTransformer() { + } + + public void addViewPagerTransformer(IViewPagerTransformer transformer) { + if (!transformers.contains(transformer)) { + transformers.add(transformer); + } + } + + public void removeViewPagerTransformer(IViewPagerTransformer transformer) { + transformers.remove(transformer); + } + + public List getTransformers() { + return transformers; + } + + public void setTransformers(List transformers) { + this.transformers.addAll(transformers); + } + + @Override + public void transformPage(@NonNull View view, final float position) { + // 回调设置的页面切换效果设置 + if (transformers != null && transformers.size() > 0) { + for (IViewPagerTransformer transformer : transformers) { + transformer.transformPage(view, position); + } + } + } + + +} diff --git a/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/transformer/ITabScaleTransformer.java b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/transformer/ITabScaleTransformer.java new file mode 100644 index 0000000..5a989a9 --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/transformer/ITabScaleTransformer.java @@ -0,0 +1,6 @@ +package com.flyco.tablayout.transformer; + +public interface ITabScaleTransformer { + void setNormalWidth(int position, int width, boolean isSelect); + void onPageScrolled(int position, float positionOffset, int positionOffsetPixels); +} diff --git a/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/transformer/IViewPagerTransformer.java b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/transformer/IViewPagerTransformer.java new file mode 100644 index 0000000..93235a5 --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/transformer/IViewPagerTransformer.java @@ -0,0 +1,12 @@ +package com.flyco.tablayout.transformer; + +import androidx.viewpager.widget.ViewPager; + +/** + * Created by li.zhipeng on 2019/1/3. + * + * ViewPager的扩展Transformer,配合SlidingScaleTabLayout使用 + * 因为字体的切换效果设置了默认的Transformer,所以扩展此接口 + */ +public interface IViewPagerTransformer extends ViewPager.PageTransformer { +} diff --git a/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/transformer/TabScaleTransformer.java b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/transformer/TabScaleTransformer.java new file mode 100644 index 0000000..5757d64 --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/transformer/TabScaleTransformer.java @@ -0,0 +1,110 @@ +package com.flyco.tablayout.transformer; + +import android.util.Log; +import android.util.TypedValue; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import com.flyco.tablayout.SlidingScaleTabLayout; + +/** + * Created by li.zhipeng on 2019/1/3. + *

+ * tab切换的 + */ +public class TabScaleTransformer implements ITabScaleTransformer { + + private SlidingScaleTabLayout slidingScaleTabLayout; + + private float textSelectSize; + + private float textUnSelectSize; + +// private float maxScale; + + private boolean openDmg; + + public TabScaleTransformer(SlidingScaleTabLayout slidingScaleTabLayout, + float textSelectSize, float textUnSelectSize, boolean openDmg) { + this.slidingScaleTabLayout = slidingScaleTabLayout; + this.textSelectSize = textSelectSize; + this.textUnSelectSize = textUnSelectSize; +// this.maxScale = (textSelectSize / textUnSelectSize) - 1; + this.openDmg = openDmg; + } + + @Override + public void setNormalWidth(int position, int width, boolean isSelect) { + } + + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + Log.i("TabScaleTransformer", "position:" + position); + // 字体大小相同,不需要切换 + if (textSelectSize == textUnSelectSize) return; + if (openDmg) { + for (int i = 0; i < slidingScaleTabLayout.getTabCount(); i++) { + if (i != position && i != position + 1) { + changTabDmgWidth(i, 0); + } + } + changeDmgSize(position, positionOffset); + } else { + for (int i = 0; i < slidingScaleTabLayout.getTabCount(); i++) { + if (i != position && i != position + 1) { + updateTextSize(i, 1); + } + } + changeTextSize(position, positionOffset); + } + } + + private void changeTextSize(final int position, final float positionOffset) { + updateTextSize(position, positionOffset); + if (position + 1 < slidingScaleTabLayout.getTabCount()) { + updateTextSize(position + 1, 1 - positionOffset); + } + } + + private void updateTextSize(final int position, final float positionOffset) { + final TextView currentTab = slidingScaleTabLayout.getTitle(position); + // 必须要在View调用post更新样式,否则可能无效 + currentTab.post(new Runnable() { + @Override + public void run() { + int textSize = (int) (textSelectSize - Math.abs((textSelectSize - textUnSelectSize) * positionOffset)); + if (currentTab.getTextSize() != textSize) { + currentTab.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); + currentTab.requestLayout(); + } + } + }); + } + + private void changeDmgSize(final int position, final float positionOffset) { + slidingScaleTabLayout.post(new Runnable() { + @Override + public void run() { +// Log.i("lzp", "position:" + position + " positionOffset:" + positionOffset); + float scale = 1 - positionOffset; + changTabDmgWidth(position, scale); + if (position + 1 < slidingScaleTabLayout.getTabCount()) { + changTabDmgWidth(position + 1, positionOffset); + } + } + }); + } + + private void changTabDmgWidth(int position, float scale) { + final ImageView currentTabDmg = slidingScaleTabLayout.getDmgView(position); + if (currentTabDmg == null) return; + if (currentTabDmg.getDrawable() == null) return; + ViewGroup.LayoutParams params = currentTabDmg.getLayoutParams(); + int width = (int) (currentTabDmg.getMinimumWidth() + (currentTabDmg.getMaxWidth() - currentTabDmg.getMinimumWidth()) * scale); + if (params.width != width) { + params.width = width; + currentTabDmg.setLayoutParams(params); + } +// Log.i("lzp", "position:" + position + " scale:" + scale + " width:" + params.width); + } +} \ No newline at end of file diff --git a/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/utils/FragmentChangeManager.java b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/utils/FragmentChangeManager.java new file mode 100644 index 0000000..f47b315 --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/utils/FragmentChangeManager.java @@ -0,0 +1,55 @@ +package com.flyco.tablayout.utils; + +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentTransaction; + +import java.util.ArrayList; + +public class FragmentChangeManager { + private FragmentManager mFragmentManager; + private int mContainerViewId; + /** Fragment切换数组 */ + private ArrayList mFragments; + /** 当前选中的Tab */ + private int mCurrentTab; + + public FragmentChangeManager(FragmentManager fm, int containerViewId, ArrayList fragments) { + this.mFragmentManager = fm; + this.mContainerViewId = containerViewId; + this.mFragments = fragments; + initFragments(); + } + + /** 初始化fragments */ + private void initFragments() { + for (Fragment fragment : mFragments) { + mFragmentManager.beginTransaction().add(mContainerViewId, fragment).hide(fragment).commit(); + } + + setFragments(0); + } + + /** 界面切换控制 */ + public void setFragments(int index) { + for (int i = 0; i < mFragments.size(); i++) { + FragmentTransaction ft = mFragmentManager.beginTransaction(); + Fragment fragment = mFragments.get(i); + if (i == index) { + ft.show(fragment); + } else { + ft.hide(fragment); + } + ft.commit(); + } + mCurrentTab = index; + } + + public int getCurrentTab() { + return mCurrentTab; + } + + public Fragment getCurrentFragment() { + return mFragments.get(mCurrentTab); + } +} \ No newline at end of file diff --git a/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/utils/UnreadMsgUtils.java b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/utils/UnreadMsgUtils.java new file mode 100644 index 0000000..0446bfa --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/utils/UnreadMsgUtils.java @@ -0,0 +1,58 @@ +package com.flyco.tablayout.utils; + + +import android.util.DisplayMetrics; +import android.view.View; +import android.widget.RelativeLayout; + +import com.flyco.tablayout.widget.MsgView; + +/** + * 未读消息提示View,显示小红点或者带有数字的红点: + * 数字一位,圆 + * 数字两位,圆角矩形,圆角是高度的一半 + * 数字超过两位,显示99+ + */ +public class UnreadMsgUtils { + public static void show(MsgView msgView, int num) { + if (msgView == null) { + return; + } + RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) msgView.getLayoutParams(); + DisplayMetrics dm = msgView.getResources().getDisplayMetrics(); + msgView.setVisibility(View.VISIBLE); + if (num <= 0) {//圆点,设置默认宽高 + msgView.setStrokeWidth(0); + msgView.setText(""); + + lp.width = (int) (5 * dm.density); + lp.height = (int) (5 * dm.density); + msgView.setLayoutParams(lp); + } else { + lp.height = (int) (18 * dm.density); + if (num < 10) {//圆 + lp.width = (int) (18 * dm.density); + msgView.setText(num + ""); + } else if (num < 100) {//圆角矩形,圆角是高度的一半,设置默认padding + lp.width = RelativeLayout.LayoutParams.WRAP_CONTENT; + msgView.setPadding((int) (6 * dm.density), 0, (int) (6 * dm.density), 0); + msgView.setText(num + ""); + } else {//数字超过两位,显示99+ + lp.width = RelativeLayout.LayoutParams.WRAP_CONTENT; + msgView.setPadding((int) (6 * dm.density), 0, (int) (6 * dm.density), 0); + msgView.setText("99+"); + } + msgView.setLayoutParams(lp); + } + } + + public static void setSize(MsgView rtv, int size) { + if (rtv == null) { + return; + } + RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) rtv.getLayoutParams(); + lp.width = size; + lp.height = size; + rtv.setLayoutParams(lp); + } +} diff --git a/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/utils/ViewUtils.java b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/utils/ViewUtils.java new file mode 100644 index 0000000..2f54c6b --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/utils/ViewUtils.java @@ -0,0 +1,39 @@ +package com.flyco.tablayout.utils; + +import android.graphics.Bitmap; +import androidx.annotation.IdRes; +import android.view.View; + +public class ViewUtils { + + public static Bitmap generateViewCacheBitmap(View view) { + view.destroyDrawingCache(); + int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); + int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); + view.measure(widthMeasureSpec, heightMeasureSpec); + int width = view.getMeasuredWidth(); + int height = view.getMeasuredHeight(); + view.layout(0, 0, width, height); + view.setDrawingCacheEnabled(true); + view.buildDrawingCache(); + return Bitmap.createBitmap(view.getDrawingCache()); + } + + public static View findBrotherView(View view, @IdRes int id, int level) { + int count = 0; + View temp = view; + while (count < level) { + View target = temp.findViewById(id); + if (target != null) { + return target; + } + count += 1; + if (temp.getParent() instanceof View) { + temp = (View) temp.getParent(); + } else { + break; + } + } + return null; + } +} diff --git a/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/widget/MsgView.java b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/widget/MsgView.java new file mode 100644 index 0000000..59dd2b6 --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/java/com/flyco/tablayout/widget/MsgView.java @@ -0,0 +1,157 @@ +package com.flyco.tablayout.widget; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Color; +import android.graphics.drawable.GradientDrawable; +import android.graphics.drawable.StateListDrawable; +import android.os.Build; +import android.util.AttributeSet; +import android.widget.TextView; + +import com.flyco.tablayout.R; + +/** 用于需要圆角矩形框背景的TextView的情况,减少直接使用TextView时引入的shape资源文件 */ +public class MsgView extends TextView { + private Context context; + private GradientDrawable gd_background = new GradientDrawable(); + private int backgroundColor; + private int cornerRadius; + private int strokeWidth; + private int strokeColor; + private boolean isRadiusHalfHeight; + private boolean isWidthHeightEqual; + + public MsgView(Context context) { + this(context, null); + } + + public MsgView(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public MsgView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + this.context = context; + obtainAttributes(context, attrs); + } + + private void obtainAttributes(Context context, AttributeSet attrs) { + TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MsgView); + backgroundColor = ta.getColor(R.styleable.MsgView_mv_backgroundColor, Color.TRANSPARENT); + cornerRadius = ta.getDimensionPixelSize(R.styleable.MsgView_mv_cornerRadius, 0); + strokeWidth = ta.getDimensionPixelSize(R.styleable.MsgView_mv_strokeWidth, 0); + strokeColor = ta.getColor(R.styleable.MsgView_mv_strokeColor, Color.TRANSPARENT); + isRadiusHalfHeight = ta.getBoolean(R.styleable.MsgView_mv_isRadiusHalfHeight, false); + isWidthHeightEqual = ta.getBoolean(R.styleable.MsgView_mv_isWidthHeightEqual, false); + + ta.recycle(); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + if (isWidthHeightEqual() && getWidth() > 0 && getHeight() > 0) { + int max = Math.max(getWidth(), getHeight()); + int measureSpec = MeasureSpec.makeMeasureSpec(max, MeasureSpec.EXACTLY); + super.onMeasure(measureSpec, measureSpec); + return; + } + + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + } + + @Override + protected void onLayout(boolean changed, int left, int top, int right, int bottom) { + super.onLayout(changed, left, top, right, bottom); + if (isRadiusHalfHeight()) { + setCornerRadius(getHeight() / 2); + } else { + setBgSelector(); + } + } + + + public void setBackgroundColor(int backgroundColor) { + this.backgroundColor = backgroundColor; + setBgSelector(); + } + + public void setCornerRadius(int cornerRadius) { + this.cornerRadius = dp2px(cornerRadius); + setBgSelector(); + } + + public void setStrokeWidth(int strokeWidth) { + this.strokeWidth = dp2px(strokeWidth); + setBgSelector(); + } + + public void setStrokeColor(int strokeColor) { + this.strokeColor = strokeColor; + setBgSelector(); + } + + public void setIsRadiusHalfHeight(boolean isRadiusHalfHeight) { + this.isRadiusHalfHeight = isRadiusHalfHeight; + setBgSelector(); + } + + public void setIsWidthHeightEqual(boolean isWidthHeightEqual) { + this.isWidthHeightEqual = isWidthHeightEqual; + setBgSelector(); + } + + public int getBackgroundColor() { + return backgroundColor; + } + + public int getCornerRadius() { + return cornerRadius; + } + + public int getStrokeWidth() { + return strokeWidth; + } + + public int getStrokeColor() { + return strokeColor; + } + + public boolean isRadiusHalfHeight() { + return isRadiusHalfHeight; + } + + public boolean isWidthHeightEqual() { + return isWidthHeightEqual; + } + + protected int dp2px(float dp) { + final float scale = context.getResources().getDisplayMetrics().density; + return (int) (dp * scale + 0.5f); + } + + protected int sp2px(float sp) { + final float scale = this.context.getResources().getDisplayMetrics().scaledDensity; + return (int) (sp * scale + 0.5f); + } + + private void setDrawable(GradientDrawable gd, int color, int strokeColor) { + gd.setColor(color); + gd.setCornerRadius(cornerRadius); + gd.setStroke(strokeWidth, strokeColor); + } + + public void setBgSelector() { + StateListDrawable bg = new StateListDrawable(); + + setDrawable(gd_background, backgroundColor, strokeColor); + bg.addState(new int[]{-android.R.attr.state_pressed}, gd_background); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {//16 + setBackground(bg); + } else { + //noinspection deprecation + setBackgroundDrawable(bg); + } + } +} diff --git a/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_scale_tab.xml b/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_scale_tab.xml new file mode 100644 index 0000000..1533d51 --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_scale_tab.xml @@ -0,0 +1,38 @@ + + + + + + + + + + \ No newline at end of file diff --git a/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab.xml b/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab.xml new file mode 100644 index 0000000..9a46415 --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab.xml @@ -0,0 +1,31 @@ + + + + + + + + \ No newline at end of file diff --git a/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_bottom.xml b/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_bottom.xml new file mode 100644 index 0000000..229e640 --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_bottom.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_left.xml b/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_left.xml new file mode 100644 index 0000000..dd9c729 --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_left.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_right.xml b/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_right.xml new file mode 100644 index 0000000..0d65d85 --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_right.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_segment.xml b/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_segment.xml new file mode 100644 index 0000000..6d36a01 --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_segment.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_top.xml b/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_top.xml new file mode 100644 index 0000000..68478b6 --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/res/layout/layout_tab_top.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/FlycoTabLayoutZ_Lib/src/main/res/values/attrs.xml b/FlycoTabLayoutZ_Lib/src/main/res/values/attrs.xml new file mode 100644 index 0000000..324c0ca --- /dev/null +++ b/FlycoTabLayoutZ_Lib/src/main/res/values/attrs.xml @@ -0,0 +1,319 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 440fdc7..3c83bbe 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -15,8 +15,8 @@ android { applicationId "com.uiuios.aios" minSdkVersion 24 targetSdkVersion 29 - versionCode 48 - versionName "5.7" + versionCode 49 + versionName "5.8" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" @@ -189,6 +189,8 @@ dependencies { // implementation 'io.github.lucksiege:pictureselector:v2.5.8' //验证码输入 // implementation 'com.jacktuotuo.customview:verificationcodeview:1.0.5' +// implementation project(path: ':FlycoTabLayoutZ_Lib') + implementation 'com.github.wuao:FlycoTabLayout2:Tag1.1.3' implementation 'com.contrarywind:Android-PickerView:4.1.9' implementation 'com.github.ForgetAll:LoadingDialog:v1.1.2' //动态权限框架 diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e19195f..380fc68 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -204,6 +204,7 @@ { private static final String TAG = PhoneActivity.class.getSimpleName(); + private String[] title = new String[]{"通话", "记录", "联系人"}; private FragmentManager mFragmentManager; private CustomPagerAdapter mCustomPagerAdapter; private List mFragments; - private ContactFragment mContactFragment; private DialerFragment mDialerFragment; private RecordFragment mRecordFragment; + private ContactFragment mContactFragment; @Override protected int getLayoutId() { @@ -44,20 +45,25 @@ public class PhoneActivity extends BaseMvvmActivity(); - if (mContactFragment == null) { - mContactFragment = new ContactFragment(); - } - mFragments.add(mContactFragment); + if (mDialerFragment == null) { mDialerFragment = new DialerFragment(); } mFragments.add(mDialerFragment); + if (mRecordFragment == null) { mRecordFragment = new RecordFragment(); } mFragments.add(mRecordFragment); + + if (mContactFragment == null) { + mContactFragment = new ContactFragment(); + } + mFragments.add(mContactFragment); + mCustomPagerAdapter = new CustomPagerAdapter(mFragmentManager, mFragments, getLifecycle()); mViewDataBinding.viewPager.setAdapter(mCustomPagerAdapter); + mViewDataBinding.slidingTabLayout.setViewPager(mViewDataBinding.viewPager, title); } @Override @@ -78,6 +84,7 @@ public class PhoneActivity extends BaseMvvmActivity { + private static final String TAG = CallRecordAdapter.class.getSimpleName(); + private Context mContext; private List mRecordsInfoList; @@ -82,10 +85,15 @@ public class CallRecordAdapter extends RecyclerView.Adapter>() { @Override public void onChanged(List contacts) { @@ -95,6 +91,13 @@ public class ContactFragment extends BaseMvvmFragment { +public class DialerFragment extends BaseMvvmFragment { private static final String TAG = DialerFragment.class.getSimpleName(); private Context mContext; @@ -39,7 +41,7 @@ public class DialerFragment extends BaseMvvmFragment { +public class DialerViewModel extends BaseViewModel { @Override - public FragmentDialerBinding getVDBinding() { + public FragmentDialerNewBinding getVDBinding() { return binding; } @@ -16,5 +16,4 @@ public class DialerViewModel extends BaseViewModelR1W1XpgiQ$|AmG9hk`W>#6Ow_1rGS8lK7ka9 zLP3367nC9h3PyuQMM1RCiiNrWLL+&K3q}jFC{3*R-XG65-^~1$@0@e*{oV6@Gkb#q zeGT-?^#A}c@b_Z`YsMh$ql4AFiwxDZn!yb5jzB`-9f+7Ega8i?91DT|JXRbO46!(= zNw*<40GNa0hDIO}%m5l2<`Gz03_-#ZXxISYwoW2ou@fK!jD_O3d^+y+<*PW5%c0}K zDNGVm;048V{nCU`NLpYhJ1v3j%E7H$3%W^Y8UY@Jus{hfkuRc2=(tb1G|gPQO~iqp zAV>lo_jghe%plMU7D6C};N-w2k;x#HN+7SHP^skApd*RwNF-@)ssniqjY6e4I)k4t zoFE62)RMLF`0;g>gi(tE;P4!_m<}gK!X~@)4HAfiJ?( zDli}sTgVk4T$m4P6I7RY4&e<+Xl z1ua5?q5tIjKZQl1sRD=?42j@mAzRb99e8ah0nJMYu@G1o3d4!Brx+9uBd{nQ7JyzM zRB%N&m(PL4qLrWUOeW2rFG5&+HssHs<1`8cE|)_id#`aKlc+vUYaA(LvbT??i=(3} zgW}}uMW%SWI8$f2449qFgZRiSm-B_|O!_KUYXzP_6PW=CxhW9GM+o!4PbJg1U(bc{ zRlUz#&R27xdViHm)FeaHHuk?7J-elGk9PXSwwlEk<3oIn+l3li|2Y%h2>=+WKf@zb z^6$V8*fMr|{Z^Y?)ZcT>-mJiM+KYvgUEZ^Jm9P1$M{f9xqf2{TYlHa|^yv+9k< zqj?skVcj}@_wsxS%NQj+KZIH9W9R5mR%ZUh+AWph3ODU*!oIN_TOHWc*x2{%?xFNJ z)sgAOO75lcs)5cvnRvLfqU2uhS-k z=QnED!!GE|MS8v(lpT;4$#b@ssIqpH-f1k)B-oZE644RF2av2U81T8v~xk1eR9?2%Vy?0akly7@hN zxp+fk>q&~jXCetMos&lxir8w`zkR~fSYfVasdwi2*4UjgzltI(&+?AS z8RhO(Ha#Y0C#eD1;=#=)l&0h6F*5y-B{tJrZDj}2F@mPM4u7n$x@o5xX#w6Zm{@Cc z#6{IvfmZGs4m7Gu8Ma7lQwMa>3{Q7+AFAfo-}%KX-`eTGSx*$TBV*IHmS)}Z4CD8E z9&h+HDtstGbT{@6f>LwG!wwB5I|F;mW}@d;M`K^8XGr5G>u=Am*EK&<2E`+K@CsC7C;8XsW?_px8%Z6y(yoBj_YM0G8a;Su*>*kbhihZY z%ks{-n?67*$Ge{E!OSA-vaz~d2eD=OZ>!2Gjlt-&pUApaxd~Q5R>73iAD1ff{6|eK z#eLb|w`1G)sM1QGB=s5u6?-nx;Q;)+nN`^9=!MsJ<{HOD&aI32Z40W$XH+kYhXtbx zFFE9d*Oza>%#l0isJ7OaKA{`B^**z*)>sZrCChHnVuat zJu$_bZvm?6oiA^+d0}2ZH!2T*HgG%&&JsS3YLlQMM1F_V8!J@^O{2VmRQ4eL=2)TKJctz@huD!IlcM^c13tat3G z)O~t4YFX}1`rJjO`lAx5q3MF8a#W@TVubZ_>i~RnU);iaDdT;edKvjhK|86ytD-`u zX5+zX{hZ%nOuIOx*mu#X+9Xzr0SLPs51IMJa_H#Y1#yuUjnU1~+(MfuHiT&(sR};n zk-=_JOEZnbLI7d%KbsNDL|2cDOoP!&50i~w)`y~RM;)D9JCVa(w5i61vufZcdWsd0jV# zQbD9Hnff5i0$YGPW37$dKj*tGOY}Ux%%$o=m-wncJjS}*etx5=!oj_Vp#Bb*CuRuS z0#JeAxAPed1yAhkR99Po_Mr_&7M6A`LCG6SM$Qw)Z|O~kz3(q@vdI_w2&+=yRp*6< zg%{ns#ryLX^%p2p8l+kKWYY)YCo?bfXg~Nu`-@^(Z9g^tUE#8&Hrc&4vi-6NRmJ^< z${*i$D}VOul~9?MqFg0uaLh|y9W${vAbG8mj#XO+Mv)#j=@Io?(9`b68E^Z-;xp_6 zVjtO`J1KwfURU#8xGS9S>?0bzx{Hr5I^i8V@yWzEyGV z{QAVh_VySUaIbM(h>v-DL+6#}$32G1e=Jwr>78KB&D9yap;Hw~@V#-Oe9~pV>_AKM s_6(2VGZk&D>AZJ;kUHm;DYDS|K*IfWsawb3S?&Ldzjq+xtY=KlUov&lUjP6A literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable/add_contact_confirm_background.xml b/app/src/main/res/drawable/add_contact_confirm_background.xml new file mode 100644 index 0000000..c9e83b8 --- /dev/null +++ b/app/src/main/res/drawable/add_contact_confirm_background.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/add_wechat_contact_background.xml b/app/src/main/res/drawable/add_wechat_contact_background.xml new file mode 100644 index 0000000..ecbf7e6 --- /dev/null +++ b/app/src/main/res/drawable/add_wechat_contact_background.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/bt_dialer_background.xml b/app/src/main/res/drawable/bt_dialer_background.xml new file mode 100644 index 0000000..105ac18 --- /dev/null +++ b/app/src/main/res/drawable/bt_dialer_background.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/bt_dialer_other_background.xml b/app/src/main/res/drawable/bt_dialer_other_background.xml new file mode 100644 index 0000000..7c1ae6e --- /dev/null +++ b/app/src/main/res/drawable/bt_dialer_other_background.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/contact_adapter_bg.xml b/app/src/main/res/drawable/contact_adapter_bg.xml new file mode 100644 index 0000000..a4469ed --- /dev/null +++ b/app/src/main/res/drawable/contact_adapter_bg.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/contact_add_contact_bg.xml b/app/src/main/res/drawable/contact_add_contact_bg.xml new file mode 100644 index 0000000..0ea83e7 --- /dev/null +++ b/app/src/main/res/drawable/contact_add_contact_bg.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/dialer_call_bg.xml b/app/src/main/res/drawable/dialer_call_bg.xml index 39dce6e..2059a8e 100644 --- a/app/src/main/res/drawable/dialer_call_bg.xml +++ b/app/src/main/res/drawable/dialer_call_bg.xml @@ -1,9 +1,14 @@ - + - + + + diff --git a/app/src/main/res/drawable/seekbar_progress_default.xml b/app/src/main/res/drawable/seekbar_progress_default.xml index 30e5bfb..47fed2c 100644 --- a/app/src/main/res/drawable/seekbar_progress_default.xml +++ b/app/src/main/res/drawable/seekbar_progress_default.xml @@ -36,7 +36,7 @@ - + diff --git a/app/src/main/res/layout-land/activity_phone.xml b/app/src/main/res/layout-land/activity_phone.xml index dd2caac..44e9815 100644 --- a/app/src/main/res/layout-land/activity_phone.xml +++ b/app/src/main/res/layout-land/activity_phone.xml @@ -16,6 +16,22 @@ android:layout_height="match_parent" android:background="@drawable/bg_aero"> + + + android:layout_height="match_parent"> + + + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintTop_toBottomOf="@+id/sliding_tab_layout" + tools:layout_editor_absoluteX="27dp" /> diff --git a/app/src/main/res/layout-port/fragment_home.xml b/app/src/main/res/layout-port/fragment_home.xml index 1ac1ecb..53a08b4 100644 --- a/app/src/main/res/layout-port/fragment_home.xml +++ b/app/src/main/res/layout-port/fragment_home.xml @@ -52,7 +52,6 @@ app:layout_constraintTop_toTopOf="parent" tools:text="00:00" /> - @@ -32,15 +31,17 @@ android:layout_height="wrap_content" android:layout_marginStart="@dimen/dp_16" android:text="新建联系人" - android:textColor="@color/default_text_color" - android:textSize="@dimen/sp_18" + android:textColor="@color/white" + android:textSize="@dimen/sp_22" app:layout_constraintBottom_toBottomOf="parent" - app:layout_constraintStart_toEndOf="@+id/iv_cancel" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/activity_add_wechat_contact.xml b/app/src/main/res/layout/activity_add_wechat_contact.xml index d7ae28e..d730cbe 100644 --- a/app/src/main/res/layout/activity_add_wechat_contact.xml +++ b/app/src/main/res/layout/activity_add_wechat_contact.xml @@ -14,7 +14,7 @@ + android:background="#404146"> @@ -42,231 +42,301 @@ android:layout_height="wrap_content" android:layout_marginStart="@dimen/dp_16" android:text="新建微信联系人" - android:textColor="@color/default_text_color" + android:textColor="@color/white" android:textSize="@dimen/sp_22" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/layout/fragment_contact.xml b/app/src/main/res/layout/fragment_contact.xml index 04e4cd1..ea57725 100644 --- a/app/src/main/res/layout/fragment_contact.xml +++ b/app/src/main/res/layout/fragment_contact.xml @@ -6,6 +6,9 @@ + - - - - - + + + + + + + + + + + + + - + android:layout_height="@dimen/dp_72" + android:layout_weight="2" + android:background="@drawable/contact_add_contact_bg" + android:onClick="@{click::add}" + app:layout_constraintBottom_toBottomOf="parent"> + + + + + + + + diff --git a/app/src/main/res/layout/fragment_dialer_new.xml b/app/src/main/res/layout/fragment_dialer_new.xml new file mode 100644 index 0000000..cfa946c --- /dev/null +++ b/app/src/main/res/layout/fragment_dialer_new.xml @@ -0,0 +1,525 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_record.xml b/app/src/main/res/layout/fragment_record.xml index 40ec807..6e97deb 100644 --- a/app/src/main/res/layout/fragment_record.xml +++ b/app/src/main/res/layout/fragment_record.xml @@ -27,10 +27,10 @@ @@ -40,7 +40,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white" - android:textSize="@dimen/sp_18" + android:textSize="@dimen/sp_22" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:text="name" /> @@ -56,32 +56,33 @@ app:layout_constraintTop_toBottomOf="@+id/tv_name" tools:text="12345665432" /> + + - - + app:layout_constraintTop_toTopOf="parent" /> + + /> + android:layout_marginStart="@dimen/dp_16" + app:layout_constraintBottom_toBottomOf="@+id/iv_head" + app:layout_constraintEnd_toStartOf="@+id/iv_sim" + app:layout_constraintStart_toEndOf="@+id/iv_head" + app:layout_constraintTop_toTopOf="@+id/iv_head">