下面列出了android.view.View#isEnabled ( ) 实例代码,或者点击链接到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();
}
@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, 40);
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 void removeSelection(View pressedChild, MotionEvent event) {
if (pressedChild == null || selectorRect.isEmpty()) {
return;
}
if (pressedChild.isEnabled()) {
positionSelector(currentChildPosition, pressedChild);
if (selectorDrawable != null) {
Drawable d = selectorDrawable.getCurrent();
if (d instanceof TransitionDrawable) {
((TransitionDrawable) d).resetTransition();
}
if (event != null && Build.VERSION.SDK_INT >= 21) {
selectorDrawable.setHotspot(event.getX(), event.getY());
}
}
} else {
selectorRect.setEmpty();
}
updateSelectorState();
}
@Override
public void onBindView(View view) {
super.onBindView(view);
try {
// move our seekbar to the new view we've been given
ViewParent oldContainer = mSeekBar.getParent();
ViewGroup newContainer = (ViewGroup) view.findViewById(R.id.seekBarPrefBarContainer);
if (oldContainer != newContainer) {
// remove the seekbar from the old view
if (oldContainer != null) {
((ViewGroup) oldContainer).removeView(mSeekBar);
}
// remove the existing seekbar (there may not be one) and add ours
newContainer.removeAllViews();
newContainer.addView(mSeekBar, ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
catch(Exception ex) {
Log.e(TAG, "Error binding view: " + ex.toString());
}
//if dependency is false from the beginning, disable the seek bar
if (view != null && !view.isEnabled())
{
mSeekBar.setEnabled(false);
}
updateView(view);
}
private void removeSelection(View pressedChild, MotionEvent event)
{
if (pressedChild == null)
{
return;
}
if (pressedChild != null && pressedChild.isEnabled())
{
positionSelector(currentChildPosition, pressedChild);
if (selectorDrawable != null)
{
Drawable d = selectorDrawable.getCurrent();
if (d != null && d instanceof TransitionDrawable)
{
((TransitionDrawable) d).resetTransition();
}
if (event != null && Build.VERSION.SDK_INT >= 21)
{
selectorDrawable.setHotspot(event.getX(), event.getY());
}
}
}
else
{
selectorRect.setEmpty();
}
updateSelectorState();
}
private void positionSelector(int position, View sel, boolean manageHotspot, float x, float y)
{
if (selectorDrawable == null)
{
return;
}
final boolean positionChanged = position != selectorPosition;
if (position != NO_POSITION)
{
selectorPosition = position;
}
selectorRect.set(sel.getLeft(), sel.getTop(), sel.getRight(), sel.getBottom());
final boolean enabled = sel.isEnabled();
if (isChildViewEnabled != enabled)
{
isChildViewEnabled = enabled;
}
if (positionChanged)
{
selectorDrawable.setVisible(false, false);
selectorDrawable.setState(StateSet.NOTHING);
}
selectorDrawable.setBounds(selectorRect);
if (positionChanged)
{
if (getVisibility() == VISIBLE)
{
selectorDrawable.setVisible(true, false);
}
}
if (Build.VERSION.SDK_INT >= 21 && manageHotspot)
{
selectorDrawable.setHotspot(x, y);
}
}
/**
* Animate view
*
* @param isShow
* @param v
*/
public void showView(View v, boolean isShow) {
if (isShow && v.isEnabled()) {
v.animate().scaleX(1).scaleY(1).setDuration(100).start();
v.setClickable(true);
v.setFocusable(true);
} else {
v.animate().scaleX(0).scaleY(0).setDuration(100).start();
v.setClickable(false);
v.setFocusable(false);
}
}
private void positionSelector(int position, View sel, boolean manageHotspot, float x, float y)
{
if (selectorDrawable == null)
{
return;
}
final boolean positionChanged = position != selectorPosition;
if (position != NO_POSITION)
{
selectorPosition = position;
}
selectorRect.set(sel.getLeft(), sel.getTop(), sel.getRight(), sel.getBottom());
final boolean enabled = sel.isEnabled();
if (isChildViewEnabled != enabled)
{
isChildViewEnabled = enabled;
}
if (positionChanged)
{
selectorDrawable.setVisible(false, false);
selectorDrawable.setState(StateSet.NOTHING);
}
selectorDrawable.setBounds(selectorRect);
if (positionChanged)
{
if (getVisibility() == VISIBLE)
{
selectorDrawable.setVisible(true, false);
}
}
if (Build.VERSION.SDK_INT >= 21 && manageHotspot)
{
selectorDrawable.setHotspot(x, y);
}
}
private void setEnabledRecursive(View v, boolean enabled) {
if (v == null)
return;
if (v.isEnabled() == enabled)
return;
v.setEnabled(enabled);
if (v instanceof ViewGroup) {
final ViewGroup vg = (ViewGroup) v;
for (int i = vg.getChildCount() - 1; i >= 0; i--) {
setEnabledRecursive(vg.getChildAt(i), enabled);
}
}
}
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;
}
void positionSelector(int position, View sel) {
if (position != INVALID_POSITION) {
mSelectorPosition = position;
}
final Rect selectorRect = mSelectorRect;
invalidate(selectorRect);
selectorRect.set(sel.getLeft(), sel.getTop(), sel.getRight(),
sel.getBottom());
invalidate(selectorRect);
final boolean isChildViewEnabled = mIsChildViewEnabled;
if (sel.isEnabled() != isChildViewEnabled) {
mIsChildViewEnabled = !isChildViewEnabled;
}
}
/**
* Observes motion events and determines when to start forwarding.
*
* @param srcEvent motion event in source view coordinates
* @return true to start forwarding motion events, false otherwise
*/
private boolean onTouchObserved(MotionEvent srcEvent) {
final View src = mSrc;
if (!src.isEnabled()) {
return false;
}
final int actionMasked = srcEvent.getActionMasked();
switch (actionMasked) {
case MotionEvent.ACTION_DOWN:
mActivePointerId = srcEvent.getPointerId(0);
if (mDisallowIntercept == null) {
mDisallowIntercept = new DisallowIntercept();
}
src.postDelayed(mDisallowIntercept, mTapTimeout);
if (mTriggerLongPress == null) {
mTriggerLongPress = new TriggerLongPress();
}
src.postDelayed(mTriggerLongPress, mLongPressTimeout);
break;
case MotionEvent.ACTION_MOVE:
final int activePointerIndex = srcEvent.findPointerIndex(mActivePointerId);
if (activePointerIndex >= 0) {
final float x = srcEvent.getX(activePointerIndex);
final float y = srcEvent.getY(activePointerIndex);
// Has the pointer moved outside of the view?
if (!src.pointInView(x, y, mScaledTouchSlop)) {
clearCallbacks();
// Don't let the parent intercept our events.
src.getParent().requestDisallowInterceptTouchEvent(true);
return true;
}
}
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
clearCallbacks();
break;
}
return false;
}
/**
* Observes motion events and determines when to start forwarding.
*
* @param srcEvent motion event in source view coordinates
* @return true to start forwarding motion events, false otherwise
*/
private boolean onTouchObserved(MotionEvent srcEvent) {
final View src = mSrc;
if (!src.isEnabled()) {
return false;
}
final int actionMasked = MotionEventCompat.getActionMasked(srcEvent);
switch (actionMasked) {
case MotionEvent.ACTION_DOWN:
mActivePointerId = srcEvent.getPointerId(0);
mWasLongPress = false;
if (mDisallowIntercept == null) {
mDisallowIntercept = new DisallowIntercept();
}
src.postDelayed(mDisallowIntercept, mTapTimeout);
if (mTriggerLongPress == null) {
mTriggerLongPress = new TriggerLongPress();
}
src.postDelayed(mTriggerLongPress, mLongPressTimeout);
break;
case MotionEvent.ACTION_MOVE:
final int activePointerIndex = srcEvent.findPointerIndex(mActivePointerId);
if (activePointerIndex >= 0) {
final float x = srcEvent.getX(activePointerIndex);
final float y = srcEvent.getY(activePointerIndex);
if (!pointInView(src, x, y, mScaledTouchSlop)) {
clearCallbacks();
// Don't let the parent intercept our events.
src.getParent().requestDisallowInterceptTouchEvent(true);
return true;
}
}
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
clearCallbacks();
break;
}
return false;
}
@Override public Boolean get(View view) {
return view.isEnabled();
}
@Override
public boolean matchesSafely(View view) {
return view.isEnabled() == isEnabled;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
if (mPressedView != null) {
if (mRecyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
// 正在滚动中,不处理
return false;
}
LogUtil.e("onSingleTapUp1");
BaseWrappedViewHolder baseWrappedViewHolder = (BaseWrappedViewHolder) mRecyclerView.getChildViewHolder(mPressedView);
// 头部和底部view不处理
if (baseWrappedViewHolder != null) {
LogUtil.e("baseWrappedViewHolder");
if (isHeaderViewOrFooterView(baseWrappedViewHolder.getItemViewType())) {
LogUtil.e("isHeaderViewOrFooterView");
return false;
}
// 获取设置点击事件的item_view的id;
Set<Integer> ids = baseWrappedViewHolder.getClickableItemIds();
Set<Integer> nestIds = baseWrappedViewHolder.getNestIds();
LogUtil.e("nestIds");
if (ids != null && ids.size() > 0) {
LogUtil.e("ids.size()");
for (Integer id :
ids) {
final View childView = mPressedView.findViewById(id);
if (childView != null&&childView.getVisibility()==View.VISIBLE) {
// 判断点击位置是否在该view上和该view是否可点击
if (isOnRange(e, childView) && childView.isEnabled()) {
// 这里要排除掉嵌套的recyclerView的点击事件
if (nestIds != null && nestIds.contains(id)) {
LogUtil.e("nestIds");
return false;
}
// 设置item的热点
setChildHotSpot(childView, e);
childView.setPressed(true);
// 点击接口
LogUtil.e("触发item_child点击");
onItemChildClick(baseWrappedViewHolder, id, childView, baseWrappedViewHolder.getAdapterPosition() - mBaseWrappedAdapter.getHeaderViewCount());
// 恢复效果,提交,防止堵塞
resetView(childView);
return true;
} else {
LogUtil.e("isEnabled" + childView.isEnabled());
}
}
}
}
// 如果执行到这里,证明没有设置点击事件,所以设置itemView的点击事件
setChildHotSpot(mPressedView, e);
mPressedView.setPressed(true);
onItemClick(baseWrappedViewHolder, baseWrappedViewHolder.itemView.getId(), mPressedView, baseWrappedViewHolder.getAdapterPosition() - mBaseWrappedAdapter.getHeaderViewCount());
resetView(mPressedView);
}
}
return false;
}
@Override
public void onLongPress(MotionEvent e) {
if (mPressedView != null) {
if (mRecyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
return;
}
mPressedView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
BaseWrappedViewHolder baseWrappedViewHolder = (BaseWrappedViewHolder) mRecyclerView.getChildViewHolder(mPressedView);
if (baseWrappedViewHolder != null) {
boolean isLongClickConsume = false;
if (isHeaderViewOrFooterView(baseWrappedViewHolder.getItemViewType())) {
isLongClickConsume = true;
}
Set<Integer> ids = baseWrappedViewHolder.getLongClickableItemIds();
Set<Integer> nestIds = baseWrappedViewHolder.getNestIds();
if (ids != null && ids.size() > 0) {
for (Integer id :
ids) {
final View childView = mPressedView.findViewById(id);
if (childView != null) {
// 判断点击位置是否在该view上和该view是否可点击
if (isOnRange(e, childView) && childView.isEnabled()) {
// 这里要排除掉嵌套的recyclerView的点击事件
if (nestIds != null && nestIds.contains(id)) {
break;
}
// 设置item的热点
setChildHotSpot(childView, e);
childView.setPressed(true);
// 点击接口
onItemChildLongClick(baseWrappedViewHolder, id, childView, baseWrappedViewHolder.getAdapterPosition() - mBaseWrappedAdapter.getHeaderViewCount());
// 恢复效果,提交,防止堵塞
isLongClickConsume = true;
isPressing = true;
break;
}
}
}
}
if (!isLongClickConsume) {
setChildHotSpot(mPressedView, e);
mPressedView.setPressed(true);
onItemLongClick(baseWrappedViewHolder, mPressedView, baseWrappedViewHolder.getAdapterPosition() - mBaseWrappedAdapter.getHeaderViewCount());
isPressing = true;
}
}
}
}
private void positionSelector(int position, View sel, boolean manageHotspot, float x, float y) {
if (removeHighlighSelectionRunnable != null) {
AndroidUtilities.cancelRunOnUIThread(removeHighlighSelectionRunnable);
removeHighlighSelectionRunnable = null;
pendingHighlightPosition = null;
}
if (selectorDrawable == null) {
return;
}
final boolean positionChanged = position != selectorPosition;
int bottomPadding;
if (getAdapter() instanceof SelectionAdapter) {
bottomPadding = ((SelectionAdapter) getAdapter()).getSelectionBottomPadding(sel);
} else {
bottomPadding = 0;
}
if (position != NO_POSITION) {
selectorPosition = position;
}
selectorRect.set(sel.getLeft(), sel.getTop(), sel.getRight(), sel.getBottom() - bottomPadding);
final boolean enabled = sel.isEnabled();
if (isChildViewEnabled != enabled) {
isChildViewEnabled = enabled;
}
if (positionChanged) {
selectorDrawable.setVisible(false, false);
selectorDrawable.setState(StateSet.NOTHING);
}
selectorDrawable.setBounds(selectorRect);
if (positionChanged) {
if (getVisibility() == VISIBLE) {
selectorDrawable.setVisible(true, false);
}
}
if (Build.VERSION.SDK_INT >= 21 && manageHotspot) {
selectorDrawable.setHotspot(x, y);
}
}
/**
* Observes motion events and determines when to start forwarding.
*
* @param srcEvent motion event in source view coordinates
* @return true to start forwarding motion events, false otherwise
*/
private boolean onTouchObserved(MotionEvent srcEvent) {
final View src = mSrc;
if (!src.isEnabled()) {
return false;
}
final int actionMasked = srcEvent.getActionMasked();
switch (actionMasked) {
case MotionEvent.ACTION_DOWN:
mActivePointerId = srcEvent.getPointerId(0);
if (!mImmediately) {
if (mDisallowIntercept == null) {
mDisallowIntercept = new DisallowIntercept();
}
src.postDelayed(mDisallowIntercept, mTapTimeout);
break;
}
case MotionEvent.ACTION_MOVE:
final int activePointerIndex = srcEvent.findPointerIndex(mActivePointerId);
if (activePointerIndex >= 0) {
final float x = srcEvent.getX(activePointerIndex);
final float y = srcEvent.getY(activePointerIndex);
if (!ViewUtils.pointInView(src, x, y, mScaledTouchSlop) || mImmediately) {
// The pointer has moved outside of the view.
if (mDisallowIntercept != null) {
src.removeCallbacks(mDisallowIntercept);
}
src.getParent().requestDisallowInterceptTouchEvent(true);
return true;
}
}
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
if (mDisallowIntercept != null) {
src.removeCallbacks(mDisallowIntercept);
}
break;
}
return false;
}