Java Code Examples for android.view.View#getHitRect()

The following examples show how to use android.view.View#getHitRect() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ListPosition.java    From Dashchan with Apache License 2.0 6 votes vote down vote up
public static ListPosition obtain(ListView listView) {
	int position = listView.getFirstVisiblePosition();
	int y = 0;
	Rect rect = new Rect();
	int paddingTop = listView.getPaddingTop(), paddingLeft = listView.getPaddingLeft();
	for (int i = 0, count = listView.getChildCount(); i < count; i++) {
		View view = listView.getChildAt(i);
		view.getHitRect(rect);
		if (rect.contains(paddingLeft, paddingTop)) {
			position += i;
			y = rect.top - paddingTop;
			break;
		}
	}
	return new ListPosition(position, y);
}
 
Example 2
Source File: MaterialRippleLayout.java    From LoyalNativeSlider with MIT License 6 votes vote down vote up
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();
}
 
Example 3
Source File: CustomSwipeRefreshLayout.java    From SwipeRefreshLayout with Apache License 2.0 6 votes vote down vote up
private boolean canChildrenScroolHorizontally(View view, MotionEvent event, int direction) {
    if (view instanceof ViewGroup) {
        final ViewGroup viewgroup = (ViewGroup) view;
        int count = viewgroup.getChildCount();
        for (int i = 0; i < count; ++i) {
            View child = viewgroup.getChildAt(i);
            Rect bounds = new Rect();
            child.getHitRect(bounds);
            if (bounds.contains((int) event.getX(), (int) event.getY())) {
                if (DEBUG)
                    Log.d(TAG, "in child " + child.getClass().getName());
                return canViewScrollHorizontally(child, event, direction);
            }
        }
    }
    return false;
}
 
Example 4
Source File: DpadAwareRecyclerView.java    From dpad-aware-recycler-view with Apache License 2.0 6 votes vote down vote up
private void requestChildFocusInner(View child, @NonNull View focused) {
    // Try to find first non-null selector to take it as an anchor.
    Drawable refSelector = null;
    for (Drawable selector : mSelectorDrawables) {
        if (selector != null) {
            refSelector = selector;
            break;
        }
    }

    int scrollState = getScrollState();

    if (refSelector != null && scrollState == SCROLL_STATE_IDLE) {
        mSelectorSourceRect.set(refSelector.getBounds());

        // Focused cannot be null
        focused.getHitRect(mSelectorDestRect);

        mReusableSelectListener.mToSelect = child;
        mReusableSelectListener.mToDeselect = mFocusArchivist.getLastFocus(this);

        animateSelectorChange(mReusableSelectListener);

        mFocusArchivist.archiveFocus(this, child);
    }
}
 
Example 5
Source File: TouchInterceptor.java    From GravityBox with Apache License 2.0 6 votes vote down vote up
private int myPointToPosition(int x, int y) {

        if (y < 0) {
            // when dragging off the top of the screen, calculate position
            // by going back from a visible item
            int pos = myPointToPosition(x, y + mItemHeightNormal);
            if (pos > 0) {
                return pos - 1;
            }
        }

        Rect frame = mTempRect;
        final int count = getChildCount();
        for (int i = count - 1; i >= 0; i--) {
            final View child = getChildAt(i);
            child.getHitRect(frame);
            if (frame.contains(x, y)) {
                return getFirstVisiblePosition() + i;
            }
        }
        return INVALID_POSITION;
    }
 
Example 6
Source File: SwipeBackLayout.java    From CoreModule with Apache License 2.0 6 votes vote down vote up
private void drawShadow(Canvas canvas, View child) {
    final Rect childRect = mTmpRect;
    child.getHitRect(childRect);

    if ((mEdgeFlag & EDGE_LEFT) != 0) {
        mShadowLeft.setBounds(childRect.left - mShadowLeft.getIntrinsicWidth(), childRect.top,
                childRect.left, childRect.bottom);
        mShadowLeft.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
        mShadowLeft.draw(canvas);
    }

    if ((mEdgeFlag & EDGE_RIGHT) != 0) {
        mShadowRight.setBounds(childRect.right, childRect.top,
                childRect.right + mShadowRight.getIntrinsicWidth(), childRect.bottom);
        mShadowRight.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
        mShadowRight.draw(canvas);
    }

    if ((mEdgeFlag & EDGE_BOTTOM) != 0) {
        mShadowBottom.setBounds(childRect.left, childRect.bottom, childRect.right,
                childRect.bottom + mShadowBottom.getIntrinsicHeight());
        mShadowBottom.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
        mShadowBottom.draw(canvas);
    }
}
 
Example 7
Source File: CustomSwipeRefreshLayout.java    From SwipeRefreshLayout with Apache License 2.0 6 votes vote down vote up
private boolean canChildrenScroolUp(View view, MotionEvent event) {
    if (view instanceof ViewGroup) {
        final ViewGroup viewgroup = (ViewGroup) view;
        int count = viewgroup.getChildCount();
        for (int i = 0; i < count; ++i) {
            View child = viewgroup.getChildAt(i);
            Rect bounds = new Rect();
            child.getHitRect(bounds);
            if (bounds.contains((int) event.getX(), (int) event.getY())) {
                return canViewScrollUp(child, event);
            }
        }
    }

    return false;
}
 
Example 8
Source File: SwipeBackLayout.java    From UltimateSwipeTool with Apache License 2.0 5 votes vote down vote up
private void drawShadow(Canvas canvas, View child) {
    final Rect childRect = mTmpRect;

    child.getHitRect(childRect);

    if ((mEdgeFlag & EDGE_LEFT) != 0) {
        mShadowLeft.setBounds(childRect.left - mShadowLeft.getIntrinsicWidth(), childRect.top,
                childRect.left, childRect.bottom);
        mShadowLeft.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
        mShadowLeft.draw(canvas);
    }

    if ((mEdgeFlag & EDGE_RIGHT) != 0) {
        mShadowRight.setBounds(childRect.right, childRect.top,
                childRect.right + mShadowRight.getIntrinsicWidth(), childRect.bottom);
        mShadowRight.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
        mShadowRight.draw(canvas);
    }

    if ((mEdgeFlag & EDGE_BOTTOM) != 0) {
        mShadowBottom.setBounds(childRect.left, childRect.bottom, childRect.right,
                childRect.bottom + mShadowBottom.getIntrinsicHeight());
        mShadowBottom.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
        mShadowBottom.draw(canvas);
    }
    if ((mEdgeFlag & EDGE_TOP) != 0) {
        mShadowTop.setBounds(childRect.left, childRect.top - mShadowTop.getIntrinsicHeight(),
                childRect.right, childRect.top);
        mShadowTop.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
        mShadowTop.draw(canvas);
    }
}
 
Example 9
Source File: PinnedSectionListView.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
private boolean isPinnedViewTouched(View view, float x, float y) {
    view.getHitRect(mTouchRect);

    // by taping top or bottom padding, the list performs on click on a border item.
    // we don't add top padding here to keep behavior consistent.
    mTouchRect.top += mTranslateY;

    mTouchRect.bottom += mTranslateY + getPaddingTop();
    mTouchRect.left += getPaddingLeft();
    mTouchRect.right -= getPaddingRight();
    return mTouchRect.contains((int) x, (int) y);
}
 
Example 10
Source File: MessageAdapter.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
private boolean checkFileExistence(Message message, View view, ViewHolder viewHolder) {
    final Rect scrollBounds = new Rect();
    view.getHitRect(scrollBounds);
    if (message.isFileDeleted() && viewHolder.messageBody.getLocalVisibleRect(scrollBounds)) {
        return activity.xmppConnectionService.getFileBackend().getFile(message).exists();
    } else {
        return false;
    }
}
 
Example 11
Source File: CustomViewAbove.java    From KickAssSlidingMenu with Apache License 2.0 5 votes vote down vote up
private boolean isInIgnoredView(MotionEvent ev) {
    Rect rect = new Rect();
    for (View v : mIgnoredViews) {
        v.getHitRect(rect);
        if (rect.contains((int) ev.getX(), (int) ev.getY())) return true;
    }
    return false;
}
 
Example 12
Source File: PinnedSectionListView.java    From AndroidWeekly with Apache License 2.0 5 votes vote down vote up
private boolean isPinnedViewTouched(View view, float x, float y) {
    view.getHitRect(mTouchRect);

    // by taping top or bottom padding, the list performs on click on a border item.
    // we don't add top padding here to keep behavior consistent.
    mTouchRect.top += mTranslateY;

    mTouchRect.bottom += mTranslateY + getPaddingTop();
    mTouchRect.left += getPaddingLeft();
    mTouchRect.right -= getPaddingRight();
    return mTouchRect.contains((int) x, (int) y);
}
 
Example 13
Source File: SlidingTab.java    From CSipSimple with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
	final int action = event.getAction();
	final float x = event.getX();
	final float y = event.getY();
	final Rect frame = new Rect();

	View leftHandle = leftSlider.tab;
	leftHandle.getHitRect(frame);
	boolean leftHit = frame.contains((int) x, (int) y);

	View rightHandle = rightSlider.tab;
	rightHandle.getHitRect(frame);
	boolean rightHit = frame.contains((int) x, (int) y);

	if (!tracking && !(leftHit || rightHit)) {
		return false;
	}

	if (action == MotionEvent.ACTION_DOWN) {
		tracking = true;
		triggered = false;
		vibrate(VIBRATE_SHORT);
		if (leftHit) {
			currentSlider = leftSlider;
			targetZone = TARGET_ZONE;
			rightSlider.hide();
		} else {
			currentSlider = rightSlider;
			targetZone = 1.0f - TARGET_ZONE;
			leftSlider.hide();
		}
		currentSlider.setState(Slider.STATE_PRESSED);
		currentSlider.showTarget();
	}

	return true;
}
 
Example 14
Source File: SwipeBackLayout.java    From light-novel-library_Wenku8_Android with GNU General Public License v2.0 5 votes vote down vote up
private void drawShadow(Canvas canvas, View child) {
	final Rect childRect = mTmpRect;
	child.getHitRect(childRect);

	if ((mEdgeFlag & EDGE_LEFT) != 0) {
		mShadowLeft.setBounds(
				childRect.left - mShadowLeft.getIntrinsicWidth(),
				childRect.top, childRect.left, childRect.bottom);
		mShadowLeft.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
		mShadowLeft.draw(canvas);
	}

	if ((mEdgeFlag & EDGE_RIGHT) != 0) {
		mShadowRight.setBounds(childRect.right, childRect.top,
				childRect.right + mShadowRight.getIntrinsicWidth(),
				childRect.bottom);
		mShadowRight.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
		mShadowRight.draw(canvas);
	}

	if ((mEdgeFlag & EDGE_BOTTOM) != 0) {
		mShadowBottom.setBounds(childRect.left, childRect.bottom,
				childRect.right,
				childRect.bottom + mShadowBottom.getIntrinsicHeight());
		mShadowBottom.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
		mShadowBottom.draw(canvas);
	}
}
 
Example 15
Source File: CustomViewAbove.java    From wakao-app with MIT License 5 votes vote down vote up
private boolean isInIgnoredView(MotionEvent ev) {
	Rect rect = new Rect();
	for (View v : mIgnoredViews) {
		v.getHitRect(rect);
		if (rect.contains((int)ev.getX(), (int)ev.getY())) return true;
	}
	return false;
}
 
Example 16
Source File: TouchInterceptionFrameLayout.java    From Android-ObservableScrollView with Apache License 2.0 5 votes vote down vote up
/**
 * Duplicate touch events to child views.
 * We want to dispatch a down motion event and the move events to
 * child views, but calling dispatchTouchEvent() causes StackOverflowError.
 * Therefore we do it manually.
 *
 * @param ev            Motion event to be passed to children.
 * @param pendingEvents Pending events like ACTION_DOWN. This will be passed to the children before ev.
 */
private void duplicateTouchEventForChildren(MotionEvent ev, MotionEvent... pendingEvents) {
    if (ev == null) {
        return;
    }
    for (int i = getChildCount() - 1; 0 <= i; i--) {
        View childView = getChildAt(i);
        if (childView != null) {
            Rect childRect = new Rect();
            childView.getHitRect(childRect);
            MotionEvent event = MotionEvent.obtainNoHistory(ev);
            if (!childRect.contains((int) event.getX(), (int) event.getY())) {
                continue;
            }
            float offsetX = -childView.getLeft();
            float offsetY = -childView.getTop();
            boolean consumed = false;
            if (pendingEvents != null) {
                for (MotionEvent pe : pendingEvents) {
                    if (pe != null) {
                        MotionEvent peAdjusted = MotionEvent.obtainNoHistory(pe);
                        peAdjusted.offsetLocation(offsetX, offsetY);
                        consumed |= childView.dispatchTouchEvent(peAdjusted);
                    }
                }
            }
            event.offsetLocation(offsetX, offsetY);
            consumed |= childView.dispatchTouchEvent(event);
            if (consumed) {
                break;
            }
        }
    }
}
 
Example 17
Source File: Toolbar.java    From Carbon with Apache License 2.0 4 votes vote down vote up
protected boolean isTransformedTouchPointInView(float x, float y, View child, PointF outLocalPoint) {
    final Rect frame = new Rect();
    child.getHitRect(frame);
    return frame.contains((int) x, (int) y);
}
 
Example 18
Source File: SectionRecyclerCellManager.java    From Shield with MIT License 4 votes vote down vote up
protected void exposeSectionItems(ScrollDirection direction) {
    if (!mCanExposeScreen || layoutManager == null || mergeRecyclerAdapter == null || shieldLayoutManager == null) {
        return;
    }
    int firstPosition = shieldLayoutManager.findFirstVisibleItemPosition(false);
    int lastPostion = shieldLayoutManager.findLastVisibleItemPosition(false);
    int firstCompletePosition = shieldLayoutManager.findFirstVisibleItemPosition(true);
    int lastCompletePosition = shieldLayoutManager.findLastVisibleItemPosition(true);

    //过滤auto offset的遮挡情况
    if (layoutManager instanceof SetAutoOffsetInterface) {
        int autoOffset = ((SetAutoOffsetInterface) layoutManager).getAutoOffset();
        if (autoOffset > 0) {
            for (int index = 0; index < recyclerView.getChildCount(); index++) {
                View itemView = recyclerView.getChildAt(index);
                if (itemView != null) {
                    int viewPosition;
                    if (recyclerView instanceof ShieldRecyclerViewInterface) {
                        viewPosition = ((ShieldRecyclerViewInterface) recyclerView).getShieldChildAdapterPosition(itemView);
                    } else {
                        viewPosition = recyclerView.getChildAdapterPosition(itemView);
                    }
                    if (viewPosition >= firstPosition) {
                        Rect itemRect = new Rect();
                        itemView.getHitRect(itemRect);

                        if (itemRect.bottom > autoOffset) {
                            if (itemRect.top < autoOffset) {
                                firstPosition = viewPosition;
                            } else if (itemRect.top > autoOffset) {
                                firstCompletePosition = viewPosition;
                                break;
                            } else {
                                firstPosition = viewPosition;
                                firstCompletePosition = viewPosition;
                                break;
                            }
                        }
                    }
                }
            }

        }
    }

    ArrayList<ExposedInfo> exposedInfos = new ArrayList<>(lastPostion - firstPosition + 2);
    for (int k = firstPosition; k <= lastPostion; k++) {

        Pair<Integer, Integer> temSectionInfo = mergeRecyclerAdapter.getSectionIndex(k);
        if (temSectionInfo == null) {
            continue;
        }
        MergeSectionDividerAdapter.DetailSectionPositionInfo sectionInfo
                = mergeRecyclerAdapter.getDetailSectionPositionInfo(temSectionInfo.first, temSectionInfo.second);
        if (sectionInfo == null) {
            continue;
        }
        ExposedInfo exposedInfo = new ExposedInfo();
        exposedInfo.owner = mergeRecyclerAdapter.getPieceAdapter(sectionInfo.adapterIndex);
        exposedInfo.details = new ExposedDetails();
        exposedInfo.details.isComplete = false;
        exposedInfo.details.section = sectionInfo.adapterSectionIndex;
        exposedInfo.details.row = sectionInfo.adapterSectionPosition;
        exposedInfo.details.cellType = exposedInfo.owner.getCellType(exposedInfo.details.section, exposedInfo.details.row);

        if (k >= firstCompletePosition && k <= lastCompletePosition) {
            exposedInfo.details.isComplete = true;
        }

        exposedInfos.add(exposedInfo);
    }
    mExposedEngine.updateExposedSections(exposedInfos, direction);
}
 
Example 19
Source File: MotionLayout.java    From Carbon with Apache License 2.0 4 votes vote down vote up
protected boolean isTransformedTouchPointInView(float x, float y, View child, PointF outLocalPoint) {
    final Rect frame = new Rect();
    child.getHitRect(frame);
    return frame.contains((int) x, (int) y);
}
 
Example 20
Source File: SwipeDismissListViewTouchListener.java    From android-open-project-demo with Apache License 2.0 4 votes vote down vote up
private boolean handleDownEvent(final MotionEvent motionEvent) {
    // Find the child view that was touched (perform a hit test)
    Rect rect = new Rect();
    int childCount = mListView.getChildCount();
    int[] listViewCoords = new int[2];
    mListView.getLocationOnScreen(listViewCoords);
    int x = (int) motionEvent.getRawX() - listViewCoords[0];
    int y = (int) motionEvent.getRawY() - listViewCoords[1];
    View downView = null;
    for (int i = 0; i < childCount && downView == null; i++) {
        View child = mListView.getChildAt(i);
        child.getHitRect(rect);
        if (rect.contains(x, y)) {
            downView = child;
        }
    }

    if (downView != null) {
        mDownX = motionEvent.getRawX();
        mDownY = motionEvent.getRawY();
        int downPosition = AdapterViewUtil.getPositionForView(mListView, downView);

        if (mDismissableManager != null) {
            long downId = mListView.getAdapter().getItemId(downPosition);
            if (!mDismissableManager.isDismissable(downId, downPosition)) {
                /* Cancel, not dismissable */
                return false;
            }
        }

        mCurrentDismissData = createPendingDismissData(downPosition, downView);

        if (mPendingDismisses.contains(mCurrentDismissData) || downPosition >= mVirtualListCount) {
            // Cancel, we're already processing this position
            mCurrentDismissData = null;
            return false;
        } else {
            mTouchChildTouched = !mIsParentHorizontalScrollContainer && mResIdOfTouchChild == 0;

            if (mResIdOfTouchChild != 0) {
                mIsParentHorizontalScrollContainer = false;

                final View childView = downView.findViewById(mResIdOfTouchChild);
                if (childView != null) {
                    final Rect childRect = getChildViewRect(mListView, childView);
                    if (childRect.contains((int) motionEvent.getX(), (int) motionEvent.getY())) {
                        mTouchChildTouched = true;
                        mListView.requestDisallowInterceptTouchEvent(true);
                    }
                }
            }

            if (mIsParentHorizontalScrollContainer) {
                // Do it now and don't wait until the user moves more than
                // the slop factor.
                mTouchChildTouched = true;
                mListView.requestDisallowInterceptTouchEvent(true);
            }

            mVelocityTracker = VelocityTracker.obtain();
            mVelocityTracker.addMovement(motionEvent);
        }
    }
    return true;
}