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

解决在RelativeLayout等依赖view id的布局中findViewById的错误 #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions badgeviewlib/src/main/java/q/rorbin/badgeview/QBadgeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public Badge bindTarget(final View targetView) {
ViewGroup.LayoutParams targetParams = targetView.getLayoutParams();
targetContainer.removeView(targetView);
final BadgeContainer badgeContainer = new BadgeContainer(getContext());
if(targetContainer instanceof RelativeLayout){
if (targetContainer instanceof RelativeLayout) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是不是可以直接去掉 instanceof 的判断,把新增的 BadgeContainer id 直接变成原来 targetView 的 id

badgeContainer.setId(targetView.getId());
}
targetContainer.addView(badgeContainer, index, targetParams);
Expand Down Expand Up @@ -832,7 +832,7 @@ private class BadgeContainer extends ViewGroup {

@Override
protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
if(!(getParent() instanceof RelativeLayout)){
if (!(getParent() instanceof RelativeLayout)) {
super.dispatchRestoreInstanceState(container);
}
}
Expand Down Expand Up @@ -871,5 +871,21 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(targetView.getMeasuredWidth(), targetView.getMeasuredHeight());
}
}

protected View findViewTraversal(int id) {

final int len = getChildCount();

for (int i = 0; i < len; i++) {
View v = getChildAt(i);
v = v.findViewById(id);

if (v != null) {
return v;
}
}

return null;
}
}
}