最近公司有个项目,要仿照微信的未读消息数提醒,提醒用户通知公告的未读数,找了几个都不太好用,后来找前辈咨询,给了一个库,感觉特别实用,在这里分享给大家。

首先给大家看项目的原型图截图:

然后是代码实现后的样子:

主要代码:

layout_test_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tl="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"><android.support.v4.view.ViewPagerandroid:id="@+id/vp_2"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_above="@id/tl_2"/><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:layout_above="@id/tl_2"android:background="@color/deviderGray"/><com.flyco.tablayout.CommonTabLayoutandroid:id="@+id/tl_2"android:layout_width="match_parent"android:layout_height="54dp"android:background="#ffffff"android:layout_alignParentBottom="true"tl:tl_iconHeight="23dp"tl:tl_iconWidth="23dp"tl:tl_indicator_color="#2C97DE"tl:tl_indicator_height="0dp"tl:tl_textSelectColor="#2C97DE"tl:tl_textUnselectColor="#66000000"tl:tl_textsize="12sp"tl:tl_underline_color="#DDDDDD"tl:tl_underline_height="1dp"/></RelativeLayout>
TestMainActivity.java:
package com.example.test.activity;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.shirley.industry.cloudofindustry.R;
import com.shirley.industry.cloudofindustry.company.CompanyServiceFragment;
import com.shirley.industry.cloudofindustry.entity.TabEntity;
import com.shirley.industry.cloudofindustry.home.HomeFragment;
import com.shirley.industry.cloudofindustry.notification.NotificationListFragment;
import com.shirley.industry.cloudofindustry.resource.ResourceFragment;
import com.shirley.industry.cloudofindustry.user.UserFragment;
import com.shirley.industry.cloudofindustry.utils.ViewFindUtils;
import java.util.ArrayList;
import java.util.Random;/*** author : Shirley* e-mail : 18220733786@163.com* date   : 2019/7/11 14:35* desc   :* version: 1.0*/
public class TestMainActivity extends AppCompatActivity {private CommonTabLayout mTabLayout_2;private View mDecorView;private ViewPager mViewPager;private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();private ArrayList<Fragment> mFragments = new ArrayList<>();private String[] mTitles = {"首页", "企业服务", "资源中心", "通知公告","个人中心"};private int[] mIconUnselectIds = {R.drawable.ic_home_normal, R.drawable.ic_company_service_nomal,R.drawable.ic_resource_center_nomal, R.drawable.ic_notification_normal,R.drawable.ic_user_center_normal};private int[] mIconSelectIds = {R.drawable.ic_home_selected, R.drawable.ic_compant_service_selected,R.drawable.ic_resource_center_selected, R.drawable.ic_notification_selected,R.drawable.ic_user_center_selected};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.layout_test_main);HomeFragment homeFragment = new HomeFragment();mFragments.add(homeFragment);CompanyServiceFragment companyServiceFragment = new CompanyServiceFragment();mFragments.add(companyServiceFragment);ResourceFragment resourceFragment = new ResourceFragment();mFragments.add(resourceFragment);NotificationListFragment notificationListFragment = new NotificationListFragment();mFragments.add(notificationListFragment);UserFragment userFragment = new UserFragment();mFragments.add(userFragment);for (int i = 0; i < mTitles.length; i++) {mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));}mDecorView = getWindow().getDecorView();mViewPager = ViewFindUtils.find(mDecorView, R.id.vp_2);mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));/** with ViewPager */mTabLayout_2 = ViewFindUtils.find(mDecorView, R.id.tl_2);tl_2();//两位数mTabLayout_2.showMsg(3, 55);mTabLayout_2.setMsgMargin(3, -20, 5);}Random mRandom = new Random();private void tl_2() {mTabLayout_2.setTabData(mTabEntities);mTabLayout_2.setOnTabSelectListener(new OnTabSelectListener() {@Overridepublic void onTabSelect(int position) {mViewPager.setCurrentItem(position);}@Overridepublic void onTabReselect(int position) {if (position == 3) {mTabLayout_2.showMsg(0, mRandom.nextInt(100) + 1);
//                    UnreadMsgUtils.show(mTabLayout_2.getMsgView(0), mRandom.nextInt(100) + 1);}}});mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {@Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}@Overridepublic void onPageSelected(int position) {mTabLayout_2.setCurrentTab(position);}@Overridepublic void onPageScrollStateChanged(int state) {}});mViewPager.setCurrentItem(1);}private class MyPagerAdapter extends FragmentPagerAdapter {public MyPagerAdapter(FragmentManager fm) {super(fm);}@Overridepublic int getCount() {return mFragments.size();}@Overridepublic CharSequence getPageTitle(int position) {return mTitles[position];}@Overridepublic Fragment getItem(int position) {return mFragments.get(position);}}protected int dp2px(float dp) {final float scale = this.getResources().getDisplayMetrics().density;return (int) (dp * scale + 0.5f);}
}

这其中主要是用到了github上的一个库,链接如下:

https://github.com/H07000223/FlycoTabLayout

github上有实现的各种样式的,我们可以根据自己的需要去选择。

android仿微信实现消未读消息数在tab栏提醒相关推荐

  1. Android系统 应用图标显示未读消息数(BadgeNumber) 桌面app图标的角标显示

    转载请标明出处:http://blog.csdn.net/xx326664162/article/details/51082574 文章出自:薛瑄的博客 你也可以查看我的其他同类文章,也会让你有一定的 ...

  2. android图标未读消息,Android系统 应用图标显示未读消息数(BadgeNumber) 桌面app图标的角标显示...

    原理 添加角标的原理就是发送一个Broadcast(广播),在广播的Intent中指定需要被添加角标的应用的packageName(包名),className(类名),count(角标数目).当然了, ...

  3. Android系统 应用图标显示未读消息数(BadgeNumber) 桌面app图标的角标显示

    http://www.51itong.net/android-badgenumber-9789.html

  4. android 未读信息反复提醒,Android仿微信未读消息数提示显示数字BadgeView大于99条显示99+...

    [实例简介] Android仿微信未读消息数提示显示数字BadgeView大于99条显示99+ [实例截图] [核心代码] BadgeView └── BadgeView ├── app │   ├─ ...

  5. 安卓微信 返回显示未读条数_Android仿微信底部菜单栏功能显示未读消息数量

    底部菜单栏很重要,我看了一下很多应用软件都是用了底部菜单栏,这里使用了tabhost做了一种通用的(就是可以像微信那样显示未读消息数量的,虽然之前也做过但是layout下的xml写的太臃肿,这里去掉了 ...

  6. Android系统 小米/三星/索尼 应用启动图标未读消息数(BadgeNumber)动态提醒

    在Android手机上,如QQ.微信当有未读消息的时候.我们可以看到在应用的启动图标的右上角会有一个红色圈圈.且圈圈里会动态显示未读消息的数目,如下图显示: 那么该功能是怎么实现的呢? 在万能的互联网 ...

  7. Android app图标显示未读消息数

    转载请标明出处:http://blog.csdn.net/xx326664162/article/details/51082574 文章出自:薛瑄的博客 你也可以查看我的其他同类文章,也会让你有一定的 ...

  8. Android 桌面图标右上角显示未读消息数字

    背景: 在Android原生系统中,众所周知不支持桌面图标显示未读消息提醒的数字,虽然第三方控件BadgeView可以实现应用内的数字提醒.但对于系统的图标,特别是app的logo图标很难实现数字标志 ...

  9. android底部导航栏带消息数的框架,GitHub - BarkSheep/Android-NavMenuLayout: 一个底部导航栏, 实现了显示未读消息数, 显示红点等效果的封装...

    Android-NavMenu-master 一个底部导航栏, 实现了显示未读消息数, 显示红点等效果的封装. 添加依赖 1. 在项目根目录的 build.gradle 中添加 allprojects ...

最新文章

  1. 108. Leetcode 188. 买卖股票的最佳时机 IV (动态规划-股票交易)
  2. HDU 4611 Balls Rearrangement 数学
  3. centos 设置双网卡,双网关
  4. Mysql学习总结(25)——MySQL外连接查询
  5. 学习PWM的一些总结
  6. php设置表单的字体,php表单标题怎么设置字体
  7. linux 将img写入硬盘,如何使用Etcher轻松将.img写入Mac上的SD卡
  8. 如何使用CNN进行物体识别和分类_可能我们之前都想错了:CNN的图像分类策略其实出奇的简单呢!...
  9. c语言做一个小程序报告,《C语言程序设计实践》课程报告30个小程序组合成一个大程序.doc...
  10. 用CLIP做多个视频任务!上交牛津提出基于Prompt将CLIP拓展到多个视频任务,在open-set场景中效果极佳!...
  11. VB 全局热键HOOK (不占系统资源版本)
  12. 4.18. 创建与修改时间
  13. linux下编译upx ucl
  14. 利用python调用谷歌翻译API
  15. 【最全】微信支付宝小程序蓝牙API开锁全流程
  16. 十道解分式方程及答案_10道解分式方程练习题及答案.doc
  17. Nginx文件系统:xxx-available文件与xxx-enabled文件的区别
  18. 常见系统故障修复(一)——修复MBR扇区故障
  19. 旋转(Rotation)矩阵转欧拉角(euler)
  20. 什么是RF、IF信号

热门文章

  1. ES5.6.4源码解析--聚合查询流程
  2. Memory和Storage有什么区别?
  3. mhdd扫描磁盘坏道快速使用方法
  4. 360浏览器加速原理
  5. 信息页文内画中画广告js实现代码(文中加载广告方式)
  6. MyBatis-小葵花宝典
  7. 这样做,分销商城就不用再愁没流量
  8. 联想X270变身充电宝
  9. 免费Xshell、Xftp下载、安装、连接教程【图文】详细
  10. 2023年,哪些行业,值得加入?