diff --git a/TAB_README.md b/TAB_README.md index fe53922..cfc0771 100644 --- a/TAB_README.md +++ b/TAB_README.md @@ -361,6 +361,7 @@ private void resFlow(){ |tab_action_orientaion|integer|left坐标,right右边,只支持 tri、rect 两种效果| |tab_isAutoScroll|boolean|是否支持自动滚动,默认为true| |tab_visual_count|integer|可视化个数,比如有一排,我们就只要显示4个,此时宽度均分| +|tab_width_equals_text|boolean|rect 是否根据text的大小来,目前只支持rect和带viewpager的情况| **TabColorTextView** diff --git a/appx/build.gradle b/appx/build.gradle index 42ccab6..ba2382d 100644 --- a/appx/build.gradle +++ b/appx/build.gradle @@ -29,8 +29,8 @@ dependencies { androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' - //implementation project(path: ':tablibx') - implementation 'com.github.LillteZheng:FlowHelper:v1.33' + implementation project(path: ':tablibx') + //implementation 'com.github.LillteZheng:FlowHelper:v1.34' implementation 'me.yokeyword:fragmentationx:1.0.2' implementation 'androidx.cardview:cardview:1.0.0' implementation 'androidx.viewpager2:viewpager2:1.0.0' diff --git a/appx/src/main/java/com/zhengsr/tabhelper/activity/TabNoViewPagerActivity.java b/appx/src/main/java/com/zhengsr/tabhelper/activity/TabNoViewPagerActivity.java index 5dee3f5..8e7950f 100644 --- a/appx/src/main/java/com/zhengsr/tabhelper/activity/TabNoViewPagerActivity.java +++ b/appx/src/main/java/com/zhengsr/tabhelper/activity/TabNoViewPagerActivity.java @@ -11,6 +11,7 @@ import com.zhengsr.tabhelper.R; import com.zhengsr.tablib.FlowConstants; import com.zhengsr.tablib.bean.TabBean; +import com.zhengsr.tablib.bean.TabConfig; import com.zhengsr.tablib.bean.TabValue; import com.zhengsr.tablib.view.action.BaseAction; import com.zhengsr.tablib.view.adapter.TabFlowAdapter; @@ -37,6 +38,8 @@ protected void onCreate(Bundle savedInstanceState) { // mTitle.add("Kotlin"); setContentView(R.layout.activity_tab_no_view_pager); TabFlowLayout flowLayout = findViewById(R.id.new_test); + TabConfig config = new TabConfig.Builder() + .setTextId(R.id.item_text).build(); flowLayout.setAdapter(new TabFlowAdapter(R.layout.item_msg, mTitle) { @Override public void bindView(View view, String data, int position) { diff --git a/appx/src/main/res/layout/activity_tab.xml b/appx/src/main/res/layout/activity_tab.xml index 4b23766..30fb615 100644 --- a/appx/src/main/res/layout/activity_tab.xml +++ b/appx/src/main/res/layout/activity_tab.xml @@ -128,7 +128,7 @@ app:tab_type="rect" app:tab_height="2dp" app:tab_color="@color/colorAccent" - app:tab_margin_b="3dp" + app:tab_width_equals_text="true" android:layout_height="wrap_content"/> 0 && mTabBean!= null) { + if (parentView.getChildCount() > 0 && mTabBean != null) { mContext = mParentView.getContext(); mViewWidth = mParentView.getViewWidth(); int childCount = mParentView.getChildCount(); @@ -78,9 +79,9 @@ public void config(AbsFlowLayout parentView) { View child = mParentView.getChildAt(0); //初始化第一个view的效果 if (child != null) { - if (isVertical()){ + if (isVertical()) { mOffset = mTabBean.tabHeight * 1.0f / child.getMeasuredHeight(); - }else { + } else { mOffset = mTabBean.tabWidth * 1.0f / child.getMeasuredWidth(); } if (mTextViewId != -1) { @@ -90,7 +91,7 @@ public void config(AbsFlowLayout parentView) { TabColorTextView colorTextView = (TabColorTextView) textView; colorTextView.setTextColor(colorTextView.getChangeColor()); } - if (textView instanceof TextView){ + if (textView instanceof TextView) { isTextView = true; } @@ -99,7 +100,7 @@ public void config(AbsFlowLayout parentView) { child.setScaleX(mTabBean.scaleFactor); child.setScaleY(mTabBean.scaleFactor); } - mParentView.getAdapter().onItemSelectState(child,true); + mParentView.getAdapter().onItemSelectState(child, true); } } @@ -107,10 +108,9 @@ public void config(AbsFlowLayout parentView) { /** - * * @param config */ - public void setTabConfig(TabConfig config){ + public void setTabConfig(TabConfig config) { if (config != null) { mTextViewId = config.getTextId(); mSelectedColor = config.getSelectedColor(); @@ -127,6 +127,7 @@ public void setTabConfig(TabConfig config){ /** * 点击事件 + * * @param lastIndex * @param curIndex */ @@ -139,19 +140,18 @@ public void onItemClick(int lastIndex, int curIndex) { mLastIndex = lastIndex; if (!isViewPager()) { autoScaleView(); - doAnim(lastIndex, curIndex,mTabBean.tabClickAnimTime); + doAnim(lastIndex, curIndex, mTabBean.tabClickAnimTime); } else { if (Math.abs(mCurrentIndex - mLastIndex) > 1) { clearColorText(); isClickMore = true; autoScaleView(); - doAnim(lastIndex, curIndex,mTabBean.tabClickAnimTime); + doAnim(lastIndex, curIndex, mTabBean.tabClickAnimTime); } } } - /** * 为了防止 colortextView 滚动时的残留,先清掉 */ @@ -175,6 +175,7 @@ public void clearColorText() { } } + @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { /** @@ -207,24 +208,44 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse curView.setScaleY(curScale); } //左边偏移量 - float left = curView.getLeft() + positionOffset * (transView.getLeft() - curView.getLeft()); + float left ; //右边表示宽度变化 - float right = curView.getRight() + positionOffset * (transView.getRight() - curView.getRight()); + float right ; - if (mTabBean.tabWidth != -1) { - //拿到左边初始坐标 - int width = curView.getMeasuredWidth(); - //todo 动态改变rect大小的,后面再看看 - left = curView.getLeft() + (width - mTabBean.tabWidth) / 2; + int width = curView.getMeasuredWidth(); + int textWidth = 0; + if (mTabBean.tabWidth != -1){ + textWidth = mTabBean.tabWidth; + left = curView.getLeft() + (width - textWidth) * 1.0f / 2; //再拿到偏移坐标 left = left + positionOffset * (curView.getMeasuredWidth() + transView.getMeasuredWidth()) / 2; - right = left + mTabBean.tabWidth; + right = left + textWidth; + } else if (mTabBean.tabWidthEqualsText && mTabBean.tabType == FlowConstants.RECT) { + TextView transText = transView.findViewById(mTextViewId); + TextView curText = curView.findViewById(mTextViewId); + if (curText != null) { + int cw = (int) curText.getPaint().measureText(curText.getText().toString()); + textWidth = cw; + if (transText != null) { + int tw = (int) transText.getPaint().measureText(transText.getText().toString()); + textWidth += (int) ((tw - cw) * positionOffset); + } + } + left = curView.getLeft() + (width - textWidth) * 1.0f / 2; + //再拿到偏移坐标 + left = left + positionOffset * (curView.getMeasuredWidth() + transView.getMeasuredWidth()) / 2; + right = left + textWidth; + }else{ + left = curView.getLeft() + positionOffset * (transView.getLeft() - curView.getLeft()); + right = curView.getRight() + positionOffset * (transView.getRight() - curView.getRight()); } + + if (isSupportMargin()) { mTabRect.left = left + mTabBean.tabMarginLeft; mTabRect.right = right - mTabBean.tabMarginRight; - }else{ - mTabRect.left = left ; + } else { + mTabRect.left = left; mTabRect.right = right; } valueChange(new TabValue(mTabRect.left, mTabRect.right)); @@ -261,6 +282,7 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse } } } + @Override public void onPageSelected(int position) { mLastIndex = mCurrentIndex; @@ -278,20 +300,20 @@ public void onPageScrollStateChanged(int state) { * Viewpager 拿到 setCurrentItem(position) 中的position,赋值给当前的 mCurrentIndex; * 且两者之间大于1时,直接使用draw和动画效果;不再让 onPageScrolled 去执行动画,避免卡顿 */ - if (state == ViewPager.SCROLL_STATE_SETTLING){ - if (!isTabClick && mViewPager != null){ + if (state == ViewPager.SCROLL_STATE_SETTLING) { + if (!isTabClick && mViewPager != null) { mLastIndex = mCurrentIndex; mCurrentIndex = mViewPager.getCurrentItem(); - if (Math.abs(mCurrentIndex - mLastIndex) > 1){ + if (Math.abs(mCurrentIndex - mLastIndex) > 1) { isClickMore = true; clearColorText(); - doAnim(mLastIndex, mCurrentIndex,mTabBean.tabClickAnimTime); + doAnim(mLastIndex, mCurrentIndex, mTabBean.tabClickAnimTime); autoScaleView(); } } } - if (state == ViewPager.SCROLL_STATE_IDLE){ + if (state == ViewPager.SCROLL_STATE_IDLE) { isClickMore = false; isTabClick = false; } @@ -330,7 +352,7 @@ public void autoScaleView() { * @param lastIndex * @param curIndex */ - public void doAnim(int lastIndex, final int curIndex,int animTime) { + public void doAnim(int lastIndex, final int curIndex, int animTime) { if (mAnimator != null) { mAnimator.cancel(); @@ -342,8 +364,8 @@ public void doAnim(int lastIndex, final int curIndex,int animTime) { if (curView != null && lastView != null) { TabValue lastValue = getValue(lastView); TabValue curValue = getValue(curView); - if (isVertical()){ - if (mTabBean.tabHeight != -1){ + if (isVertical()) { + if (mTabBean.tabHeight != -1) { lastValue.top = mTabRect.top; lastValue.bottom = mTabRect.bottom; int height = curView.getMeasuredHeight(); @@ -351,11 +373,11 @@ public void doAnim(int lastIndex, final int curIndex,int animTime) { curValue.top = (height - mTabBean.tabHeight) / 2 + curView.getTop(); curValue.bottom = mTabBean.tabHeight + curValue.top; } - }else { + } else { + int width = curView.getMeasuredWidth(); if (mTabBean.tabWidth != -1) { lastValue.left = mTabRect.left; lastValue.right = mTabRect.right; - int width = curView.getMeasuredWidth(); if (mTabBean.tabType == FlowConstants.RECT) { curValue.left = (1 - mOffset) * width / 2 + curView.getLeft(); curValue.right = width * mOffset + curValue.left; @@ -363,6 +385,19 @@ public void doAnim(int lastIndex, final int curIndex,int animTime) { curValue.left = (width - mTabBean.tabWidth) / 2 + curView.getLeft(); curValue.right = mTabBean.tabWidth + curValue.left; } + }else if (mTabBean.tabWidthEqualsText && mTabBean.tabType == FlowConstants.RECT && + (mViewPager != null || mViewPager2 != null)){ + TextView curText = curView.findViewById(mTextViewId); + int textWidth = 0; + //todo 点击的也要支持 + if (curText != null) { + int cw = (int) curText.getPaint().measureText(curText.getText().toString()); + textWidth = cw; + float offset = textWidth * 1.0f / width; + curValue.left = (1 - offset) * width / 2 + curView.getLeft(); + curValue.right = curValue.left + textWidth; + } + } } @@ -391,9 +426,9 @@ public void onAnimationEnd(Animator animation) { if (child == null) { return; } - if (i == mCurrentIndex){ + if (i == mCurrentIndex) { adapter.onItemSelectState(child, true); - }else{ + } else { adapter.onItemSelectState(child, false); } } @@ -402,7 +437,7 @@ public void onAnimationEnd(Animator animation) { } }); mAnimator.start(); - }else{ + } else { if (mAnimator != null) { mAnimator.end(); mAnimator = null; @@ -444,10 +479,11 @@ public void chooseSelectedPosition(int position) { /** * 选中默认的颜色 + * * @param lastIndex * @param curIndex */ - public void chooseIndex(int lastIndex,int curIndex){ + public void chooseIndex(int lastIndex, int curIndex) { mCurrentIndex = curIndex; mLastIndex = lastIndex; if (mViewPager != null) { @@ -460,7 +496,7 @@ public void chooseIndex(int lastIndex,int curIndex){ if (mParentView != null) { View child = mParentView.getChildAt(mCurrentIndex); if (child != null) { - doAnim(mLastIndex,mCurrentIndex,0); + doAnim(mLastIndex, mCurrentIndex, 0); mOffset = mTabBean.tabWidth * 1.0f / child.getMeasuredWidth(); if (mTextViewId != -1) { View textView = child.findViewById(mTextViewId); @@ -469,8 +505,11 @@ public void chooseIndex(int lastIndex,int curIndex){ TabColorTextView colorTextView = (TabColorTextView) textView; colorTextView.setTextColor(colorTextView.getChangeColor()); } - if (textView instanceof TextView){ + if (textView instanceof TextView) { isTextView = true; + if (mSelectedColor != -2) { + ((TextView) textView).setTextColor(mSelectedColor); + } } } if (mTabBean.autoScale && mTabBean.scaleFactor > 1) { @@ -487,9 +526,9 @@ public void chooseIndex(int lastIndex,int curIndex){ /** * 清掉属性动画 */ - private void clearScale(){ + private void clearScale() { if (mParentView != null) { - if (mTabBean.autoScale && mTabBean.scaleFactor >1) { + if (mTabBean.autoScale && mTabBean.scaleFactor > 1) { int childCount = mParentView.getChildCount(); for (int i = 0; i < childCount; i++) { View view = mParentView.getChildAt(i); @@ -502,6 +541,7 @@ private void clearScale(){ /** * 获取 value 的数据 + * * @param view * @return */ @@ -515,7 +555,6 @@ private TabValue getValue(View view) { } - /** * item 偏移的变化率 * @@ -528,11 +567,12 @@ protected void valueChange(TabValue value) { /** * 拿到自定义属性 + * * @param bean */ public void configAttrs(TabBean bean) { mTabBean = bean; - if (bean.tabColor != -2){ + if (bean.tabColor != -2) { mPaint.setColor(bean.tabColor); } } @@ -554,34 +594,38 @@ public int getLastIndex() { /** * 是否是Viewpager + * * @return */ - private boolean isViewPager(){ + private boolean isViewPager() { return mViewPager != null || mViewPager2 != null; } + /** * tab 的方向 + * * @return */ - public boolean isVertical(){ + public boolean isVertical() { return mTabBean.tabOrientation == FlowConstants.VERTICAL; } - public boolean isLeftAction(){ + public boolean isLeftAction() { return mTabBean.actionOrientation != -1 && mTabBean.actionOrientation == FlowConstants.LEFT; } - public boolean isRightAction(){ + + public boolean isRightAction() { return mTabBean.actionOrientation != -1 && mTabBean.actionOrientation == FlowConstants.RIGHT; } - public void setContext(Context context){ + public void setContext(Context context) { mContext = context; } - private boolean isSupportMargin(){ - if (FlowConstants.RES == mTabBean.tabType|| - FlowConstants.ROUND == mTabBean.tabType - ){ + private boolean isSupportMargin() { + if (FlowConstants.RES == mTabBean.tabType || + FlowConstants.ROUND == mTabBean.tabType + ) { return true; } return false; diff --git a/tablibx/src/main/res/values/attrs.xml b/tablibx/src/main/res/values/attrs.xml index b60a237..1f8e6d4 100644 --- a/tablibx/src/main/res/values/attrs.xml +++ b/tablibx/src/main/res/values/attrs.xml @@ -20,6 +20,7 @@ +