Skip to content

Commit

Permalink
Update badge view demo and make it easy.
Browse files Browse the repository at this point in the history
  • Loading branch information
ittianyu committed May 25, 2017
1 parent 5a3eb27 commit 64879f7
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 81 deletions.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,33 @@ bind.vp.setAdapter(adapter);
bind.bnve.setupWithViewPager(bind.vp);
```

#### Add badge view ####

1. Add badge lib
```
compile 'q.rorbin:badgeview:1.1.0'
```
2. Bind bottom view
```
// add badge
addBadgeAt(2, 1);

private Badge addBadgeAt(int position, int number) {
// add badge
return new QBadgeView(this)
.setBadgeNumber(number)
.setGravityOffset(12, 2, true)
.bindTarget(bind.bnve.getBottomNavigationItemView(position))
.setOnDragStateChangedListener(new Badge.OnDragStateChangedListener() {
@Override
public void onDragStateChanged(int dragState, Badge badge, View targetView) {
if (Badge.OnDragStateChangedListener.STATE_SUCCEED == dragState)
Toast.makeText(BadgeViewActivity.this, R.string.tips_badge_removed, Toast.LENGTH_SHORT).show();
}
});
}
```

#### Other usage in BottomNavigationViewEx ####
You can see the demo.

Expand Down Expand Up @@ -404,6 +431,34 @@ bind.vp.setAdapter(adapter);
bind.bnve.setupWithViewPager(bind.vp);
```


#### 添加带数字的小红点 ####

1. Gradle 中加入 badge 库的依赖
```
compile 'q.rorbin:badgeview:1.1.0'
```
2. 和底部控件绑定
```
// add badge
addBadgeAt(2, 1);

private Badge addBadgeAt(int position, int number) {
// add badge
return new QBadgeView(this)
.setBadgeNumber(number)
.setGravityOffset(12, 2, true)
.bindTarget(bind.bnve.getBottomNavigationItemView(position))
.setOnDragStateChangedListener(new Badge.OnDragStateChangedListener() {
@Override
public void onDragStateChanged(int dragState, Badge badge, View targetView) {
if (Badge.OnDragStateChangedListener.STATE_SUCCEED == dragState)
Toast.makeText(BadgeViewActivity.this, R.string.tips_badge_removed, Toast.LENGTH_SHORT).show();
}
});
}
```

#### 其他 BottomNavigationViewEx 的用法 ####
请参考demo。

Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {

defaultConfig {
applicationId "com.ittianyu.bottomnavigationviewexsample"
minSdkVersion 9
minSdkVersion 14
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 13
versionName "1.1.9"
Expand Down Expand Up @@ -38,5 +38,5 @@ dependencies {
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'

// badge
compile 'com.allenliu.badgeview:library:1.1.1'
compile 'q.rorbin:badgeview:1.1.0'
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
package com.ittianyu.bottomnavigationviewexsample.features.badgeview;

import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.allenliu.badgeview.BadgeFactory;
import com.allenliu.badgeview.BadgeView;
import com.ittianyu.bottomnavigationviewexsample.R;
import com.ittianyu.bottomnavigationviewexsample.databinding.ActivityBadgeViewBinding;

import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;

public class BadgeViewActivity extends AppCompatActivity {
private ActivityBadgeViewBinding bind;
private BadgeView badgeView1;
private BadgeView badgeView3;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -38,78 +31,23 @@ private void initView() {
bind.bnve.enableItemShiftingMode(false);


// add a BadgeView at second icon
bind.bnve.post(new Runnable() {
@Override
public void run() {
badgeView1 = addBadgeViewAt(1, "1", BadgeView.SHAPE_OVAL);
badgeView3 = addBadgeViewAt(3, "99", BadgeView.SHAPE_OVAL);
// add badge
addBadgeAt(2, 1);
}

// hide the red circle when click
bind.bnve.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
private Badge addBadgeAt(int position, int number) {
// add badge
return new QBadgeView(this)
.setBadgeNumber(number)
.setGravityOffset(12, 2, true)
.bindTarget(bind.bnve.getBottomNavigationItemView(position))
.setOnDragStateChangedListener(new Badge.OnDragStateChangedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int position = bind.bnve.getMenuItemPosition(item);
switch (position) {
case 1:
toggleBadgeView(badgeView1);
break;
case 3:
toggleBadgeView(badgeView3);
break;
}
return true;
public void onDragStateChanged(int dragState, Badge badge, View targetView) {
if (Badge.OnDragStateChangedListener.STATE_SUCCEED == dragState)
Toast.makeText(BadgeViewActivity.this, R.string.tips_badge_removed, Toast.LENGTH_SHORT).show();
}
});
}
});

}

/**
* show or hide badgeView
* @param badgeView
*/
private void toggleBadgeView(BadgeView badgeView) {
badgeView.setVisibility(badgeView.getVisibility() == View.VISIBLE ? View.INVISIBLE : View.VISIBLE);
}

/**
* add a BadgeView on icon at position
* @param position add to which icon
* @param text the text show on badge
* @param shape the badge view shape
* @return
*/
private BadgeView addBadgeViewAt(int position, String text, int shape) {
// get position
ImageView icon = bind.bnve.getIconAt(position);
int[] pos = new int[2];
icon.getLocationInWindow(pos);
// action bar height
ActionBar actionBar = getSupportActionBar();
int actionBarHeight = 0;
if (null != actionBar) {
actionBarHeight = actionBar.getHeight();
}
int x = (int) (pos[0] + icon.getMeasuredWidth() * 0.7f);
int y = (int) (pos[1] - actionBarHeight - icon.getMeasuredHeight() * 1.25f);
// calculate width
int width = 16 + 4 * (text.length() - 1);
int height = 16;

BadgeView badgeView = BadgeFactory.create(this)
.setTextColor(Color.WHITE)
.setWidthAndHeight(width, height)
.setBadgeBackground(Color.RED)
.setTextSize(10)
.setBadgeGravity(Gravity.LEFT | Gravity.TOP)
.setBadgeCount(text)
.setShape(shape)
// .setMargin(0, 0, 0, 0)
.bind(this.bind.rlRoot);
badgeView.setX(x);
badgeView.setY(y);
return badgeView;
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
<string name="face">表情</string>
<string name="lamp">灯</string>
<string name="weather">天气</string>
<string name="tips_badge_removed">Badge 被消除了</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@
<string name="custom_typeface" translatable="false">custom typeface</string>
<string name="icon_selector" translatable="false">icon selector</string>
<string name="icon_margin_top" translatable="false">icon margin top</string>
<string name="tips_badge_removed">Badge is removed</string>

</resources>

0 comments on commit 64879f7

Please sign in to comment.