Skip to content

Commit

Permalink
添加设置未读数、提示小红点、提示消息的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
chaychan committed Sep 10, 2017
1 parent ac08f33 commit e4c8b3d
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ public void run() {
cancelTabLoading(bottomItem);//停止旋转动画
}
});

mBottomBarLayout.setUnread(0,20);//设置第一个页签的未读数为20
mBottomBarLayout.setUnread(1,101);//设置第二个页签的未读书
mBottomBarLayout.showNotify(2);//设置第三个页签显示提示的小红点
mBottomBarLayout.setMsg(3,"NEW");//设置第四个页签显示NEW提示文字
}

/**停止首页页签的旋转动画*/
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
Expand Down
66 changes: 60 additions & 6 deletions library/src/main/java/com/chaychan/library/BottomBarItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;



/**
* @author ChayChan
* @description: 底部tab条目
Expand All @@ -35,9 +35,15 @@ public class BottomBarItem extends LinearLayout {
private int mIconHeight;//图标的高度
private int mItemPadding;//BottomBarItem的padding

private TextView mTextView;

private ImageView mImageView;
private TextView mTvUnread;
private TextView mTvNotify;
private TextView mTvMsg;
private TextView mTextView;

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


public BottomBarItem(Context context) {
Expand Down Expand Up @@ -73,6 +79,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));

ta.recycle();

checkValues();
Expand Down Expand Up @@ -107,9 +116,11 @@ private void init() {
view.setPadding(mItemPadding,mItemPadding,mItemPadding,mItemPadding);
}
mImageView = (ImageView) view.findViewById(R.id.iv_icon);
mTvUnread = (TextView) view.findViewById(R.id.tv_unred_num);
mTvMsg = (TextView) view.findViewById(R.id.tv_msg);
mTvNotify = (TextView) view.findViewById(R.id.tv_point);
mTextView = (TextView) view.findViewById(R.id.tv_text);


mImageView.setImageResource(mIconNormalResourceId);

if (mIconWidth != 0 && mIconHeight != 0){
Expand All @@ -120,9 +131,12 @@ private void init() {
mImageView.setLayoutParams(imageLayoutParams);
}

mTextView.getPaint().setTextSize(mTextSize);
mTextView.setText(mText);
mTextView.setTextColor(mTextColorNormal);
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);//设置标签文字

LayoutParams textLayoutParams = (LayoutParams) mTextView.getLayoutParams();
textLayoutParams.topMargin = mMarginTop;
Expand Down Expand Up @@ -156,4 +170,44 @@ public void setStatus(boolean isSelected){
mImageView.setImageResource(isSelected?mIconSelectedResourceId:mIconNormalResourceId);
mTextView.setTextColor(isSelected?mTextColorSelected:mTextColorNormal);
}

private void setTvVisiable(TextView tv){
//都设置为不可见
mTvUnread.setVisibility(GONE);
mTvMsg.setVisibility(GONE);
mTvNotify.setVisibility(GONE);

tv.setVisibility(VISIBLE);//设置为可见
}

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

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

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

public void hideNotify(){
mTvNotify.setVisibility(GONE);
}
}
42 changes: 42 additions & 0 deletions library/src/main/java/com/chaychan/library/BottomBarLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,48 @@ public void setCurrentItem(int currentItem) {
mViewPager.setCurrentItem(mCurrentItem,mSmoothScroll);
}

/**
* 设置未读数
* @param position 底部标签的下标
* @param unreadNum 未读数
*/
public void setUnread(int position,int unreadNum){
mItemViews.get(position).setUnreadNum(unreadNum);
}

/**
* 设置提示消息
* @param position 底部标签的下标
* @param msg 未读数
*/
public void setMsg(int position,String msg){
mItemViews.get(position).setMsg(msg);
}

/**
* 隐藏提示消息
* @param position 底部标签的下标
*/
public void hideMsg(int position){
mItemViews.get(position).hideMsg();
}

/**
* 显示提示的小红点
* @param position 底部标签的下标
*/
public void showNotify(int position){
mItemViews.get(position).showNotify();
}

/**
* 隐藏提示的小红点
* @param position 底部标签的下标
*/
public void hideNotify(int position){
mItemViews.get(position).hideNotify();
}

public int getCurrentItem() {
return mCurrentItem;
}
Expand Down
9 changes: 9 additions & 0 deletions library/src/main/res/drawable/shape_notify_point.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<solid android:color="@color/red" />
<size
android:width="10dp"
android:height="10dp" />
</shape>
6 changes: 6 additions & 0 deletions library/src/main/res/drawable/shape_unread.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="20dp" />
<solid android:color="@color/red" />
<padding android:left="4dp" android:right="4dp" android:top="1dp" android:bottom="1dp"/>
</shape>
60 changes: 53 additions & 7 deletions library/src/main/res/layout/item_bottom_bar.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
>

<ImageView
android:id="@+id/iv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/iv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>

<TextView
android:id="@+id/tv_unred_num"
android:layout_width="wrap_content"
android:minWidth="15dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="14dp"
android:background="@drawable/shape_unread"
android:gravity="center"
android:text="99+"
android:textColor="@color/white"
android:textSize="10sp"
android:visibility="gone" />

<TextView
android:id="@+id/tv_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="14dp"
android:background="@drawable/shape_unread"
android:gravity="center"
android:text="NEW"
android:textColor="@color/white"
android:textSize="6sp"
android:visibility="gone" />

<TextView
android:id="@+id/tv_point"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:background="@drawable/shape_notify_point"
android:gravity="center"
android:textSize="6sp"
android:visibility="gone" />

</FrameLayout>


<TextView
android:id="@+id/tv_text"
Expand Down
4 changes: 4 additions & 0 deletions library/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@
<attr name="iconHeight" format="dimension"/>
<!--设置BottomBarItem的padding-->
<attr name="itemPadding" format="dimension"/>
<!--设置未读数字体大小-->
<attr name="unreadTextSize" format="dimension"/>
<!--设置提示消息字体大小-->
<attr name="msgTextSize" format="dimension"/>
</declare-styleable>
</resources>
1 change: 1 addition & 0 deletions library/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffff</color>
<color name="red">#ff0000</color>
<color name="selector_grey">#DDDDDD</color>
</resources>

0 comments on commit e4c8b3d

Please sign in to comment.