Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

badge not hiding on 0 entered. #90

Open
armata99 opened this issue Aug 7, 2019 · 5 comments
Open

badge not hiding on 0 entered. #90

armata99 opened this issue Aug 7, 2019 · 5 comments

Comments

@armata99
Copy link

armata99 commented Aug 7, 2019

neither the hide method nor the 0 input can hide the badge...

@ewgcat
Copy link

ewgcat commented Aug 19, 2019

me ,two

@ewgcat
Copy link

ewgcat commented Aug 19, 2019

me,too

@ewgcat
Copy link

ewgcat commented Aug 19, 2019

neither the hide method nor the 0 input can hide the badge...

该问题是因为重复创建了BadgeView。应该让BadgeView只创建一次。

@hmawla
Copy link

hmawla commented Sep 22, 2019

The problem is
if (mBadgeNumber < 0) {
mBadgeText = "";
in QBadgeView.java

@OverRide
public Badge setBadgeNumber(int badgeNumber) {
mBadgeNumber = badgeNumber;
if (mBadgeNumber < 0) {
mBadgeText = "";
} else if (mBadgeNumber > mMaxBadgeNumber) {
mBadgeText = mExact ? String.valueOf(mBadgeNumber) : mMaxBadgeNumber + "+";
} else if (mBadgeNumber > 0) {
mBadgeText = String.valueOf(mBadgeNumber);
} else {
mBadgeText = null;
}
measureText();
invalidate();
return this;
}

Try editing it in your source since apparently the developer ditched the project 2 years ago

@hyuwah
Copy link

hyuwah commented Oct 13, 2020

I stumbled upon this issue but found a (rather hacky) way to solve this without having to modifying the library yourself

If you use Kotlin in your project, you can use this extension function snippet

// Extensions
fun QBadgeView.setup(count: Int, targetView: View) {
    this
            .setBadgeNumber(count)
            .setBadgeGravity(Gravity.TOP or Gravity.END)
            // add other setter / options according to your own need
            .bindTarget(targetView)
    if (count == 0) {
        val parent = this.targetView.parent
        if (parent is ViewGroup) {
            parent.children.forEach { child ->
                if (child is QBadgeView) { child.isVisible = false }
            }
        }
    }
}

// Usage
private fun setupView() {
    // val count = someMethodToGetCount()
    // val targetView = the view from findViewById / viewBinding / kotlin_synthetic
    QBadgeView(context).setup(count, targetView)
}

Basically, we just simply set the BadgeView's visibility to GONE when our number / count is 0

val parent = this.targetView.parent // "this" is the BadgeView object
        if (parent is ViewGroup) {
            // Iterate the children to search for BadgeView object
            parent.children.forEach { child ->
                if (child is QBadgeView) { 
                    child.isVisible = false  // If it's a BadgeView, we set visibility to gone, 
                                             // isVisible is just a nicer way to do it from core-ktx library
                                             // otherwise just use setVisibility( ... )
                }
            }
        }

Hope this helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants