diff --git a/README.md b/README.md index 75c6009..29c4445 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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。 diff --git a/app/build.gradle b/app/build.gradle index 7c94f76..ddfe14f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,7 +6,7 @@ android { defaultConfig { applicationId "com.ittianyu.bottomnavigationviewexsample" - minSdkVersion 9 + minSdkVersion 14 targetSdkVersion rootProject.ext.targetSdkVersion versionCode 13 versionName "1.1.9" @@ -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' } diff --git a/app/src/main/java/com/ittianyu/bottomnavigationviewexsample/features/badgeview/BadgeViewActivity.java b/app/src/main/java/com/ittianyu/bottomnavigationviewexsample/features/badgeview/BadgeViewActivity.java index f03dde2..c7cd83e 100644 --- a/app/src/main/java/com/ittianyu/bottomnavigationviewexsample/features/badgeview/BadgeViewActivity.java +++ b/app/src/main/java/com/ittianyu/bottomnavigationviewexsample/features/badgeview/BadgeViewActivity.java @@ -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) { @@ -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; - } } diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 264e519..d32700d 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -12,4 +12,5 @@ 表情 天气 + Badge 被消除了 \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 475ed03..db49f67 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -37,5 +37,6 @@ custom typeface icon selector icon margin top + Badge is removed