Java Code Examples for android.view.View.setPressed()

The following are Jave code examples for showing how to use setPressed() of the android.view.View class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: exciting-app   File: AbsHListView.java   Source Code and License Vote up 7 votes
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
	switch (keyCode) {
	case KeyEvent.KEYCODE_DPAD_CENTER:
	case KeyEvent.KEYCODE_ENTER:
		if (!isEnabled()) {
			return true;
		}
		if (isClickable() && isPressed() && mSelectedPosition >= 0
				&& mAdapter != null
				&& mSelectedPosition < mAdapter.getCount()) {

			final View view = getChildAt(mSelectedPosition - mFirstPosition);
			if (view != null) {
				performItemClick(view, mSelectedPosition, mSelectedColId);
				view.setPressed(false);
			}
			setPressed(false);
			return true;
		}
		break;
	}
	return super.onKeyUp(keyCode, event);
}
 
Example 2
Project: Swap   File: PLA_AbsListView.java   Source Code and License Vote up 7 votes
private boolean startScrollIfNeeded(int deltaY) {
    // Check if we have moved far enough that it looks more like a
    // scroll than a tap
    final int distance = Math.abs(deltaY);
    int touchSlop = mTouchSlop;
    if (distance > touchSlop) {
        createScrollingCache();
        mTouchMode = TOUCH_MODE_SCROLL;
        mMotionCorrection = deltaY;
        setPressed(false);
        View motionView = getChildAt(mMotionPosition - mFirstPosition);
        if (motionView != null) {
            motionView.setPressed(false);
        }
        reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
        // Time to start stealing events! Once we've stolen them, don't let anyone
        // steal from us
        requestDisallowInterceptTouchEvent(true);
        return true;
    }

    return false;
}
 
Example 3
Project: letv   File: AbsHListView.java   Source Code and License Vote up 7 votes
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case 23:
        case 66:
            if (!isEnabled()) {
                return true;
            }
            if (isClickable() && isPressed() && this.mSelectedPosition >= 0 && this.mAdapter != null && this.mSelectedPosition < this.mAdapter.getCount()) {
                View view = getChildAt(this.mSelectedPosition - this.mFirstPosition);
                if (view != null) {
                    performItemClick(view, this.mSelectedPosition, this.mSelectedColId);
                    view.setPressed(false);
                }
                setPressed(false);
                return true;
            }
    }
    return super.onKeyUp(keyCode, event);
}
 
Example 4
Project: GitHub   File: FreeFlowContainer.java   Source Code and License Vote up 6 votes
@Override
public void run() {
	if (beginTouchAt == null) {
		// Assuming child that was being long pressed
		// is no longer valid
		return;
	}

	mCheckStates.clear();
	final View child = beginTouchAt.view;
	if (child != null) {
		boolean handled = false;
		// if (!mDataChanged) {
		handled = performLongPress();
		// }
		if (handled) {
			mTouchMode = TOUCH_MODE_REST;

			if (mOnTouchModeChangedListener != null) {
				mOnTouchModeChangedListener
						.onTouchModeChanged(mTouchMode);
			}

			// setPressed(false);
			child.setPressed(false);
		} else {
			mTouchMode = TOUCH_MODE_DONE_WAITING;

			if (mOnTouchModeChangedListener != null) {
				mOnTouchModeChangedListener
						.onTouchModeChanged(mTouchMode);
			}
		}
	}
}
 
Example 5
Project: GitHub   File: ItemTouchListenerAdapter.java   Source Code and License Vote up 6 votes
@Override
public void onShowPress(MotionEvent e) {
    View view = getChildViewUnder(e);
    if (view != null) {
        view.setPressed(true);
    }
}
 
Example 6
Project: GitHub   File: ItemTouchListenerAdapter.java   Source Code and License Vote up 6 votes
@Override
public boolean onSingleTapUp(MotionEvent e) {
    View view = getChildViewUnder(e);
    if (view == null) return false;

    view.setPressed(false);
    int position = shiftAdjustInt(recyclerView.getChildAdapterPosition(view));

    if (position != AdmobAdapter.POSITION_ON_AD) {
        listener.onItemClick(recyclerView, view, position);
    }
    return true;
}
 
Example 7
Project: letv   File: AbsHListView.java   Source Code and License Vote up 6 votes
protected void keyPressed() {
    if (isEnabled() && isClickable()) {
        Drawable selector = this.mSelector;
        Rect selectorRect = this.mSelectorRect;
        if (selector == null) {
            return;
        }
        if ((isFocused() || touchModeDrawsInPressedState()) && !selectorRect.isEmpty()) {
            View v = getChildAt(this.mSelectedPosition - this.mFirstPosition);
            if (v != null) {
                if (!v.hasFocusable()) {
                    v.setPressed(true);
                } else {
                    return;
                }
            }
            setPressed(true);
            boolean longClickable = isLongClickable();
            Drawable d = selector.getCurrent();
            if (d != null && (d instanceof TransitionDrawable)) {
                if (longClickable) {
                    ((TransitionDrawable) d).startTransition(ViewConfiguration.getLongPressTimeout());
                } else {
                    ((TransitionDrawable) d).resetTransition();
                }
            }
            if (longClickable && !this.mDataChanged) {
                if (this.mPendingCheckForKeyLongPress == null) {
                    this.mPendingCheckForKeyLongPress = new CheckForKeyLongPress(this, null);
                }
                this.mPendingCheckForKeyLongPress.rememberWindowAttachCount();
                postDelayed(this.mPendingCheckForKeyLongPress, (long) ViewConfiguration.getLongPressTimeout());
            }
        }
    }
}
 
Example 8
Project: GitHub   File: SwipeListView.java   Source Code and License Vote up 6 votes
/**
 * Recycle cell. This method should be called from getView in Adapter when use SWIPE_ACTION_CHOICE
 *
 * @param convertView parent view
 * @param position    position in list
 */
public void recycle(View convertView, int position) {
    touchListener.reloadChoiceStateInView(convertView.findViewById(swipeFrontView), position);
    touchListener.reloadSwipeStateInView(convertView.findViewById(swipeFrontView), position);

    // Clean pressed state (if dismiss is fire from a cell, to this cell, with a press drawable, in a swipelistview
    // when this cell will be recycle it will still have his pressed state. This ensure the pressed state is
    // cleaned.
    for (int j = 0; j < ((ViewGroup) convertView).getChildCount(); ++j) {
        View nextChild = ((ViewGroup) convertView).getChildAt(j);
        nextChild.setPressed(false);
    }
}
 
Example 9
Project: boohee_v5.6   File: ListPopupWindow.java   Source Code and License Vote up 6 votes
private void setPressedItem(View child, int position, float x, float y) {
    this.mDrawsInPressedState = true;
    if (VERSION.SDK_INT >= 21) {
        drawableHotspotChanged(x, y);
    }
    if (!isPressed()) {
        setPressed(true);
    }
    layoutChildren();
    if (this.mMotionPosition != -1) {
        View motionView = getChildAt(this.mMotionPosition - getFirstVisiblePosition());
        if (!(motionView == null || motionView == child || !motionView.isPressed())) {
            motionView.setPressed(false);
        }
    }
    this.mMotionPosition = position;
    float childX = x - ((float) child.getLeft());
    float childY = y - ((float) child.getTop());
    if (VERSION.SDK_INT >= 21) {
        child.drawableHotspotChanged(childX, childY);
    }
    if (!child.isPressed()) {
        child.setPressed(true);
    }
    positionSelectorLikeTouchCompat(position, child, x, y);
    setSelectorEnabled(false);
    refreshDrawableState();
}
 
Example 10
Project: boohee_v5.6   File: BaseMenuPresenter.java   Source Code and License Vote up 6 votes
public void updateMenuView(boolean cleared) {
    ViewGroup parent = this.mMenuView;
    if (parent != null) {
        int childIndex = 0;
        if (this.mMenu != null) {
            this.mMenu.flagActionItems();
            ArrayList<MenuItemImpl> visibleItems = this.mMenu.getVisibleItems();
            int itemCount = visibleItems.size();
            for (int i = 0; i < itemCount; i++) {
                MenuItemImpl item = (MenuItemImpl) visibleItems.get(i);
                if (shouldIncludeItem(childIndex, item)) {
                    View convertView = parent.getChildAt(childIndex);
                    MenuItemImpl oldItem = convertView instanceof ItemView ? ((ItemView) convertView).getItemData() : null;
                    View itemView = getItemView(item, convertView, parent);
                    if (item != oldItem) {
                        itemView.setPressed(false);
                        ViewCompat.jumpDrawablesToCurrentState(itemView);
                    }
                    if (itemView != convertView) {
                        addItemView(itemView, childIndex);
                    }
                    childIndex++;
                }
            }
        }
        while (childIndex < parent.getChildCount()) {
            if (!filterLeftoverView(parent, childIndex)) {
                childIndex++;
            }
        }
    }
}
 
Example 11
Project: boohee_v5.6   File: ListPopupWindow.java   Source Code and License Vote up 6 votes
private void clearPressedItem() {
    this.mDrawsInPressedState = false;
    setPressed(false);
    drawableStateChanged();
    View motionView = getChildAt(this.mMotionPosition - getFirstVisiblePosition());
    if (motionView != null) {
        motionView.setPressed(false);
    }
    if (this.mClickAnimation != null) {
        this.mClickAnimation.cancel();
        this.mClickAnimation = null;
    }
}
 
Example 12
Project: Bigbang   File: DragDropHelper.java   Source Code and License Vote up 6 votes
private Bitmap getViewBitmap(View v) {
    v.clearFocus();
    v.setPressed(false);

    boolean willNotCache = v.willNotCacheDrawing();
    v.setWillNotCacheDrawing(false);

    int color = v.getDrawingCacheBackgroundColor();
    v.setDrawingCacheBackgroundColor(0);

    if (color != 0) {
        v.destroyDrawingCache();
    }
    v.buildDrawingCache();
    Bitmap cacheBitmap = v.getDrawingCache();
    if (cacheBitmap == null) {
        return null;
    }

    Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

    v.destroyDrawingCache();
    v.setWillNotCacheDrawing(willNotCache);
    v.setDrawingCacheBackgroundColor(color);

    return bitmap;
}
 
Example 13
Project: XFrame   File: XBitmapUtils.java   Source Code and License Vote up 6 votes
/**
 * 把一个View的对象转换成bitmap
 *
 * @param view View
 * @return Bitmap
 */
public static Bitmap getBitmapFromView2(View view) {
    if (view == null) {
        return null;
    }
    view.clearFocus();
    view.setPressed(false);

    // 能画缓存就返回false
    boolean willNotCache = view.willNotCacheDrawing();
    view.setWillNotCacheDrawing(false);
    int color = view.getDrawingCacheBackgroundColor();
    view.setDrawingCacheBackgroundColor(0);
    if (color != 0) {
        view.destroyDrawingCache();
    }
    view.buildDrawingCache();
    Bitmap cacheBitmap = view.getDrawingCache();
    if (cacheBitmap == null) {
        XPrintUtils.e( "failed getViewBitmap(" + view + ") -->"+
                    new RuntimeException());
        return null;
    }
    Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
    // Restore the view
    view.destroyDrawingCache();
    view.setWillNotCacheDrawing(willNotCache);
    view.setDrawingCacheBackgroundColor(color);
    return bitmap;
}
 
Example 14
Project: GitHub   File: ConvertUtils.java   Source Code and License Vote up 5 votes
/**
 * 把view转化为bitmap(截图)
 * 参见:http://www.cnblogs.com/lee0oo0/p/3355468.html
 */
public static Bitmap toBitmap(View view) {
    int width = view.getWidth();
    int height = view.getHeight();
    if (view instanceof ListView) {
        height = 0;
        // 获取listView实际高度
        ListView listView = (ListView) view;
        for (int i = 0; i < listView.getChildCount(); i++) {
            height += listView.getChildAt(i).getHeight();
        }
    } else if (view instanceof ScrollView) {
        height = 0;
        // 获取scrollView实际高度
        ScrollView scrollView = (ScrollView) view;
        for (int i = 0; i < scrollView.getChildCount(); i++) {
            height += scrollView.getChildAt(i).getHeight();
        }
    }
    view.setDrawingCacheEnabled(true);
    view.clearFocus();
    view.setPressed(false);
    boolean willNotCache = view.willNotCacheDrawing();
    view.setWillNotCacheDrawing(false);
    // Reset the drawing cache background color to fully transparent for the duration of this operation
    int color = view.getDrawingCacheBackgroundColor();
    view.setDrawingCacheBackgroundColor(Color.WHITE);//截图去黑色背景(透明像素)
    if (color != Color.WHITE) {
        view.destroyDrawingCache();
    }
    view.buildDrawingCache();
    Bitmap cacheBitmap = view.getDrawingCache();
    if (cacheBitmap == null) {
        return null;
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(cacheBitmap, 0, 0, null);
    canvas.save(Canvas.ALL_SAVE_FLAG);
    canvas.restore();
    if (!bitmap.isRecycled()) {
        LogUtils.verbose("recycle bitmap: " + bitmap.toString());
        bitmap.recycle();
    }
    // Restore the view
    view.destroyDrawingCache();
    view.setWillNotCacheDrawing(willNotCache);
    view.setDrawingCacheBackgroundColor(color);
    return bitmap;
}
 
Example 15
Project: GitHub   File: FloatLayoutHelper.java   Source Code and License Vote up 5 votes
@Override
public boolean onTouch(View v, MotionEvent event) {
    // 做初始化
    if (mTouchSlop == 0) {
        final ViewConfiguration configuration = ViewConfiguration.get(v
                .getContext());
        mTouchSlop = configuration.getScaledTouchSlop();
        parentViewHeight = ((View) (v.getParent())).getHeight();
        parentViewWidth = ((View) (v.getParent())).getWidth();
        ((View) (v.getParent())).getGlobalVisibleRect(parentLoction);

        ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) layoutParams;
            leftMargin = params.leftMargin;
            topMargin = params.topMargin;
            rightMargin = params.rightMargin;
            bottomMargin = params.bottomMargin;
        }

    }

    int action = event.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            isDrag = false;
            (v.getParent()).requestDisallowInterceptTouchEvent(true);
            lastPosX = (int) event.getX();
            lastPosY = (int) event.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            if (Math.abs(event.getX() - lastPosX) > mTouchSlop
                    || Math.abs(event.getY() - lastPosY) > mTouchSlop) {
                isDrag = true;
            }
            if (isDrag) {
                int posX = (int) event.getRawX();
                int posY = (int) event.getRawY();
                int rParentX = posX - parentLoction.left;
                int rParentY = posY - parentLoction.top;
                int width = v.getWidth();
                int height = v.getHeight();
                int translateY = rParentY - height / 2;
                int translateX = rParentX - width / 2;
                int curTranslateX = translateX - v.getLeft() - leftMargin - mAdjuster.left;
                v.setTranslationX(curTranslateX);
                int curTranslateY = translateY - v.getTop() - topMargin/* - mAdjuster.top*/;
                if (curTranslateY + v.getHeight() + v.getTop() + bottomMargin/* + mAdjuster.bottom */> parentViewHeight) {
                    curTranslateY = parentViewHeight - v.getHeight()
                            - v.getTop() - bottomMargin/* - mAdjuster.bottom*/;
                }
                if (curTranslateY + v.getTop() - topMargin/* - mAdjuster.top*/ < 0) {
                    curTranslateY = -v.getTop() + topMargin/* + mAdjuster.top*/;
                }
                v.setTranslationY(curTranslateY);
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            doPullOverAnimation(v);
            (v.getParent()).requestDisallowInterceptTouchEvent(false);
            v.setPressed(false);
            break;
    }
    return isDrag;
}
 
Example 16
Project: GitHub   File: FloatLayoutHelper.java   Source Code and License Vote up 5 votes
@Override
public boolean onTouch(View v, MotionEvent event) {
    // 做初始化
    if (mTouchSlop == 0) {
        final ViewConfiguration configuration = ViewConfiguration.get(v
                .getContext());
        mTouchSlop = configuration.getScaledTouchSlop();
        parentViewHeight = ((View) (v.getParent())).getHeight();
        parentViewWidth = ((View) (v.getParent())).getWidth();
        ((View) (v.getParent())).getGlobalVisibleRect(parentLoction);

        ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) layoutParams;
            leftMargin = params.leftMargin;
            topMargin = params.topMargin;
            rightMargin = params.rightMargin;
            bottomMargin = params.bottomMargin;
        }

    }

    int action = event.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            isDrag = false;
            (v.getParent()).requestDisallowInterceptTouchEvent(true);
            lastPosX = (int) event.getX();
            lastPosY = (int) event.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            if (Math.abs(event.getX() - lastPosX) > mTouchSlop
                    || Math.abs(event.getY() - lastPosY) > mTouchSlop) {
                isDrag = true;
            }
            if (isDrag) {
                int posX = (int) event.getRawX();
                int posY = (int) event.getRawY();
                int rParentX = posX - parentLoction.left;
                int rParentY = posY - parentLoction.top;
                int width = v.getWidth();
                int height = v.getHeight();
                int translateY = rParentY - height / 2;
                int translateX = rParentX - width / 2;
                int curTranslateX = translateX - v.getLeft() - leftMargin - mAdjuster.left;
                v.setTranslationX(curTranslateX);
                int curTranslateY = translateY - v.getTop() - topMargin/* - mAdjuster.top*/;
                if (curTranslateY + v.getHeight() + v.getTop() + bottomMargin/* + mAdjuster.bottom */> parentViewHeight) {
                    curTranslateY = parentViewHeight - v.getHeight()
                            - v.getTop() - bottomMargin/* - mAdjuster.bottom*/;
                }
                if (curTranslateY + v.getTop() - topMargin/* - mAdjuster.top*/ < 0) {
                    curTranslateY = -v.getTop() + topMargin/* + mAdjuster.top*/;
                }
                v.setTranslationY(curTranslateY);
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            doPullOverAnimation(v);
            (v.getParent()).requestDisallowInterceptTouchEvent(false);
            v.setPressed(false);
            break;
    }
    return isDrag;
}
 
Example 17
Project: airgram   File: ActionBarMenuItem.java   Source Code and License Vote up 5 votes
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
        if (hasSubMenu() && (popupWindow == null || popupWindow != null && !popupWindow.isShowing())) {
            showMenuRunnable = new Runnable() {
                @Override
                public void run() {
                    if (getParent() != null) {
                        getParent().requestDisallowInterceptTouchEvent(true);
                    }
                    toggleSubMenu();
                }
            };
            AndroidUtilities.runOnUIThread(showMenuRunnable, 200);
        }
    } else if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
        if (hasSubMenu() && (popupWindow == null || popupWindow != null && !popupWindow.isShowing())) {
            if (event.getY() > getHeight()) {
                if (getParent() != null) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                toggleSubMenu();
                return true;
            }
        } else if (popupWindow != null && popupWindow.isShowing()) {
            getLocationOnScreen(location);
            float x = event.getX() + location[0];
            float y = event.getY() + location[1];
            popupLayout.getLocationOnScreen(location);
            x -= location[0];
            y -= location[1];
            selectedMenuView = null;
            for (int a = 0; a < popupLayout.getItemsCount(); a++) {
                View child = popupLayout.getItemAt(a);
                child.getHitRect(rect);
                if ((Integer) child.getTag() < 100) {
                    if (!rect.contains((int) x, (int) y)) {
                        child.setPressed(false);
                        child.setSelected(false);
                        if (Build.VERSION.SDK_INT == 21) {
                            child.getBackground().setVisible(false, false);
                        }
                    } else {
                        child.setPressed(true);
                        child.setSelected(true);
                        if (Build.VERSION.SDK_INT >= 21) {
                            if (Build.VERSION.SDK_INT == 21) {
                                child.getBackground().setVisible(true, false);
                            }
                            child.drawableHotspotChanged(x, y - child.getTop());
                        }
                        selectedMenuView = child;
                    }
                }
            }
        }
    } else if (popupWindow != null && popupWindow.isShowing() && event.getActionMasked() == MotionEvent.ACTION_UP) {
        if (selectedMenuView != null) {
            selectedMenuView.setSelected(false);
            if (parentMenu != null) {
                parentMenu.onItemClick((Integer) selectedMenuView.getTag());
            } else if (delegate != null) {
                delegate.onItemClick((Integer) selectedMenuView.getTag());
            }
            popupWindow.dismiss(allowCloseAnimation);
        } else {
            popupWindow.dismiss();
        }
    } else {
        if (selectedMenuView != null) {
            selectedMenuView.setSelected(false);
            selectedMenuView = null;
        }
    }
    return super.onTouchEvent(event);
}
 
Example 18
Project: exciting-app   File: AbsHListView.java   Source Code and License Vote up 5 votes
private boolean startScrollIfNeeded(int x) {
	// Check if we have moved far enough that it looks more like a
	// scroll than a tap
	final int deltaX = x - mMotionX;
	final int distance = Math.abs(deltaX);
	final boolean overscroll = getScrollX() != 0;
	if (overscroll || distance > mTouchSlop) {
		createScrollingCache();
		if (overscroll) {
			mTouchMode = TOUCH_MODE_OVERSCROLL;
			mMotionCorrection = 0;
		} else {
			mTouchMode = TOUCH_MODE_SCROLL;
			mMotionCorrection = deltaX > 0 ? mTouchSlop : -mTouchSlop;
		}
		final Handler handler = getHandler();
		// Handler should not be null unless the AbsListView is not attached
		// to a
		// window, which would make it very hard to scroll it... but the
		// monkeys
		// say it's possible.
		if (handler != null) {
			handler.removeCallbacks(mPendingCheckForLongPress);
		}
		setPressed(false);
		View motionView = getChildAt(mMotionPosition - mFirstPosition);
		if (motionView != null) {
			motionView.setPressed(false);
		}
		reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
		// Time to start stealing events! Once we've stolen them, don't let
		// anyone
		// steal from us
		final ViewParent parent = getParent();
		if (parent != null) {
			parent.requestDisallowInterceptTouchEvent(true);
		}
		scrollIfNeeded(x);
		return true;
	}

	return false;
}
 
Example 19
Project: PlusGram   File: ActionBarMenuItem.java   Source Code and License Vote up 5 votes
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
        if (hasSubMenu() && (popupWindow == null || popupWindow != null && !popupWindow.isShowing())) {
            showMenuRunnable = new Runnable() {
                @Override
                public void run() {
                    if (getParent() != null) {
                        getParent().requestDisallowInterceptTouchEvent(true);
                    }
                    toggleSubMenu();
                }
            };
            AndroidUtilities.runOnUIThread(showMenuRunnable, 200);
        }
    } else if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
        if (hasSubMenu() && (popupWindow == null || popupWindow != null && !popupWindow.isShowing())) {
            if (event.getY() > getHeight()) {
                if (getParent() != null) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                toggleSubMenu();
                return true;
            }
        } else if (popupWindow != null && popupWindow.isShowing()) {
            getLocationOnScreen(location);
            float x = event.getX() + location[0];
            float y = event.getY() + location[1];
            popupLayout.getLocationOnScreen(location);
            x -= location[0];
            y -= location[1];
            selectedMenuView = null;
            for (int a = 0; a < popupLayout.getItemsCount(); a++) {
                View child = popupLayout.getItemAt(a);
                child.getHitRect(rect);
                if ((Integer) child.getTag() < 100) {
                    if (!rect.contains((int) x, (int) y)) {
                        child.setPressed(false);
                        child.setSelected(false);
                        if (Build.VERSION.SDK_INT == 21) {
                            child.getBackground().setVisible(false, false);
                        }
                    } else {
                        child.setPressed(true);
                        child.setSelected(true);
                        if (Build.VERSION.SDK_INT >= 21) {
                            if (Build.VERSION.SDK_INT == 21) {
                                child.getBackground().setVisible(true, false);
                            }
                            child.drawableHotspotChanged(x, y - child.getTop());
                        }
                        selectedMenuView = child;
                    }
                }
            }
        }
    } else if (popupWindow != null && popupWindow.isShowing() && event.getActionMasked() == MotionEvent.ACTION_UP) {
        if (selectedMenuView != null) {
            selectedMenuView.setSelected(false);
            if (parentMenu != null) {
                parentMenu.onItemClick((Integer) selectedMenuView.getTag());
            } else if (delegate != null) {
                delegate.onItemClick((Integer) selectedMenuView.getTag());
            }
            popupWindow.dismiss(allowCloseAnimation);
        } else {
            popupWindow.dismiss();
        }
    } else {
        if (selectedMenuView != null) {
            selectedMenuView.setSelected(false);
            selectedMenuView = null;
        }
    }
    return super.onTouchEvent(event);
}
 
Example 20
Project: vlayout   File: FloatLayoutHelper.java   Source Code and License Vote up 5 votes
@Override
public boolean onTouch(View v, MotionEvent event) {
    // 做初始化
    if (mTouchSlop == 0) {
        final ViewConfiguration configuration = ViewConfiguration.get(v
                .getContext());
        mTouchSlop = configuration.getScaledTouchSlop();
        parentViewHeight = ((View) (v.getParent())).getHeight();
        parentViewWidth = ((View) (v.getParent())).getWidth();
        ((View) (v.getParent())).getGlobalVisibleRect(parentLoction);

        ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) layoutParams;
            leftMargin = params.leftMargin;
            topMargin = params.topMargin;
            rightMargin = params.rightMargin;
            bottomMargin = params.bottomMargin;
        }

    }

    int action = event.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            isDrag = false;
            (v.getParent()).requestDisallowInterceptTouchEvent(true);
            lastPosX = (int) event.getX();
            lastPosY = (int) event.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            if (Math.abs(event.getX() - lastPosX) > mTouchSlop
                    || Math.abs(event.getY() - lastPosY) > mTouchSlop) {
                isDrag = true;
            }
            if (isDrag) {
                int posX = (int) event.getRawX();
                int posY = (int) event.getRawY();
                int rParentX = posX - parentLoction.left;
                int rParentY = posY - parentLoction.top;
                int width = v.getWidth();
                int height = v.getHeight();
                int translateY = rParentY - height / 2;
                int translateX = rParentX - width / 2;
                int curTranslateX = translateX - v.getLeft() - leftMargin - mAdjuster.left;
                v.setTranslationX(curTranslateX);
                int curTranslateY = translateY - v.getTop() - topMargin/* - mAdjuster.top*/;
                if (curTranslateY + v.getHeight() + v.getTop() + bottomMargin/* + mAdjuster.bottom */ > parentViewHeight) {
                    curTranslateY = parentViewHeight - v.getHeight()
                            - v.getTop() - bottomMargin/* - mAdjuster.bottom*/;
                }
                if (curTranslateY + v.getTop() - topMargin/* - mAdjuster.top*/ < 0) {
                    curTranslateY = -v.getTop() + topMargin/* + mAdjuster.top*/;
                }
                v.setTranslationY(curTranslateY);
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            doPullOverAnimation(v);
            (v.getParent()).requestDisallowInterceptTouchEvent(false);
            v.setPressed(false);
            break;
    }
    return isDrag;
}