下面列出了android.view.View#isClickable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private boolean findClickableViewInChild(View view, int x, int y) {
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
final Rect rect = new Rect();
child.getHitRect(rect);
final boolean contains = rect.contains(x, y);
if (contains) {
return findClickableViewInChild(child, x - rect.left, y - rect.top);
}
}
} else if (view != childView) {
return (view.isEnabled() && (view.isClickable() || view.isLongClickable() || view.isFocusableInTouchMode()));
}
return view.isFocusableInTouchMode();
}
private boolean findClickableViewInChild(View view, int x, int y) {
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
final Rect rect = new Rect();
child.getHitRect(rect);
final boolean contains = rect.contains(x, y);
if (contains) {
return findClickableViewInChild(child, x - rect.left, y - rect.top);
}
}
} else if (view != childView) {
return (view.isEnabled() && (view.isClickable() || view.isLongClickable() || view.isFocusableInTouchMode()));
}
return view.isFocusableInTouchMode();
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int x = (int) event.getRawX();
int y = (int) event.getRawY();
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
View touchTarget = getTouchTarget(this, x, y);
if (touchTarget != null && touchTarget.isClickable() && touchTarget.isEnabled()) {
mTouchTarget = touchTarget;
initParametersForChild(event, touchTarget);
postInvalidateDelayed(INVALIDATE_DURATION);
}
} else if (action == MotionEvent.ACTION_UP) {
mIsPressed = false;
postInvalidateDelayed(INVALIDATE_DURATION);
mDispatchUpTouchEventRunnable.event = event;
postDelayed(mDispatchUpTouchEventRunnable, 200);
return true;
} else if (action == MotionEvent.ACTION_CANCEL) {
mIsPressed = false;
postInvalidateDelayed(INVALIDATE_DURATION);
}
return super.dispatchTouchEvent(event);
}
private boolean findClickableViewInChild(View view, int x, int y) {
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
final Rect rect = new Rect();
child.getHitRect(rect);
final boolean contains = rect.contains(x, y);
if (contains) {
return findClickableViewInChild(child, x - rect.left, y - rect.top);
}
}
} else if (view != childView) {
return (view.isEnabled() && (view.isClickable() || view.isLongClickable() || view.isFocusableInTouchMode()));
}
return view.isFocusableInTouchMode();
}
private boolean findClickableViewInChild(View view, int x, int y) {
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
final Rect rect = new Rect();
child.getHitRect(rect);
final boolean contains = rect.contains(x, y);
if (contains) {
return findClickableViewInChild(child, x - rect.left, y - rect.top);
}
}
} else if (view != childView) {
return (view.isEnabled() && (view.isClickable() || view.isLongClickable() || view.isFocusableInTouchMode()));
}
return view.isFocusableInTouchMode();
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int x = (int) event.getRawX();
int y = (int) event.getRawY();
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
View touchTarget = getTouchTarget(this, x, y);
if (touchTarget != null && touchTarget.isClickable() && touchTarget.isEnabled()) {
mTouchTarget = touchTarget;
initParametersForChild(event, touchTarget);
postInvalidateDelayed(INVALIDATE_DURATION);
}
} else if (action == MotionEvent.ACTION_UP) {
mIsPressed = false;
postInvalidateDelayed(INVALIDATE_DURATION);
mDispatchUpTouchEventRunnable.event = event;
postDelayed(mDispatchUpTouchEventRunnable, 200);
return true;
} else if (action == MotionEvent.ACTION_CANCEL) {
mIsPressed = false;
postInvalidateDelayed(INVALIDATE_DURATION);
}
return super.dispatchTouchEvent(event);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int x = (int) event.getRawX();
int y = (int) event.getRawY();
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
View touchTarget = getTouchTarget(this, x, y);
if (touchTarget != null && touchTarget.isClickable() && touchTarget.isEnabled()) {
mTouchTarget = touchTarget;
initParametersForChild(event, touchTarget);
postInvalidateDelayed(INVALIDATE_DURATION);
}
} else if (action == MotionEvent.ACTION_UP) {
mIsPressed = false;
postInvalidateDelayed(INVALIDATE_DURATION);
mDispatchUpTouchEventRunnable.event = event;
postDelayed(mDispatchUpTouchEventRunnable, 200);
return true;
} else if (action == MotionEvent.ACTION_CANCEL) {
mIsPressed = false;
postInvalidateDelayed(INVALIDATE_DURATION);
}
return super.dispatchTouchEvent(event);
}
private boolean findClickableViewInChild(View view, int x, int y) {
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
final Rect rect = new Rect();
child.getHitRect(rect);
final boolean contains = rect.contains(x, y);
if (contains) {
return findClickableViewInChild(child, x - rect.left, y - rect.top);
}
}
} else if (view != childView) {
return (view.isEnabled() && (view.isClickable() || view.isLongClickable() || view.isFocusableInTouchMode()));
}
return view.isFocusableInTouchMode();
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int x = (int) event.getRawX();
int y = (int) event.getRawY();
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
View touchTarget = getTouchTarget(this, x, y);
if (touchTarget != null && touchTarget.isClickable() && touchTarget.isEnabled()) {
mTouchTarget = touchTarget;
initParametersForChild(event, touchTarget);
postInvalidateDelayed(INVALIDATE_DURATION);
}
} else if (action == MotionEvent.ACTION_UP) {
mIsPressed = false;
postInvalidateDelayed(INVALIDATE_DURATION);
mDispatchUpTouchEventRunnable.event = event;
postDelayed(mDispatchUpTouchEventRunnable, 200);
return true;
} else if (action == MotionEvent.ACTION_CANCEL) {
mIsPressed = false;
postInvalidateDelayed(INVALIDATE_DURATION);
}
return super.dispatchTouchEvent(event);
}
/**
* add childView id
*
* @param viewId add the child view id can support childview click
* @return if you use adapter bind listener
* @link {(adapter.setOnItemChildClickListener(listener))}
* <p>
* or if you can use recyclerView.addOnItemTouch(listerer) wo also support this menthod
*/
@Deprecated
@SuppressWarnings("unchecked")
public BaseViewHolder addOnClickListener(@IdRes final int viewId) {
childClickViewIds.add(viewId);
final View view = getView(viewId);
if (view != null) {
if (!view.isClickable()) {
view.setClickable(true);
}
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (adapter.getOnItemChildClickListener() != null) {
adapter.getOnItemChildClickListener().onItemChildClick(adapter, v, getClickPosition());
}
}
});
}
return this;
}
private boolean findClickableViewInChild(View view, int x, int y) {
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
final Rect rect = new Rect();
child.getHitRect(rect);
final boolean contains = rect.contains(x, y);
if (contains) {
return findClickableViewInChild(child, x - rect.left, y - rect.top);
}
}
} else if (view != childView) {
return (view.isEnabled() && (view.isClickable() || view.isLongClickable() || view.isFocusableInTouchMode()));
}
return view.isFocusableInTouchMode();
}
private boolean isTouchPointInView(View view, int x, int y) {
int[] location = new int[2];
view.getLocationOnScreen(location);
int left = location[0];
int top = location[1];
int right = left + view.getMeasuredWidth();
int bottom = top + view.getMeasuredHeight();
if (view.isClickable() && y >= top && y <= bottom
&& x >= left && x <= right) {
return true;
}
return false;
}
/**
* Determines if the supplied {@link View} is actionable for accessibility purposes.
*
* @param view The {@link View} to evaluate
* @return {@code true} if {@code view} is considered actionable for accessibility
*/
public static boolean isActionableForAccessibility(View view) {
if (view == null) {
return false;
}
return (view.isClickable() || view.isLongClickable() || view.isFocusable());
}
private boolean isTouchPointInView(View view, int x, int y) {
int[] location = new int[2];
view.getLocationOnScreen(location);
int left = location[0];
int top = location[1];
int right = left + view.getMeasuredWidth();
int bottom = top + view.getMeasuredHeight();
if (view.isClickable() && y >= top && y <= bottom
&& x >= left && x <= right) {
return true;
}
return false;
}
private boolean isTouchPointInView(View view, int x, int y) {
int[] location = new int[2];
view.getLocationOnScreen(location);
int left = location[0];
int top = location[1];
int right = left + view.getMeasuredWidth();
int bottom = top + view.getMeasuredHeight();
if (view.isClickable() && y >= top && y <= bottom
&& x >= left && x <= right) {
return true;
}
return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (v.isClickable()) {
/* handle press states */
if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.setPressed(true);
timer = new Timer();
// Initial delay = length of a long press
timer.schedule(new IncrementTask(0, ViewConfiguration.getLongPressTimeout()), ViewConfiguration.getLongPressTimeout());
} else if (event.getAction() == MotionEvent.ACTION_UP) {
timer.cancel();
v.setPressed(false);
}
long lengthOfPress = event.getEventTime() - event.getDownTime();
// If the button has been "tapped" then handle normally
if (lengthOfPress < ViewConfiguration.getLongPressTimeout()
&& event.getAction() == MotionEvent.ACTION_UP) {
incrementListener.increment();
}
return true;
} else {
/* If the view isn't clickable, let the touch be handled by others. */
return false;
}
}
private void checkIfChildWasClicked(MotionEvent ev, final View childView) {
if (childView.isClickable() && isPointInsideView(ev.getRawX(), ev.getRawY(), childView)) {
childView.performClick();
} else if (childView instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) childView;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View childChildView = viewGroup.getChildAt(i);
checkIfChildWasClicked(ev, childChildView);
}
}
}
Builder(int id, @Nullable ViewHierarchyElementAndroid parent, View fromView) {
// Bookkeeping
this.id = id;
this.parentId = (parent != null) ? parent.getId() : null;
this.drawingOrder = null;
// API 16+ properties
this.scrollable = AT_16 ? fromView.isScrollContainer() : null;
// API 11+ properties
this.backgroundDrawableColor =
(AT_11 && (fromView != null) && (fromView.getBackground() instanceof ColorDrawable))
? ((ColorDrawable) fromView.getBackground()).getColor()
: null;
// Base properties
this.visibleToUser = ViewAccessibilityUtils.isVisibleToUser(fromView);
this.className = fromView.getClass().getName();
this.accessibilityClassName = null;
this.packageName = fromView.getContext().getPackageName();
this.resourceName =
(fromView.getId() != View.NO_ID)
? ViewAccessibilityUtils.getResourceNameForView(fromView)
: null;
this.contentDescription = SpannableStringAndroid.valueOf(fromView.getContentDescription());
this.enabled = fromView.isEnabled();
if (fromView instanceof TextView) {
TextView textView = (TextView) fromView;
// Hint text takes precedence if no text is present.
CharSequence text = textView.getText();
if (TextUtils.isEmpty(text)) {
text = textView.getHint();
}
this.text = SpannableStringAndroid.valueOf(text);
this.textSize = textView.getTextSize();
this.textColor = textView.getCurrentTextColor();
this.typefaceStyle =
(textView.getTypeface() != null) ? textView.getTypeface().getStyle() : null;
} else {
this.text = null;
this.textSize = null;
this.textColor = null;
this.typefaceStyle = null;
}
this.importantForAccessibility = ViewAccessibilityUtils.isImportantForAccessibility(fromView);
this.clickable = fromView.isClickable();
this.longClickable = fromView.isLongClickable();
this.focusable = fromView.isFocusable();
this.editable = ViewAccessibilityUtils.isViewEditable(fromView);
this.canScrollForward =
(ViewCompat.canScrollVertically(fromView, 1)
|| ViewCompat.canScrollHorizontally(fromView, 1));
this.canScrollBackward =
(ViewCompat.canScrollVertically(fromView, -1)
|| ViewCompat.canScrollHorizontally(fromView, -1));
this.checkable = (fromView instanceof Checkable);
this.checked = (fromView instanceof Checkable) ? ((Checkable) fromView).isChecked() : null;
this.hasTouchDelegate = (fromView.getTouchDelegate() != null);
this.touchDelegateBounds = ImmutableList.of(); // Unavailable from the View API
this.boundsInScreen = getBoundsInScreen(fromView);
this.nonclippedHeight = fromView.getHeight();
this.nonclippedWidth = fromView.getWidth();
this.actionList = ImmutableList.of(); // Unavailable from the View API
}
@Override
public boolean matchesSafely(View view) {
return view.isClickable() == isClickable;
}
@Override
@SuppressLint("ClickableViewAccessibility")
public boolean onTouch(@NonNull final View v, @NonNull final MotionEvent event) {
final boolean retVal;
if (!mMotionListenerEnabled || mMotions.isEmpty()) {
if (mOnTouchListener != null) {
retVal = mOnTouchListener.onTouch(v, event);
} else {
retVal = false;
}
return retVal;
}
for (Motion motion : mMotions) {
for (EventImitator imitator : motion.imitators) {
imitator.imitate(v, event);
}
}
if (mOnTouchListener != null) {
retVal = mOnTouchListener.onTouch(v, event);
} else {
retVal = true;
}
if (mRequestDisallowTouchEvent) {
// prevents parent from scrolling or otherwise stealing touch events
v.getParent().requestDisallowInterceptTouchEvent(true);
}
if (v.isClickable()) {
if (event.getEventTime() - event.getDownTime()
> ViewConfiguration.getLongPressTimeout()) {
v.setPressed(false);
return true;
}
if (event.getHistorySize() > 0) {
final float deltaX = event.getHistoricalX(event.getHistorySize() - 1) - event.getX();
final float deltaY = event.getHistoricalY(event.getHistorySize() - 1) - event.getY();
// if user has moved too far, it is no longer a click
final boolean removeClickState = Math.pow(deltaX, 2) + Math.pow(deltaY, 2)
> Math.pow(MAX_CLICK_DISTANCE, 2);
v.setPressed(!removeClickState);
return removeClickState;
} else {
return false;
}
}
return retVal;
}