From 4c50770d6dc66f3f2e6933a3c4a9adfdca6e794c Mon Sep 17 00:00:00 2001 From: zhengsr <845731923@qq.com> Date: Tue, 1 Mar 2022 11:04:06 +0800 Subject: [PATCH] =?UTF-8?q?2.0=E7=89=88=E6=9C=AC=201.=20=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=9D=9E=20Viewpager=20=E6=97=B6=EF=BC=8C=E9=80=89=E4=B8=AD?= =?UTF-8?q?=E4=B8=8D=E6=9B=B4=E6=8D=A2=E9=A2=9C=E8=89=B2=202.=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20TextConfig=20=E7=94=A8=E4=BA=8E=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=20TextView=20=E7=9A=84=E4=B8=80=E4=BA=9B=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 2 +- README.md | 1 + TAB_README.md | 124 +++++++++--------- .../tabhelper/activity/NetTestActivity.java | 7 - .../tabhelper/activity/TabActivity.java | 72 +++++----- .../activity/TabNoViewPagerActivity.java | 5 +- .../com/zhengsr/tablib/bean/TabConfig.java | 42 +++--- .../com/zhengsr/tablib/bean/TextConfig.java | 51 +++++++ .../zhengsr/tablib/view/TabColorTextView.java | 7 + .../tablib/view/action/BaseAction.java | 14 +- .../tablib/view/flow/TabFlowLayout.java | 1 + .../tablib/view/flow/base/AbsFlowLayout.java | 76 +++++------ 12 files changed, 227 insertions(+), 175 deletions(-) create mode 100644 tablibx/src/main/java/com/zhengsr/tablib/bean/TextConfig.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 259528b..351f17f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,7 +5,7 @@ - + diff --git a/README.md b/README.md index 166d6f2..cfa0bf9 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,7 @@ implementation 'com.github.LillteZheng:FlowHelper:v1.37' ## 版本信息: +- v2.0 : 内置部分控件,减少接入成本,并优化一些bug,和关闭demo ,viewpager 内存泄露的问题 - v1.37 : 增加 tab_width_equals_text ,让 rect 根据 text 的长度变化,修复 TabColorTextView 加粗不起作用的问题 - v1.32 : 修复res和round,tab_margin_x 不起作用的问题 - v1.30 : 重构分离了ViewPager和非ViewPager的情况,并处理数据增加减少时,notifyDataChange()导致布局混乱的问题 diff --git a/TAB_README.md b/TAB_README.md index 88ed830..94c9373 100644 --- a/TAB_README.md +++ b/TAB_README.md @@ -80,48 +80,53 @@ tab_type 可以填 tri ,rect,round 等类型 **Java** -那么,在 xml 写好了,接着,就是在 Activity 中,这样写: +TabFlowLayout 支持TextView 和 TabColorTextView 两种,可以通知 TabConfig 配置,简易版本如下: ``` -private void rectFlow(){ +private void configFlow(){ TabFlowLayout flowLayout = findViewById(R.id.rectflow); - //设置数据,这里以 setAdapter 的形式 - flowLayout.setAdapter(new TabFlowAdapter(R.layout.item_msg,mTitle) { - @Override - public void onItemSelectState(View view, boolean isSelected) { - super.onItemSelectState(view, isSelected); - //选中时,可以改变不同颜色,如果你的background 为 selector,可以不写这个 - if (isSelected){ - setTextColor(view,R.id.item_text,Color.WHITE); - }else{ - setTextColor(view,R.id.item_text,getResources().getColor(R.color.unselect)); - } - } - - @Override - public void bindView(View view, String data, int position) { - /** - * 绑定数据,可以使用 setText(..) 等快捷方式,也可以视同 view.findViewById() - * 同时,当你的子控件需要点击事件时,可以通过 addChildrenClick() 注册事件, - * 然后重写 onItemChildClick(..) 即可拿到事件,否则就自己写。 - * 自己的点击和长按不需要注册 - */ - setText(view,R.id.item_text,data) - .setTextColor(view,R.id.item_text,getResources().getColor(R.color.unselect)); - if (position == 0){ - setVisible(view,R.id.item_msg,true); - } - - // 注册子控件的点击事件 - //addChildrenClick(view,R.id.item_text,position); - //注册子控件的长按事件 - //addChildrenLongClick(view,R.id.item_text,position); - - } - }); + //1. 默认是 TextView 模式,只需要配置 title 的数据即可,不需要其他操作,当然这个 List 是 String 类型的 + TabConfig config = new TabConfig.Builder() + .setViewpager(mViewPager) + .setSelectedColor(Color.WHITE) + .setUnSelectColor(getResources().getColor(R.color.unselect)) + .build(); + flowLayout.setAdapter(config, new TabFlowAdapter<>(mTitle)); + + //2. 也可以配置成颜色渐变模式,比如你还想设置TextView的大小,粗细等,如下: + TextConfig textConfig = new TextConfig() + .setPadding(l, t, l, t) + .setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)) + .setTextSize(18); + + TabConfig config = new TabConfig.Builder() + .setViewpager(mViewPager) + .setDefaultTextType(FlowConstants.COLORTEXT) + .setTextConfig(textConfig) + .setSelectedColor(getResources().getColor(R.color.colorAccent)) + .setUnSelectColor(getResources().getColor(R.color.unselect)) + .build(); + + flowLayout.setAdapter(config, new TabFlowAdapter<>(mTitle)); + + //3. 如果你觉得默认的不满足你的需求,setAdapter 也支持传入自定义的 layout ,如: + TabConfig config = new TabConfig.Builder() + .setViewPager(mViewPager) + .setTextId(R.id.item_text) + .setDefaultPos(2) + .setVisibleCount(4) + .build(); + + flowLayout.setAdapter(config,adapter = new TabFlowAdapter(R.layout.item_tab,datas) { + + @Override + public void bindView(View view, NavData data, int position) { + setText(view,R.id.item.text,data.getTitle(); + } + }); } ``` -可以看到,只需要设置 adapter 就行了,需要注意的是你要传入子控件的 layout,这样方便你自定义你的布局,比如一个TextView 和一个红点点。 +可以看到,只需要设置 adapter 就行了,也可以传入 layout,这样方便你自定义你的布局,比如一个TextView 和一个红点点。 **如果需要数据更新,使用adapter.notifyDataChanged() 即可** @@ -129,7 +134,7 @@ private void rectFlow(){ [实现一个可定制化的TabFlowLayout(三) -- 动态数据添加与常用接口封装](https://blog.csdn.net/u011418943/article/details/103817967) -如果你需要使用颜色渐变的效果,蒋xml 的TextView 换成 TabColorTextView 就可以了,比如: +如果你需要使用颜色渐变的效果,直接看上面的第二种配置,如果不满足,也可以自定义 xml ,比如: ``` (R.layout.item_msg,mTitle) { - @Override - public void bindView(View view, String data, int position) { - setText(view,R.id.item_text,data); - } - - @Override - public void onItemClick(View view, String data, int position) { - super.onItemClick(view, data, position); - mViewPager.setCurrentItem(position); - } - }); + flowLayout.setAdapter(new TabFlowAdapter<>(mTitle)); } ``` @@ -362,6 +361,9 @@ private void resFlow(){ |tab_isAutoScroll|boolean|是否支持自动滚动,默认为true| |tab_visual_count|integer|可视化个数,比如有一排,我们就只要显示4个,此时宽度均分| |tab_width_equals_text|boolean|rect 是否根据text的大小来,目前只支持rect和带viewpager和不放大的情况| +|tab_default_textType|enum|默认的控件,normal - TextView,color - TabColorTextView| +|tab_text_select_color|color|选择颜色| +|tab_text_unselect_color|color|未选择颜色| **TabColorTextView** diff --git a/appx/src/main/java/com/zhengsr/tabhelper/activity/NetTestActivity.java b/appx/src/main/java/com/zhengsr/tabhelper/activity/NetTestActivity.java index 401a879..e5bdecc 100644 --- a/appx/src/main/java/com/zhengsr/tabhelper/activity/NetTestActivity.java +++ b/appx/src/main/java/com/zhengsr/tabhelper/activity/NetTestActivity.java @@ -62,13 +62,6 @@ protected void onCreate(Bundle savedInstanceState) { public void bindView(View view, String data, int position) { setDefaultText(view,data); } - - @Override - public void onItemClick(View view, String data, int position) { - super.onItemClick(view, data, position); - mViewPager.setCurrentItem(position); - } - }); diff --git a/appx/src/main/java/com/zhengsr/tabhelper/activity/TabActivity.java b/appx/src/main/java/com/zhengsr/tabhelper/activity/TabActivity.java index 8e641cc..3422a5c 100644 --- a/appx/src/main/java/com/zhengsr/tabhelper/activity/TabActivity.java +++ b/appx/src/main/java/com/zhengsr/tabhelper/activity/TabActivity.java @@ -2,6 +2,7 @@ import android.graphics.Canvas; import android.graphics.Color; +import android.graphics.Typeface; import android.os.Bundle; import android.view.View; @@ -18,6 +19,9 @@ import com.zhengsr.tablib.bean.TabBean; import com.zhengsr.tablib.bean.TabConfig; import com.zhengsr.tablib.bean.TabValue; +import com.zhengsr.tablib.bean.TextConfig; +import com.zhengsr.tablib.utils.DisplayUtil; +import com.zhengsr.tablib.view.TabColorTextView; import com.zhengsr.tablib.view.action.BaseAction; import com.zhengsr.tablib.view.adapter.TabFlowAdapter; import com.zhengsr.tablib.view.flow.TabVpFlowLayout; @@ -34,6 +38,7 @@ public class TabActivity extends BaseActivity { private ViewPager2 mViewPager; private CusAdapter2 mViewAdapter; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -47,37 +52,35 @@ protected void onCreate(Bundle savedInstanceState) { rectFlow(); triFlow(); roundFlow(); - resFlow(); - colorFlow(); + resFlow(); + colorFlow(); cusFlow(); } boolean isDetele = false; - private void rectFlow(){ + + private void rectFlow() { final TabVpFlowLayout flowLayout = findViewById(R.id.rectflow); - // flowLayout.setViewPager(mViewPager,R.id.item_text,getResources().getColor(R.color.unselect),Color.WHITE); TabConfig config = new TabConfig.Builder() .setViewpager(mViewPager) .setSelectedColor(Color.WHITE) .setUnSelectColor(getResources().getColor(R.color.unselect)) .build(); - - - - flowLayout.setAdapter(config,new TabFlowAdapter<>(mTitle)); + flowLayout.setAdapter(config, new TabFlowAdapter<>(mTitle)); TabVpFlowLayout flowLayout2 = findViewById(R.id.rectflow2); - flowLayout2.setAdapter(config,new TabFlowAdapter<>(mTitle)); + flowLayout2.setAdapter(config, new TabFlowAdapter<>(mTitle)); } - private void triFlow(){ + private void triFlow() { TabVpFlowLayout flowLayout = findViewById(R.id.triflow); flowLayout.setViewPager(mViewPager); flowLayout.setAdapter(new TabFlowAdapter(mTitle)); } - private void roundFlow(){ + + private void roundFlow() { TabVpFlowLayout flowLayout = findViewById(R.id.roundflow); TabConfig config = new TabConfig.Builder() @@ -87,14 +90,15 @@ private void roundFlow(){ .setUnSelectColor(getResources().getColor(R.color.unselect)) .build(); flowLayout.setTabConfig(config); - flowLayout.setAdapter(new TabFlowAdapter(R.layout.item_msg,mTitle) { + flowLayout.setAdapter(new TabFlowAdapter(R.layout.item_msg, mTitle) { @Override public void bindView(View view, String data, int position) { - setText(view,R.id.item_text,data); + setText(view, R.id.item_text, data); } }); } - private void resFlow(){ + + private void resFlow() { final TabVpFlowLayout flowLayout = findViewById(R.id.resflow); /** @@ -116,35 +120,31 @@ private void resFlow(){ flowLayout.setTabBean(bean); flowLayout.setViewPager(mViewPager); - flowLayout.setAdapter(new TabFlowAdapter(mTitle) ); + flowLayout.setAdapter(new TabFlowAdapter(mTitle)); } - private void colorFlow(){ + private void colorFlow() { TabVpFlowLayout flowLayout = findViewById(R.id.colorflow); + int l = DisplayUtil.dip2px(this, 12); + int t = DisplayUtil.dip2px(this, 6); + TextConfig textConfig = new TextConfig() + .setPadding(l, t, l, t) + .setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)) + .setTextSize(18); + TabConfig config = new TabConfig.Builder() .setViewpager(mViewPager) .setDefaultTextType(FlowConstants.COLORTEXT) + .setTextConfig(textConfig) .setSelectedColor(getResources().getColor(R.color.colorAccent)) .setUnSelectColor(getResources().getColor(R.color.unselect)) - .setDefaultTextSize(16) .build(); - flowLayout.setAdapter(config,new TabFlowAdapter<>(mTitle)); - /*flowLayout.setAdapter(config,new TabFlowAdapter(R.layout.item_color_msg,mTitle) { - @Override - public void bindView(View view, String data, int position) { - setText(view,R.id.item_text,data); - } + flowLayout.setAdapter(config, new TabFlowAdapter<>(mTitle)); - @Override - public void onItemClick(View view, String data, int position) { - super.onItemClick(view, data, position); - mViewPager.setCurrentItem(position); - } - });*/ } - private void cusFlow(){ + private void cusFlow() { TabVpFlowLayout flowLayout = findViewById(R.id.cusflow); flowLayout.setCusAction(new CircleAction()); flowLayout.setViewPager(mViewPager); @@ -160,11 +160,11 @@ public void config(AbsFlowLayout parentView) { super.config(parentView); View child = parentView.getChildAt(0); if (child != null) { - float l = parentView.getPaddingLeft() + child.getMeasuredWidth()/2; - float t = parentView.getPaddingTop() + child.getMeasuredHeight() - mTabBean.tabHeight/2 -mTabBean.tabMarginBottom; + float l = parentView.getPaddingLeft() + child.getMeasuredWidth() / 2; + float t = parentView.getPaddingTop() + child.getMeasuredHeight() - mTabBean.tabHeight / 2 - mTabBean.tabMarginBottom; float r = mTabBean.tabWidth + l; float b = child.getMeasuredHeight() - mTabBean.tabMarginBottom; - mTabRect.set(l,t,r,b); + mTabRect.set(l, t, r, b); } } @@ -173,16 +173,16 @@ public void config(AbsFlowLayout parentView) { protected void valueChange(TabValue value) { super.valueChange(value); //由于自定义的,都是从left 开始算起的,所以这里还需要加上圆的半径 - mTabRect.left = value.left + mTabBean.tabWidth/2; + mTabRect.left = value.left + mTabBean.tabWidth / 2; } @Override public void draw(Canvas canvas) { - canvas.drawCircle(mTabRect.left, mTabRect.top,mTabBean.tabWidth/2,mPaint); + canvas.drawCircle(mTabRect.left, mTabRect.top, mTabBean.tabWidth / 2, mPaint); } } - class CusAdapter2 extends FragmentStateAdapter{ + class CusAdapter2 extends FragmentStateAdapter { public CusAdapter2(@NonNull FragmentActivity fragmentActivity) { super(fragmentActivity); 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 7eef28b..35e19e3 100644 --- a/appx/src/main/java/com/zhengsr/tabhelper/activity/TabNoViewPagerActivity.java +++ b/appx/src/main/java/com/zhengsr/tabhelper/activity/TabNoViewPagerActivity.java @@ -35,11 +35,11 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mTitle.add("Java"); mTitle.add("Android"); - // 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(); + .setTextId(R.id.item_text) + .build(); flowLayout.setAdapter(new TabFlowAdapter( mTitle) { @Override public void bindView(View view, String data, int position) { @@ -86,7 +86,6 @@ private void rectFlow() { flowLayout.setAdapter(mAdapter); TabFlowLayout flowLayout2 = findViewById(R.id.rectflow2); - flowLayout2.setAdapter(new TabFlowAdapter( mTitle3)); } diff --git a/tablibx/src/main/java/com/zhengsr/tablib/bean/TabConfig.java b/tablibx/src/main/java/com/zhengsr/tablib/bean/TabConfig.java index 9779bee..3c18d4a 100644 --- a/tablibx/src/main/java/com/zhengsr/tablib/bean/TabConfig.java +++ b/tablibx/src/main/java/com/zhengsr/tablib/bean/TabConfig.java @@ -1,6 +1,7 @@ package com.zhengsr.tablib.bean; import android.graphics.Rect; +import android.widget.TextView; import androidx.annotation.ColorInt; import androidx.annotation.IdRes; @@ -8,6 +9,7 @@ import androidx.viewpager2.widget.ViewPager2; import com.zhengsr.tablib.FlowConstants; +import com.zhengsr.tablib.view.TabColorTextView; /** * @author by zhengshaorui 2021/5/21 08:53 @@ -47,12 +49,12 @@ public int getVisibleCount() { return builder.visibleCount; } - public Rect getTextPaddingRect(){ - return builder.rect; + public TextConfig getTextConfig(){ + return builder.textConfig; } - public float getTextSize(){ - return builder.textSize; + public Builder getBuilder() { + return builder; } public boolean isUseColorText(){ @@ -73,9 +75,8 @@ public static class Builder{ private ViewPager viewPager; private ViewPager2 viewpager2; private int visibleCount=-1; - private Rect rect; - private float textSize; private boolean useColorText; + private TextConfig textConfig; public Builder setViewPager(ViewPager viewPager){ this.viewPager = viewPager; return this; @@ -118,38 +119,28 @@ public Builder setVisibleCount(int visibleCount){ return this; } - /** - * 不设置 layout 时,设置TextView pading的宽高,此方法只针对不设置 layout 的情况 - * @param l 默认 10 dp - * @param t 默认 6 dp - * @param r 默认 6 dp - * @param b 默认 10 dp - * @returnt - */ - public Builder setDefaultTextPadding(int l, int t, int r, int b) { - rect = new Rect(l, t, r, b); - return this; - } /** * 不设置 layout ,默认 TextView 的大小 - * @param size ,默认14 sp + * @param type {@link FlowConstants#NORMALTEXT} 或者 {@link FlowConstants#COLORTEXT} * @return */ - public Builder setDefaultTextSize(float size) { - textSize = size; + public Builder setDefaultTextType(int type) { + useColorText = type == FlowConstants.COLORTEXT; return this; } + /** - * 不设置 layout ,默认 TextView 的大小 - * @param type {@link FlowConstants#NORMALTEXT} 或者 {@link FlowConstants#COLORTEXT} + * 设置 TextView 的通用属性 + * @param config * @return */ - public Builder setDefaultTextType(int type) { - useColorText = type == FlowConstants.COLORTEXT; + public Builder setTextConfig(TextConfig config) { + this.textConfig = config; return this; } + public TabConfig build(){ //判断参数 if (viewpager2 != null && viewPager != null){ @@ -169,7 +160,6 @@ public String toString() { ", viewPager=" + viewPager + ", viewpager2=" + viewpager2 + ", visibleCount=" + visibleCount + - ", rect=" + rect + '}'; } } diff --git a/tablibx/src/main/java/com/zhengsr/tablib/bean/TextConfig.java b/tablibx/src/main/java/com/zhengsr/tablib/bean/TextConfig.java new file mode 100644 index 0000000..30154b1 --- /dev/null +++ b/tablibx/src/main/java/com/zhengsr/tablib/bean/TextConfig.java @@ -0,0 +1,51 @@ +package com.zhengsr.tablib.bean; + +import android.graphics.Rect; +import android.graphics.Typeface; + +import androidx.annotation.ColorInt; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author by zhengshaorui 2022/3/1 + * describe:用于配置 TextView 相关的一些属性 + */ +public class TextConfig { + private Rect rect; + private List list; + private float size = 0; + public TextConfig setPadding(int l,int t,int r,int b){ + if (rect == null) { + rect = new Rect(); + } + rect.set(l,t,r,b); + return this; + } + public TextConfig setTypeface(Typeface tf){ + if (list == null) { + list = new ArrayList<>(); + } + list.add(tf); + return this; + } + public TextConfig setTextSize(float size){ + this.size = size; + return this; + } + + + public Rect getPadding() { + return rect; + } + + public List getTypefaces() { + return list; + } + + public float getTextSize() { + return size; + } + +} diff --git a/tablibx/src/main/java/com/zhengsr/tablib/view/TabColorTextView.java b/tablibx/src/main/java/com/zhengsr/tablib/view/TabColorTextView.java index 4fedec2..699a67b 100644 --- a/tablibx/src/main/java/com/zhengsr/tablib/view/TabColorTextView.java +++ b/tablibx/src/main/java/com/zhengsr/tablib/view/TabColorTextView.java @@ -10,6 +10,7 @@ import android.util.Log; import android.view.Gravity; +import androidx.annotation.NonNull; import androidx.appcompat.widget.AppCompatTextView; import com.zhengsr.tablib.R; @@ -157,4 +158,10 @@ public int getChangeColor() { public int getDefaultColor() { return mDefaultColor; } + + @NonNull + @Override + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } } diff --git a/tablibx/src/main/java/com/zhengsr/tablib/view/action/BaseAction.java b/tablibx/src/main/java/com/zhengsr/tablib/view/action/BaseAction.java index d77fead..8f63837 100644 --- a/tablibx/src/main/java/com/zhengsr/tablib/view/action/BaseAction.java +++ b/tablibx/src/main/java/com/zhengsr/tablib/view/action/BaseAction.java @@ -241,12 +241,11 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse mParentView.postInvalidate(); - Log.d(TAG, "zsr onPageScrolled: "+needSmooth); if (needSmooth) { //处理颜色渐变 if (isColorText) { TextView leftView = mParentView.getTextView(position); - TextView rightView = mParentView.getTextView(position+1); + TextView rightView = mParentView.getTextView(position + 1); TabColorTextView colorLeft = (TabColorTextView) leftView; TabColorTextView colorRight = (TabColorTextView) rightView; colorLeft.setprogress(1 - positionOffset, TabColorTextView.DEC_RIGHT); @@ -304,7 +303,6 @@ public void onPageScrollStateChanged(int state) { * Viewpager 拿到 setCurrentItem(position) 中的position,赋值给当前的 mCurrentIndex; * 且两者之间大于1时,直接使用draw和动画效果;不再让 onPageScrolled 去执行动画,避免卡顿 */ - Log.d(TAG, "zsr onPageScrollStateChanged: "+isTabClick+" "+state); if (state == ViewPager.SCROLL_STATE_SETTLING) { if (!isTabClick && (mViewPager != null || mViewPager2 != null)) { mLastIndex = mCurrentIndex; @@ -438,8 +436,14 @@ public void onAnimationEnd(Animator animation) { return; } if (i == mCurrentIndex) { + if (isTextView && !isColorText && mSelectedColor != FlowConstants.COLOR_ILLEGAL) { + mParentView.getTextView(i).setTextColor(mSelectedColor); + } adapter.onItemSelectState(child, true); } else { + if (isTextView && !isColorText && mUnSelectedColor != FlowConstants.COLOR_ILLEGAL) { + mParentView.getTextView(i).setTextColor(mUnSelectedColor); + } adapter.onItemSelectState(child, false); } } @@ -644,7 +648,9 @@ private boolean isSupportMargin() { } return false; } - private boolean needSmooth; + + private boolean needSmooth = true; + public void updatePos(int lastIndex, int curIndex) { needSmooth = Math.abs(curIndex - lastIndex) <= SMOOTH_Threshold; } diff --git a/tablibx/src/main/java/com/zhengsr/tablib/view/flow/TabFlowLayout.java b/tablibx/src/main/java/com/zhengsr/tablib/view/flow/TabFlowLayout.java index 467ec4a..8eb1e16 100644 --- a/tablibx/src/main/java/com/zhengsr/tablib/view/flow/TabFlowLayout.java +++ b/tablibx/src/main/java/com/zhengsr/tablib/view/flow/TabFlowLayout.java @@ -135,6 +135,7 @@ private void chooseItem(int position, View view) { */ updateScroll(view, true); postInvalidate(); + } @Override diff --git a/tablibx/src/main/java/com/zhengsr/tablib/view/flow/base/AbsFlowLayout.java b/tablibx/src/main/java/com/zhengsr/tablib/view/flow/base/AbsFlowLayout.java index 5132438..1daedbd 100644 --- a/tablibx/src/main/java/com/zhengsr/tablib/view/flow/base/AbsFlowLayout.java +++ b/tablibx/src/main/java/com/zhengsr/tablib/view/flow/base/AbsFlowLayout.java @@ -5,6 +5,7 @@ import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Rect; +import android.graphics.Typeface; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; @@ -23,6 +24,7 @@ import com.zhengsr.tablib.R; import com.zhengsr.tablib.bean.TabBean; import com.zhengsr.tablib.bean.TabConfig; +import com.zhengsr.tablib.bean.TextConfig; import com.zhengsr.tablib.callback.FlowListener; import com.zhengsr.tablib.callback.FlowListenerAdapter; import com.zhengsr.tablib.utils.AttrsUtils; @@ -52,7 +54,7 @@ public class AbsFlowLayout extends ScrollFlowLayout { */ protected BaseAction mAction; private TabConfig mTabConfig; - private TabFlowAdapter mAdapter; + protected TabFlowAdapter mAdapter; protected Scroller mScroller; protected int mLastScrollPos = 0; protected int mLastIndex = 0; @@ -456,51 +458,25 @@ public TextView getTextView(int pos){ private TextView getTextview(int pos,int textType){ boolean useColorText = false; + TextView textView ; if (mTabConfig != null) { useColorText = mTabConfig.isUseColorText(); } if (textType == FlowConstants.COLORTEXT || useColorText) { - TabColorTextView textView = new TabColorTextView(getContext()); - int l = DisplayUtil.dip2px(getContext(), 10); - int t = DisplayUtil.dip2px(getContext(), 6); - textView.setPadding(l, t, l, t); - textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); - if (mTabConfig != null) { - if (mTabConfig.getTextPaddingRect() != null) { - Rect rect = mTabConfig.getTextPaddingRect(); - textView.setPadding(rect.left,rect.top,rect.right,rect.bottom); - } - if (mTabConfig.getTextSize() != 0) { - textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,mTabConfig.getTextSize()); - } - } - textView.setCusTextColor(Color.GRAY,Color.RED); + TabColorTextView colorTextView = new TabColorTextView(getContext()); + + colorTextView.setCusTextColor(Color.GRAY,Color.RED); if (mTabBean != null) { if (mTabBean.selectedColor != FlowConstants.COLOR_ILLEGAL && mTabBean.unSelectedColor != FlowConstants.COLOR_ILLEGAL) { - textView.setCusTextColor(mTabBean.unSelectedColor,mTabBean.selectedColor); - } - } - textView.setGravity(Gravity.CENTER); - return textView; - - }else { - TextView textView = new TextView(getContext()); - int l = DisplayUtil.dip2px(getContext(), 10); - int t = DisplayUtil.dip2px(getContext(), 6); - textView.setPadding(l, t, l, t); - if (mTabConfig != null) { - if (mTabConfig.getTextPaddingRect() != null) { - Rect rect = mTabConfig.getTextPaddingRect(); - textView.setPadding(rect.left,rect.top,rect.right,rect.bottom); - } - if (mTabConfig.getTextSize() != 0) { - textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,mTabConfig.getTextSize()); + colorTextView.setCusTextColor(mTabBean.unSelectedColor,mTabBean.selectedColor); } } + textView = colorTextView; - textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); - textView.setGravity(Gravity.CENTER); + + }else { + textView = new TextView(getContext()); textView.setTextColor(Color.BLACK); if (mTabBean != null) { if (mTabBean.selectedColor != FlowConstants.COLOR_ILLEGAL) { @@ -511,8 +487,34 @@ private TextView getTextview(int pos,int textType){ } } } - return textView; + + } + textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); + textView.setGravity(Gravity.CENTER); + int l = DisplayUtil.dip2px(getContext(), 10); + int t = DisplayUtil.dip2px(getContext(), 6); + textView.setPadding(l, t, l, t); + textView.setGravity(Gravity.CENTER); + + if (mTabConfig != null) { + TextConfig config = mTabConfig.getTextConfig(); + if (config != null) { + if (config.getPadding() != null) { + Rect rect = config.getPadding(); + textView.setPadding(rect.left, rect.top, rect.right, rect.bottom); + } + if (config.getTextSize() != 0) { + textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, config.getTextSize()); + } + if (config.getTypefaces() != null) { + for (Typeface typeface : config.getTypefaces()) { + textView.setTypeface(typeface); + } + } + } + } + return textView; } }