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

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

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

       

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

      主要代码:

      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.ViewPager
        android:id="@+id/vp_2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/tl_2"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_above="@id/tl_2"
        android:background="@color/deviderGray"/>

    <com.flyco.tablayout.CommonTabLayout
        android: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};

    @Override
    protected 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() {
            @Override
            public void onTabSelect(int position) {
                mViewPager.setCurrentItem(position);
            }

            @Override
            public 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() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mTabLayout_2.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mViewPager.setCurrentItem(1);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public 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上有实现的各种样式的,我们可以根据自己的需要去选择。

     

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值