下面列出了android.view.View#isLongClickable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void onLongPress() {
clearCallbacks();
final View src = mSrc;
if (!src.isEnabled() || src.isLongClickable()) {
// Ignore long-press if the view is disabled or has its own
// handler.
return;
}
if (!onForwardingStarted()) {
return;
}
// Don't let the parent intercept our events.
src.getParent().requestDisallowInterceptTouchEvent(true);
// Make sure we cancel any ongoing source event stream.
final long now = SystemClock.uptimeMillis();
final MotionEvent e = MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL, 0, 0, 0);
src.onTouchEvent(e);
e.recycle();
mForwarding = true;
}
/**
* add long click view id
*
* @param viewId
* @return if you use adapter bind listener
* @link {(adapter.setOnItemChildLongClickListener(listener))}
* <p>
* or if you can use recyclerView.addOnItemTouch(listerer) wo also support this menthod
*/
@SuppressWarnings("unchecked")
public BaseViewHolder addOnLongClickListener(@IdRes final int viewId) {
itemChildLongClickViewIds.add(viewId);
final View view = getView(viewId);
if (view != null) {
if (!view.isLongClickable()) {
view.setLongClickable(true);
}
view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return adapter.getOnItemChildLongClickListener() != null &&
adapter.getOnItemChildLongClickListener().onItemChildLongClick(adapter, v, getClickPosition());
}
});
}
return this;
}
/**
* add long click view id
*
* @param viewId
* @return if you use adapter bind listener
* @link {(adapter.setOnItemChildLongClickListener(listener))}
* <p>
* or if you can use recyclerView.addOnItemTouch(listerer) wo also support this menthod
*/
public BaseViewHolder addOnLongClickListener(final int viewId) {
itemChildLongClickViewIds.add(viewId);
final View view = getView(viewId);
if (view != null) {
if (!view.isLongClickable()) {
view.setLongClickable(true);
}
view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (adapter.getmOnItemChildLongClickListener() != null) {
return adapter.getmOnItemChildLongClickListener().onItemChildLongClick(adapter, v, getClickPosition());
}
return false;
}
});
}
return this;
}
/**
* add long click view id
*
* @param viewId
* @return if you use adapter bind listener
* @link {(adapter.setOnItemChildLongClickListener(listener))}
* <p>
* or if you can use recyclerView.addOnItemTouch(listerer) wo also support this menthod
*/
public BaseViewHolder addOnLongClickListener(final int viewId) {
itemChildLongClickViewIds.add(viewId);
final View view = getView(viewId);
if (!view.isLongClickable()) {
view.setLongClickable(true);
}
if (view != null) {
view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (adapter.getmOnItemChildLongClickListener() != null) {
adapter.getmOnItemChildLongClickListener().onItemChildLongClick(adapter, v, getClickPosition());
}
return false;
}
});
}
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 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();
}
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();
}
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();
}
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();
}
/**
* 获取 View 是否可以长按
* @param view {@link View}
* @return {@code true} 可长按, {@code false} 不可长按
*/
public static boolean isLongClickable(final View view) {
if (view != null) {
return view.isLongClickable();
}
return false;
}
static int getViewAttributeFlags(Object content) {
int flags = 0;
if (content instanceof View) {
final View view = (View) content;
if (view.isClickable()) {
flags |= FLAG_VIEW_CLICKABLE;
}
if (view.isLongClickable()) {
flags |= FLAG_VIEW_LONG_CLICKABLE;
}
if (view.isFocusable()) {
flags |= FLAG_VIEW_FOCUSABLE;
}
if (view.isEnabled()) {
flags |= FLAG_VIEW_ENABLED;
}
if (view.isSelected()) {
flags |= FLAG_VIEW_SELECTED;
}
}
return flags;
}
private void checkForLongClick(View target, int delayOffset) {
if (!target.isLongClickable()) {
return;
}
if (longPress == null) {
longPress = new CheckForLongPress(target);
}
target.postDelayed(longPress, ViewConfiguration.getLongPressTimeout() - delayOffset);
}
private void checkForLongClick(View target, int delayOffset) {
if (target.isLongClickable()) {
mHasPerformedLongPress = false;
if (mPendingCheckForLongPress == null) {
mPendingCheckForLongPress = new CheckForLongPress(target);
}
target.postDelayed(mPendingCheckForLongPress,
ViewConfiguration.getLongPressTimeout() - delayOffset);
}
}
private void checkForLongClick(View target, int delayOffset) {
if (target.isLongClickable()) {
mHasPerformedLongPress = false;
if (mPendingCheckForLongPress == null) {
mPendingCheckForLongPress = new CheckForLongPress(target);
}
target.postDelayed(mPendingCheckForLongPress,
ViewConfiguration.getLongPressTimeout() - delayOffset);
}
}
/**
* 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 void checkForLongClick(View target, int delayOffset) {
if (target.isLongClickable()) {
mHasPerformedLongPress = false;
if (mPendingCheckForLongPress == null) {
mPendingCheckForLongPress = new CheckForLongPress(target);
}
target.postDelayed(mPendingCheckForLongPress,
ViewConfiguration.getLongPressTimeout() - delayOffset);
}
}
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
}