Skip to content

Commit

Permalink
Merge pull request #4 from chnyangjie/unreadNumThreshold
Browse files Browse the repository at this point in the history
BottomBarItem 添加了 unreadNumThreshold 属性 超过这个属性值的unreadNum才会被显示为n+
BottomBarLayout add smoothScroll attribute 属性 可以在layout文件中设置是否smoothScroll
  • Loading branch information
chaychan authored Jan 5, 2018
2 parents 60341f6 + 3cc20bb commit bba68fc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 26 deletions.
68 changes: 42 additions & 26 deletions library/src/main/java/com/chaychan/library/BottomBarItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Locale;


/**
* @author ChayChan
Expand Down Expand Up @@ -45,7 +47,7 @@ public class BottomBarItem extends LinearLayout {

private int mUnreadTextSize = 10; //未读数默认字体大小10sp
private int mMsgTextSize = 6; //消息默认字体大小6sp

private int unreadNumThreshold = 99;

public BottomBarItem(Context context) {
this(context, null);
Expand All @@ -66,7 +68,7 @@ public BottomBarItem(Context context, @Nullable AttributeSet attrs, int defStyle
mIconSelectedResourceId = ta.getResourceId(R.styleable.BottomBarItem_iconSelected, -1);

mText = ta.getString(R.styleable.BottomBarItem_itemText);
mTextSize = ta.getDimensionPixelSize(R.styleable.BottomBarItem_itemTextSize, UIUtils.sp2px(mContext,mTextSize));
mTextSize = ta.getDimensionPixelSize(R.styleable.BottomBarItem_itemTextSize, UIUtils.sp2px(mContext, mTextSize));

mTextColorNormal = ta.getColor(R.styleable.BottomBarItem_textColorNormal, mTextColorNormal);
mTextColorSelected = ta.getColor(R.styleable.BottomBarItem_textColorSelected, mTextColorSelected);
Expand All @@ -80,8 +82,9 @@ public BottomBarItem(Context context, @Nullable AttributeSet attrs, int defStyle
mIconHeight = ta.getDimensionPixelSize(R.styleable.BottomBarItem_iconHeight, 0);
mItemPadding = ta.getDimensionPixelSize(R.styleable.BottomBarItem_itemPadding, 0);

mUnreadTextSize = ta.getDimensionPixelSize(R.styleable.BottomBarItem_unreadTextSize, UIUtils.sp2px(mContext,mUnreadTextSize));
mMsgTextSize = ta.getDimensionPixelSize(R.styleable.BottomBarItem_msgTextSize, UIUtils.sp2px(mContext,mMsgTextSize));
mUnreadTextSize = ta.getDimensionPixelSize(R.styleable.BottomBarItem_unreadTextSize, UIUtils.sp2px(mContext, mUnreadTextSize));
mMsgTextSize = ta.getDimensionPixelSize(R.styleable.BottomBarItem_msgTextSize, UIUtils.sp2px(mContext, mMsgTextSize));
unreadNumThreshold = ta.getInteger(R.styleable.BottomBarItem_unreadThreshold,99);

ta.recycle();

Expand All @@ -101,7 +104,7 @@ private void checkValues() {
throw new IllegalStateException("您还没有设置选中状态下的图标,请指定iconSelected的图标");
}

if (mOpenTouchBg && mTouchDrawable == null){
if (mOpenTouchBg && mTouchDrawable == null) {
//如果有开启触摸背景效果但是没有传对应的drawable
throw new IllegalStateException("开启了触摸效果,但是没有指定touchDrawable");
}
Expand All @@ -112,9 +115,9 @@ private void init() {
setGravity(Gravity.CENTER);

View view = View.inflate(mContext, R.layout.item_bottom_bar, null);
if (mItemPadding != 0){
if (mItemPadding != 0) {
//如果有设置item的padding
view.setPadding(mItemPadding,mItemPadding,mItemPadding,mItemPadding);
view.setPadding(mItemPadding, mItemPadding, mItemPadding, mItemPadding);
}
mImageView = (ImageView) view.findViewById(R.id.iv_icon);
mTvUnread = (TextView) view.findViewById(R.id.tv_unred_num);
Expand All @@ -124,17 +127,17 @@ private void init() {

mImageView.setImageResource(mIconNormalResourceId);

if (mIconWidth != 0 && mIconHeight != 0){
if (mIconWidth != 0 && mIconHeight != 0) {
//如果有设置图标的宽度和高度,则设置ImageView的宽高
FrameLayout.LayoutParams imageLayoutParams = (FrameLayout.LayoutParams) mImageView.getLayoutParams();
imageLayoutParams.width = mIconWidth;
imageLayoutParams.height = mIconHeight;
mImageView.setLayoutParams(imageLayoutParams);
}

mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,mTextSize);//设置底部文字字体大小
mTvUnread.setTextSize(TypedValue.COMPLEX_UNIT_PX,mUnreadTextSize);//设置未读数的字体大小
mTvMsg.setTextSize(TypedValue.COMPLEX_UNIT_PX,mMsgTextSize);//设置提示文字的字体大小
mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);//设置底部文字字体大小
mTvUnread.setTextSize(TypedValue.COMPLEX_UNIT_PX, mUnreadTextSize);//设置未读数的字体大小
mTvMsg.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMsgTextSize);//设置提示文字的字体大小

mTextView.setTextColor(mTextColorNormal);//设置底部文字字体颜色
mTextView.setText(mText);//设置标签文字
Expand All @@ -143,7 +146,7 @@ private void init() {
textLayoutParams.topMargin = mMarginTop;
mTextView.setLayoutParams(textLayoutParams);

if (mOpenTouchBg){
if (mOpenTouchBg) {
//如果有开启触摸背景
setBackground(mTouchDrawable);
}
Expand All @@ -167,12 +170,12 @@ public void setIconSelectedResourceId(int mIconSelectedResourceId) {
this.mIconSelectedResourceId = mIconSelectedResourceId;
}

public void setStatus(boolean isSelected){
mImageView.setImageResource(isSelected?mIconSelectedResourceId:mIconNormalResourceId);
mTextView.setTextColor(isSelected?mTextColorSelected:mTextColorNormal);
public void setStatus(boolean isSelected) {
mImageView.setImageResource(isSelected ? mIconSelectedResourceId : mIconNormalResourceId);
mTextView.setTextColor(isSelected ? mTextColorSelected : mTextColorNormal);
}

private void setTvVisiable(TextView tv){
private void setTvVisiable(TextView tv) {
//都设置为不可见
mTvUnread.setVisibility(GONE);
mTvMsg.setVisibility(GONE);
Expand All @@ -181,34 +184,47 @@ private void setTvVisiable(TextView tv){
tv.setVisibility(VISIBLE);//设置为可见
}

public int getUnreadNumThreshold() {
return unreadNumThreshold;
}

public void setUnreadNumThreshold(int unreadNumThreshold) {
this.unreadNumThreshold = unreadNumThreshold;
}

/**
* 设置未读数
* @param unreadNum 小于等于0则隐藏,大于0小于99则显示对应数字,超过99显示99+
*
* @param unreadNum 小于等于{@link com.chaychan.library.BottomBarItem#unreadNumThreshold}则隐藏,
* 大于0小于{@link com.chaychan.library.BottomBarItem#unreadNumThreshold}则显示对应数字,
* 超过{@link com.chaychan.library.BottomBarItem#unreadNumThreshold}
* 显示{@link com.chaychan.library.BottomBarItem#unreadNumThreshold}+
*/
public void setUnreadNum(int unreadNum){
public void setUnreadNum(int unreadNum) {
setTvVisiable(mTvUnread);
if (unreadNum <= 0){
if (unreadNum <= 0) {
mTvUnread.setVisibility(GONE);
}else if (unreadNum <= 99){
} else if (unreadNum <= unreadNumThreshold) {
mTvUnread.setText(String.valueOf(unreadNum));
}else{
mTvUnread.setText("99+");
} else {
mTvUnread.setText(String.format(Locale.CHINA, "%d+", unreadNum));
}
}
public void setMsg(String msg){

public void setMsg(String msg) {
setTvVisiable(mTvMsg);
mTvMsg.setText(msg);
}

public void hideMsg(){
public void hideMsg() {
mTvMsg.setVisibility(GONE);
}

public void showNotify(){
public void showNotify() {
setTvVisiable(mTvNotify);
}

public void hideNotify(){
public void hideNotify() {
mTvNotify.setVisibility(GONE);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.chaychan.library;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.ViewPager;
Expand Down Expand Up @@ -38,6 +39,9 @@ public BottomBarLayout(Context context, AttributeSet attrs) {

public BottomBarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BottomBarLayout);
mSmoothScroll = ta.getBoolean(R.styleable.BottomBarLayout_smoothScroll,true);
ta.recycle();

setOrientation(HORIZONTAL);
}
Expand Down
5 changes: 5 additions & 0 deletions library/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@
<attr name="unreadTextSize" format="dimension"/>
<!--设置提示消息字体大小-->
<attr name="msgTextSize" format="dimension"/>
<!--设置未读数组阈值 大于阈值的数字将显示为 n+ n为设置的阈值-->
<attr name="unreadThreshold" format="integer"/>
</declare-styleable>
<declare-styleable name="BottomBarLayout">
<attr name="smoothScroll" format="boolean"/>
</declare-styleable>
</resources>

0 comments on commit bba68fc

Please sign in to comment.