diff --git a/README.md b/README.md index e097a8d..c82c723 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,43 @@ ![](./intro_img/display1.gif) +####显示未读数、提示小红点、提示消息 + +![](./intro_img/4.png) ### BottomBarLayout的使用 +#### BottomBarItem属性介绍 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #### 布局文件中配置 在xml文件中,配置BottomBarLayout,包裹子条目BottomBarItem @@ -94,33 +128,6 @@ -#### BottomBarItem属性介绍 - - - - - - - - - - - - - - - - - - - - - - - - - - #### java文件中设置 找过对应的ViewPager和BottomBarLayout,为ViewPager设置Adapter,然后为BottomBarLayout设置ViewPager @@ -149,6 +156,22 @@ } }); +#### 显示未读数、提示小红点、提示消息 + + mBottomBarLayout.setUnread(0,20);//设置第一个页签的未读数为20 + mBottomBarLayout.setUnread(1,101);//设置第二个页签的未读书 + mBottomBarLayout.showNotify(2);//设置第三个页签显示提示的小红点 + mBottomBarLayout.setMsg(3,"NEW");//设置第四个页签显示NEW提示文字 + +当设置的未读数小于或等于0时,消失未读数的小红点将会消失; +当未读数为1-99时,则显示对应的数字; +当未读数大于99时,显示99+; + +#### 隐藏提示小红点、提示消息 + + mBottomBarLayout.hideNotify(2);//隐藏第三个页签显示提示的小红点 + mBottomBarLayout.hideMsg(3);//隐藏第四个页签显示的提示文字 + #### BottomBarItem的介绍   BottomBarItem继承于LinearLayout,其子View有显示图标的ImageView和展示文字的TextView,分别可以通过getImageView()和getTextView()方法获取到对应的子控件。github上不少底部导航栏的控件都没能获取到对应的子控件,所以在需要对子控件进行操作的时候极不方便,有一些的思路并不是用ImageView和TextView,而是用绘制的,所以也不能获取到对应的显示图标的控件或展示文字的控件,造成无法获取到该控件,无法进行一些业务上的操作,比如类似今日头条的底部的首页,点击首页的页签,会更换成加载中的图标,执行旋转动画,BottomBarLayout可以轻松地做到这个需求。 @@ -233,7 +256,7 @@ 打开app的module中的build.gradle,在dependencies{}中,添加依赖,如下: dependencies { - compile 'com.github.chaychan:BottomBarLayout:1.0.2' + compile 'com.github.chaychan:BottomBarLayout:1.0.4' } diff --git a/demo/src/main/java/com/chaychan/bottombarlayout/MainActivity.java b/demo/src/main/java/com/chaychan/bottombarlayout/MainActivity.java index b5bbae6..e7fa8d4 100644 --- a/demo/src/main/java/com/chaychan/bottombarlayout/MainActivity.java +++ b/demo/src/main/java/com/chaychan/bottombarlayout/MainActivity.java @@ -115,6 +115,11 @@ public void run() { cancelTabLoading(bottomItem);//停止旋转动画 } }); + + mBottomBarLayout.setUnread(0,20);//设置第一个页签的未读数为20 + mBottomBarLayout.setUnread(1,101);//设置第二个页签的未读书 + mBottomBarLayout.showNotify(2);//设置第三个页签显示提示的小红点 + mBottomBarLayout.setMsg(3,"NEW");//设置第四个页签显示NEW提示文字 } /**停止首页页签的旋转动画*/ diff --git a/intro_img/4.png b/intro_img/4.png new file mode 100644 index 0000000..b0581f5 Binary files /dev/null and b/intro_img/4.png differ diff --git a/library/build.gradle b/library/build.gradle index 841548e..90c8b69 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -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' }) diff --git a/library/src/main/java/com/chaychan/library/BottomBarItem.java b/library/src/main/java/com/chaychan/library/BottomBarItem.java index 5e92a1b..aa8169b 100644 --- a/library/src/main/java/com/chaychan/library/BottomBarItem.java +++ b/library/src/main/java/com/chaychan/library/BottomBarItem.java @@ -5,14 +5,15 @@ 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.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; - /** * @author ChayChan * @description: 底部tab条目 @@ -35,9 +36,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) { @@ -73,6 +80,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(); @@ -107,22 +117,27 @@ 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){ //如果有设置图标的宽度和高度,则设置ImageView的宽高 - LayoutParams imageLayoutParams = (LayoutParams) mImageView.getLayoutParams(); + FrameLayout.LayoutParams imageLayoutParams = (FrameLayout.LayoutParams) mImageView.getLayoutParams(); imageLayoutParams.width = mIconWidth; imageLayoutParams.height = mIconHeight; 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; @@ -156,4 +171,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); + } } diff --git a/library/src/main/java/com/chaychan/library/BottomBarLayout.java b/library/src/main/java/com/chaychan/library/BottomBarLayout.java index 38c8492..6d89a06 100644 --- a/library/src/main/java/com/chaychan/library/BottomBarLayout.java +++ b/library/src/main/java/com/chaychan/library/BottomBarLayout.java @@ -139,6 +139,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; } diff --git a/library/src/main/res/drawable/shape_notify_point.xml b/library/src/main/res/drawable/shape_notify_point.xml new file mode 100644 index 0000000..9507100 --- /dev/null +++ b/library/src/main/res/drawable/shape_notify_point.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/library/src/main/res/drawable/shape_unread.xml b/library/src/main/res/drawable/shape_unread.xml new file mode 100644 index 0000000..8290ce8 --- /dev/null +++ b/library/src/main/res/drawable/shape_unread.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/library/src/main/res/layout/item_bottom_bar.xml b/library/src/main/res/layout/item_bottom_bar.xml index 78f25ba..e7ca1a5 100644 --- a/library/src/main/res/layout/item_bottom_bar.xml +++ b/library/src/main/res/layout/item_bottom_bar.xml @@ -1,18 +1,64 @@ - + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/library/src/main/res/values/colors.xml b/library/src/main/res/values/colors.xml index 73bb0c1..caa2a19 100644 --- a/library/src/main/res/values/colors.xml +++ b/library/src/main/res/values/colors.xml @@ -1,5 +1,6 @@ #ffffff + #ff0000 #DDDDDD \ No newline at end of file