Skip to content

Commit

Permalink
release 1.2.3 修复红点的显示位置;bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
li504799868 committed Jul 21, 2020
1 parent bb2878d commit a074ee3
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 50 deletions.
2 changes: 1 addition & 1 deletion FlycoTabLayout_Lib/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release'

version = "1.2.2"
version = "1.2.3"
android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ public class SlidingScaleTabLayout extends HorizontalScrollView implements ViewP
private int mTabMarginTop;
private int mTabMarginBottom;

private int mTabMsgMarginTop;
private int mTabMsgMarginRight;
private int mTabDotMarginTop;
private int mTabDotMarginRight;

private boolean openDmg = true;

/**
Expand Down Expand Up @@ -224,6 +229,13 @@ private void obtainAttributes(Context context, AttributeSet attrs) {

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

openDmg = ta.getBoolean(R.styleable.SlidingScaleTabLayout_tl_openTextDmg, false);
ta.recycle();

Expand Down Expand Up @@ -306,7 +318,7 @@ public void notifyDataSetChanged() {
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, this, false);
tabView = LayoutInflater.from(mContext).inflate(R.layout.layout_scale_tab, mTabsContainer, false);
TextView title = tabView.findViewById(R.id.tv_tab_title);
// 设置tab的位置信息
setTabLayoutParams(title);
Expand Down Expand Up @@ -342,7 +354,7 @@ private void setTabLayoutParams(TextView title) {
title.setLayoutParams(params);

if (isDmgOpen()) {
ImageView imageView = (ImageView) ViewUtils.findBrotherView(title, R.id.tv_tav_title_dmg, 3);
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;
Expand Down Expand Up @@ -379,19 +391,6 @@ private boolean isDmgOpen() {
return openDmg && mTextSelectSize != mTextUnSelectSize;
}

public void addNewTab(String title) {
View tabView = View.inflate(mContext, R.layout.layout_scale_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
*/
Expand Down Expand Up @@ -470,7 +469,7 @@ else if (mTextBold == TEXT_BOLD_WHEN_SELECT) {
private void generateTitleDmg(View tabView, TextView textView, int position) {
// 如果需要开启镜像,需要把所有的字设置为选中的字体
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextUnSelectSize);
ImageView imageView = tabView.findViewById(R.id.tv_tav_title_dmg);
ImageView imageView = tabView.findViewById(R.id.tv_tab_title_dmg);
imageView.setImageBitmap(ViewUtils.generateViewCacheBitmap(textView));
imageView.setMaxWidth(imageView.getDrawable().getIntrinsicWidth());
// iTabScaleTransformer.setNormalWidth(position, imageView.getDrawable().getIntrinsicWidth(), position == mViewPager.getCurrentItem());
Expand Down Expand Up @@ -988,11 +987,30 @@ public void showMsg(int position, int num) {
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;
}

setMsgMargin(position, 4, 2);
mInitSetMap.put(position, true);
}
}
Expand Down Expand Up @@ -1024,30 +1042,6 @@ public void hideMsg(int position) {
}
}

/**
* 设置未读消息偏移,原点为文字的右上角.当控件高度固定,消息提示位置易控制,显示效果佳
*/
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);
ImageView imageView = tabView.findViewById(R.id.tv_tav_title_dmg);
// float textWidth = mTextPaint.measureText(tv_tab_title.getText().toString());
float textWidth = imageView.getWidth();
// float textHeight = mTextPaint.descent() - mTextPaint.ascent();
float textHeight = imageView.getHeight();
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对象从而各种设置
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void run() {

private int changTabDmgWidth(int position, float scale, int leftWidth) {
final TextView currentTab = slidingScaleTabLayout.getTitle(position);
final ImageView currentTabDmg = (ImageView) ViewUtils.findBrotherView(currentTab, R.id.tv_tav_title_dmg, 3);
final ImageView currentTabDmg = (ImageView) ViewUtils.findBrotherView(currentTab, R.id.tv_tab_title_dmg, 3);
if (currentTabDmg == null) return 0;
ViewGroup.LayoutParams params = currentTabDmg.getLayoutParams();
if (leftWidth != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public static void show(MsgView msgView, int num) {
msgView.setLayoutParams(lp);
} else {
lp.height = (int) (18 * dm.density);
if (num > 0 && num < 10) {//圆
if (num < 10) {//圆
lp.width = (int) (18 * dm.density);
msgView.setText(num + "");
} else if (num > 9 && num < 100) {//圆角矩形,圆角是高度的一半,设置默认padding
} 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 + "");
Expand Down
7 changes: 5 additions & 2 deletions FlycoTabLayout_Lib/src/main/res/layout/layout_scale_tab.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
Expand All @@ -11,10 +12,11 @@
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:includeFontPadding="false"
tools:text="111"
android:singleLine="true" />

<ImageView
android:id="@+id/tv_tav_title_dmg"
android:id="@+id/tv_tab_title_dmg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
Expand All @@ -30,6 +32,7 @@
mv:mv_backgroundColor="#FD481F"
mv:mv_isRadiusHalfHeight="true"
mv:mv_strokeColor="#ffffff"
mv:mv_strokeWidth="1dp" />
mv:mv_strokeWidth="1dp"
tools:visibility="visible" />

</RelativeLayout>
21 changes: 21 additions & 0 deletions FlycoTabLayout_Lib/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@
<attr name="tl_tab_marginTop" format="dimension" />
<attr name="tl_tab_marginBottom" format="dimension" />

<attr name="tl_tab_msg_marginTop" format="dimension" />
<attr name="tl_tab_msg_marginRight" format="dimension" />

<attr name="tl_tab_dot_marginTop" format="dimension" />
<attr name="tl_tab_dot_marginRight" format="dimension" />

<!-- tab的位置 -->
<attr name="tl_tab_gravity" format="enum">
<enum name="Top" value="0" />
<enum name="Bottom" value="1" />
<enum name="Center" value="2" />
</attr>

<!-- tab的竖直位置 -->
<attr name="tl_tab_vertical_gravity" format="enum">
<enum name="Top" value="0" />
Expand Down Expand Up @@ -276,6 +289,14 @@
<!-- 是否开启文字的图片镜像 -->
<attr name="tl_openTextDmg" format="boolean" />

<!-- 未读消息的位置 -->
<attr name="tl_tab_msg_marginTop" />
<attr name="tl_tab_msg_marginRight" />

<!-- 红点的位置 -->
<attr name="tl_tab_dot_marginTop" />
<attr name="tl_tab_dot_marginRight" />

</declare-styleable>

<declare-styleable name="MsgView">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ protected void onCreate(Bundle savedInstanceState) {
viewPager.setOffscreenPageLimit(4);
tabLayout.setViewPager(viewPager);
viewPager.setCurrentItem(2);

tabLayout.showDot(2);
tabLayout.showMsg(0, 99);
}

class MyViewPagerAdapter extends PagerAdapter {
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@
app:tl_indicator_height="2dp"
app:tl_indicator_width="7dp"
app:tl_openTextDmg="true"
app:tl_tab_horizontal_gravity="Left"

app:tl_tab_dot_marginRight="-5dp"
app:tl_tab_dot_marginTop="-5dp"

app:tl_tab_horizontal_gravity="Left"
app:tl_tab_marginBottom="8dp"

app:tl_tab_msg_marginRight="-10dp"
app:tl_tab_msg_marginTop="-10dp"

app:tl_tab_padding="15dp"

app:tl_tab_vertical_gravity="Bottom"
app:tl_textBold="SELECT"

app:tl_textSelectColor="@color/colorAccent"
app:tl_textSelectSize="20sp"

app:tl_textUnSelectColor="@color/colorPrimaryDark"
app:tl_textUnSelectSize="14sp" />

Expand Down

0 comments on commit a074ee3

Please sign in to comment.