微信消息提醒与消息数字提示之BadgeView
BadgeView 一个可以自由定制外观、支持拖拽消除的 MaterialDesign 风格 Android BadgeView。
GitHub地址:https://github.com/stefanjauker/BadgeView
代码:
MainActivity:
package com.example.jash.badgeview;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.jauker.widget.BadgeView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extendsAppCompatActivity {
private ViewPager pager;
private FragmentPagerAdapter mAdapter;
private List<Fragment> mDatas;
private TextView FirstText;
private TextView SecText;
private TextView ThridText;
private LinearLayout layout;
private BadgeView badgeView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager =(ViewPager) findViewById(R.id.viewPager);
initView();
initText();
badgeView.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View view, DragEvent dragEvent) {
switch (dragEvent.getAction()) {
}
return false;
}
});
}
private void initText() {
FirstText = (TextView) findViewById(R.id.first);
SecText = (TextView) findViewById(R.id.second);
ThridText = (TextView) findViewById(R.id.thirid);
layout =(LinearLayout) findViewById(R.id.first_layout);
}
private void initView() {
mDatas = new ArrayList<>();
FirstFragment firstFragment = new FirstFragment();
SecondFragment secondFragment = new SecondFragment();
ThridFragment thridFragment = new ThridFragment();
mDatas.add(firstFragment);
mDatas.add(secondFragment);
mDatas.add(thridFragment);
mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()){
@Override
public FragmentgetItem(int position) {
return mDatas.get(position);
}
@Override
public int getCount() {
return mDatas.size();
}
};
pager.setAdapter(mAdapter);
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset,int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
resetTextView();
switch (position) {
case 0:
if (badgeView!= null) {
layout.removeView(badgeView);
}
badgeView = new BadgeView(MainActivity.this);
badgeView.setBadgeCount(20);
badgeView.setBadgeGravity(Gravity.BOTTOM | Gravity.RIGHT);
layout.addView(badgeView);
FirstText.setTextColor(Color.GREEN);
break;
case 1:
layout.removeView(badgeView);
SecText.setTextColor(Color.GREEN);
break;
case 2:
layout.removeView(badgeView);
ThridText.setTextColor(Color.GREEN);
break;
}
}
private void resetTextView() {
FirstText.setTextColor(Color.BLACK);
SecText.setTextColor(Color.BLACK);
ThridText.setTextColor(Color.BLACK);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
mainXML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.jash.badgeview.MainActivity"><include layout="@layout/top1" /><include layout="@layout/top2" /><android.support.v4.view.ViewPagerandroid:id="@+id/viewPager"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" />
</LinearLayout>
Top1XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="50dp"android:background="#000"android:paddingLeft="12dp"android:paddingRight="12dp"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:gravity="center"android:orientation="horizontal"><ImageViewandroid:layout_width="30dp"android:layout_height="30dp"android:src="@drawable/actionbar_icon" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="12dp"android:text="微信"android:textColor="#d3d3d3"android:textSize="18dp" /></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:gravity="center"android:orientation="horizontal"><ImageViewandroid:layout_width="30dp"android:layout_height="30dp"android:src="@drawable/actionbar_search_icon" /><ImageViewandroid:layout_width="30dp"android:layout_height="30dp"android:src="@drawable/actionbar_add_icon" /><ImageViewandroid:layout_width="30dp"android:layout_height="30dp"android:src="@drawable/actionbar_more_icon" /></LinearLayout>
</RelativeLayout>
Top2XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="40dp"android:background="#eee"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="37dp"android:orientation="horizontal"><LinearLayoutandroid:id="@+id/first_layout"android:orientation="horizontal"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"><TextViewandroid:id="@+id/first"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="聊天"android:textColor="#0f0" /></LinearLayout><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"><TextViewandroid:id="@+id/second"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="发现"android:textColor="#000" /></LinearLayout><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"><TextViewandroid:id="@+id/thirid"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="通讯录"android:textColor="#000" /></LinearLayout></LinearLayout><ImageViewandroid:layout_width="match_parent"android:layout_height="3dp"android:background="@drawable/tabline" /></LinearLayout>
三个Fragment的Activity:
package com.example.jash.badgeview;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;/*** A simple {@link Fragment} subclass.*/
public class FirstFragment extends Fragment {public FirstFragment() {// Required empty public constructor}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_first, container, false);}}
Fragment的XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.jash.badgeview.FirstFragment"><TextViewandroid:gravity="center"android:layout_width="match_parent"android:layout_height="match_parent"android:text="聊天" /></FrameLayout>
BadgeView的主要方法:
· setTargetView(View)
设置控件
· setBadgeCount(int)
设置显示的数字
· setBadgeGravity(Gravity)
设置显示的位置
· setBackgroundColor()
设置背景色
· setBackgroundResource()
设置背景图片
· setTypeface()
设置显示字体
· setShadowLayer()
设置字体阴影
· setVisibility
设置显藏状态