List of usage examples for android.view MotionEvent getRawX
public final float getRawX()
From source file:info.tellmetime.TellmetimeActivity.java
/** * Processes touch events for FrameLayout overlaying whole activity. * * If the touch was not on the panel then it changes background color according to the touch * position as long as the touch event was recognized as a movement, * or toggles panel visibility if it was a tap. * * @return true if the touch event was consumed. *//*from w w w . j av a2 s .c o m*/ @Override public boolean onTouch(View view, MotionEvent event) { // Coordinates of current touch point. final float rawX = event.getRawX(); final float rawY = event.getRawY(); switch (MotionEventCompat.getActionMasked(event)) { case (MotionEvent.ACTION_DOWN): // This action signals that a new touch gesture begins, so we cache its starting point. mStartX = rawX; mStartY = rawY; // Schedule a Runnable to run in 1 second. mLongPressHandler.postDelayed(mLongPressed, 1000); return true; case (MotionEvent.ACTION_MOVE): // Distance between starting point and current one. final double distance = Math.sqrt(Math.pow(rawX - mStartX, 2) + Math.pow(rawY - mStartY, 2)); if (distance > mTouchSlop) { // If the user exceeded the slop value, then we start to track the movement. isSlopped = true; // Cancel scheduled Runnable. mLongPressHandler.removeCallbacks(mLongPressed); if (mHider.isVisible()) { // Hide panel to present only the clock. mHider.hide(); } else { if (mBackgroundMode != MODE_BACKGROUND_SOLID) return true; // Change the background color according to the touch position. if (event.getPointerCount() > 1) { // If the user is using more than one finger, then we change saturation. // Saturation depends on top <-> bottom movement of second finger. mHSV[1] = MotionEventCompat.getY(event, 1) / mScreenHeight; } else { // If it's a single finger gesture, then we change hue and value. // Hue depends on left <-> right movement. mHSV[0] = (float) Math.floor(rawX / mScreenWidth * 360); // Value depends on up <-> down movement. mHSV[2] = rawY / mScreenHeight; } mBackgroundColor = Color.HSVToColor(mHSV); mSurface.setBackgroundColor(mBackgroundColor); } } else { // If required distance is not exceeded, we only unset the slop flag. isSlopped = false; } return true; case (MotionEvent.ACTION_UP): mLongPressHandler.removeCallbacks(mLongPressed); // This action ends every touch interaction, so if the event was just a tap, // i.e. distance hasn't exceed the slop value, then it concerns panel. if (!isSlopped && !wasModeToggled) { // If the touch gesture was just a tap then toggle panel visibility. mHider.toggle(); } // As the gesture ended, we need to unset the slop and toggle flag. isSlopped = false; wasModeToggled = false; return true; case (MotionEvent.ACTION_POINTER_UP): // Action occurs when second or farther finger goes up. isSlopped = true; return true; default: return super.onTouchEvent(event); } }
From source file:com.marshalchen.common.uimodule.superlistview.SwipeDismissListViewTouchListener.java
@Override public boolean onTouch(View view, MotionEvent motionEvent) { if (mViewWidth < 2) { mViewWidth = mListView.getWidth(); }//from w w w . j a v a 2 s . c o m switch (MotionEventCompat.getActionMasked(motionEvent)) { case MotionEvent.ACTION_DOWN: { if (mPaused) { return false; } // TODO: ensure this is a finger, and set a flag // 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 child; for (int i = 0; i < childCount; i++) { child = mListView.getChildAt(i); child.getHitRect(rect); if (rect.contains(x, y)) { mDownView = child; mDownViewProxy = AnimatorProxy.wrap(child); break; } } if (mDownView != null) { mDownX = motionEvent.getRawX(); mDownY = motionEvent.getRawY(); mDownPosition = mListView.getPositionForView(mDownView); if (mCallbacks.canDismiss(mDownPosition)) { mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(motionEvent); } else { mDownView = null; mDownViewProxy = null; } } return false; } case MotionEvent.ACTION_CANCEL: { if (mVelocityTracker == null) { break; } if (mDownView != null && mSwiping) { // cancel animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null); } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownViewProxy = null; mDownPosition = ListView.INVALID_POSITION; mSwiping = false; break; } case MotionEvent.ACTION_UP: { if (mVelocityTracker == null) { break; } float deltaX = motionEvent.getRawX() - mDownX; mVelocityTracker.addMovement(motionEvent); mVelocityTracker.computeCurrentVelocity(1000); float velocityX = mVelocityTracker.getXVelocity(); float absVelocityX = Math.abs(velocityX); float absVelocityY = Math.abs(mVelocityTracker.getYVelocity()); boolean dismiss = false; boolean dismissRight = false; if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) { dismiss = true; dismissRight = deltaX > 0; } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity && absVelocityY < absVelocityX && mSwiping) { // dismiss only if flinging in the same direction as dragging dismiss = (velocityX < 0) == (deltaX < 0); dismissRight = mVelocityTracker.getXVelocity() > 0; } if (dismiss && mDownPosition != ListView.INVALID_POSITION) { // dismiss final View downView = mDownView; // mDownView gets null'd before animation ends final int downPosition = mDownPosition; ++mDismissAnimationRefCount; animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0) .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { performDismiss(downView, downPosition); } }); } else { animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null); } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownViewProxy = null; mDownPosition = ListView.INVALID_POSITION; mSwiping = false; break; } case MotionEvent.ACTION_MOVE: { if (mVelocityTracker == null || mPaused) { break; } mVelocityTracker.addMovement(motionEvent); float deltaX = motionEvent.getRawX() - mDownX; float deltaY = motionEvent.getRawY() - mDownY; if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) { mSwiping = true; mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop); mListView.requestDisallowInterceptTouchEvent(true); // Cancel ListView's touch (un-highlighting the item) MotionEvent cancelEvent = MotionEvent.obtain(motionEvent); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT)); mListView.onTouchEvent(cancelEvent); cancelEvent.recycle(); } if (mSwiping) { mDownViewProxy.setTranslationX(deltaX - mSwipingSlop); mDownViewProxy.setAlpha(Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); return true; } break; } } return false; }
From source file:com.quentindommerc.superlistview.SwipeDismissListViewTouchListener.java
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override/*w w w . ja va 2 s .c om*/ public boolean onTouch(View view, MotionEvent motionEvent) { if (mViewWidth < 2) { mViewWidth = mListView.getWidth(); } switch (MotionEventCompat.getActionMasked(motionEvent)) { case MotionEvent.ACTION_DOWN: { if (mPaused) { return false; } // TODO: ensure this is a finger, and set a flag // 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 child; for (int i = 0; i < childCount; i++) { child = mListView.getChildAt(i); child.getHitRect(rect); if (rect.contains(x, y)) { mDownView = child; break; } } if (mDownView != null) { mDownX = motionEvent.getRawX(); mDownY = motionEvent.getRawY(); mDownPosition = mListView.getPositionForView(mDownView); if (mCallbacks.canDismiss(mDownPosition)) { mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(motionEvent); } else { mDownView = null; } } return false; } case MotionEvent.ACTION_CANCEL: { if (mVelocityTracker == null) { break; } if (mDownView != null && mSwiping) { // cancel animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null); } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownPosition = ListView.INVALID_POSITION; mSwiping = false; break; } case MotionEvent.ACTION_UP: { if (mVelocityTracker == null) { break; } float deltaX = motionEvent.getRawX() - mDownX; mVelocityTracker.addMovement(motionEvent); mVelocityTracker.computeCurrentVelocity(1000); float velocityX = mVelocityTracker.getXVelocity(); float absVelocityX = Math.abs(velocityX); float absVelocityY = Math.abs(mVelocityTracker.getYVelocity()); boolean dismiss = false; boolean dismissRight = false; if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) { dismiss = true; dismissRight = deltaX > 0; } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity && absVelocityY < absVelocityX && mSwiping) { // dismiss only if flinging in the same direction as dragging dismiss = (velocityX < 0) == (deltaX < 0); dismissRight = mVelocityTracker.getXVelocity() > 0; } if (dismiss && mDownPosition != ListView.INVALID_POSITION) { // dismiss final View downView = mDownView; // mDownView gets null'd before animation ends final int downPosition = mDownPosition; ++mDismissAnimationRefCount; animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0) .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { performDismiss(downView, downPosition); } }); } else { animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null); } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownPosition = ListView.INVALID_POSITION; mSwiping = false; break; } case MotionEvent.ACTION_MOVE: { if (mVelocityTracker == null || mPaused) { break; } mVelocityTracker.addMovement(motionEvent); float deltaX = motionEvent.getRawX() - mDownX; float deltaY = motionEvent.getRawY() - mDownY; if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) { mSwiping = true; mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop); mListView.requestDisallowInterceptTouchEvent(true); // Cancel ListView's touch (un-highlighting the item) MotionEvent cancelEvent = MotionEvent.obtain(motionEvent); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT)); mListView.onTouchEvent(cancelEvent); cancelEvent.recycle(); } if (mSwiping) { if (AnimatorProxy.NEEDS_PROXY) { AnimatorProxy proxy = AnimatorProxy.wrap(mDownView); proxy.setTranslationX(deltaX - mSwipingSlop); proxy.setAlpha(Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); } else { mDownView.setTranslationX(deltaX - mSwipingSlop); mDownView.setAlpha(Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); } return true; } break; } } return false; }
From source file:com.dpg.kodimote.view.SwipeableRecyclerViewTouchListener.java
private boolean handleTouchEvent(MotionEvent motionEvent) { if (mViewWidth < 2) { mViewWidth = mRecyclerView.getWidth(); }/*from w ww. j a v a 2 s . c o m*/ Log.d("Swipe", motionEvent.getActionMasked() + ""); switch (motionEvent.getActionMasked()) { case MotionEvent.ACTION_DOWN: { if (mPaused) { break; } // Find the child view that was touched (perform a hit test) Rect rect = new Rect(); int childCount = mRecyclerView.getChildCount(); int[] listViewCoords = new int[2]; mRecyclerView.getLocationOnScreen(listViewCoords); int x = (int) motionEvent.getRawX() - listViewCoords[0]; int y = (int) motionEvent.getRawY() - listViewCoords[1]; View child; for (int i = 0; i < childCount; i++) { child = mRecyclerView.getChildAt(i); child.getHitRect(rect); if (rect.contains(x, y)) { mDownView = child; break; } } if (mDownView != null && mAnimatingPosition != mRecyclerView.getChildLayoutPosition(mDownView)) { mAlpha = ViewCompat.getAlpha(mDownView); mDownX = motionEvent.getRawX(); mDownY = motionEvent.getRawY(); mDownPosition = mRecyclerView.getChildLayoutPosition(mDownView); mSwipingLeft = mSwipeListener.canSwipeLeft(mDownPosition); mSwipingRight = mSwipeListener.canSwipeRight(mDownPosition); if (mSwipingLeft || mSwipingRight) { mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(motionEvent); } else { mDownView = null; } } break; } case MotionEvent.ACTION_CANCEL: { if (mVelocityTracker == null) { break; } if (mDownView != null && mSwiping) { // cancel ViewCompat.animate(mDownView).translationX(0).alpha(mAlpha).setDuration(mAnimationTime) .setListener(null); } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownPosition = ListView.INVALID_POSITION; mSwiping = false; break; } case MotionEvent.ACTION_UP: { if (mVelocityTracker == null) { break; } mFinalDelta = motionEvent.getRawX() - mDownX; mVelocityTracker.addMovement(motionEvent); mVelocityTracker.computeCurrentVelocity(1000); float velocityX = mVelocityTracker.getXVelocity(); float absVelocityX = Math.abs(velocityX); float absVelocityY = Math.abs(mVelocityTracker.getYVelocity()); boolean dismiss = false; boolean dismissRight = false; if (Math.abs(mFinalDelta) > mViewWidth / 2 && mSwiping) { dismiss = true; dismissRight = mFinalDelta > 0; } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity && absVelocityY < absVelocityX && mSwiping) { // dismiss only if flinging in the same direction as dragging dismiss = (velocityX < 0) == (mFinalDelta < 0); dismissRight = mVelocityTracker.getXVelocity() > 0; } if (dismiss && mDownPosition != mAnimatingPosition && mDownPosition != ListView.INVALID_POSITION) { // dismiss final View downView = mDownView; // mDownView gets null'd before animation ends final int downPosition = mDownPosition; ++mDismissAnimationRefCount; mAnimatingPosition = mDownPosition; ViewCompat.animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0) .setDuration(mAnimationTime).setListener(new ViewPropertyAnimatorListener() { @Override public void onAnimationStart(View view) { // Do nothing. } @Override public void onAnimationEnd(View view) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { performDismiss(downView, downPosition); } } @Override public void onAnimationCancel(View view) { // Do nothing. } }); } else { // cancel ViewCompat.animate(mDownView).translationX(0).alpha(mAlpha).setDuration(mAnimationTime) .setListener(null); } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownPosition = ListView.INVALID_POSITION; mSwiping = false; break; } case MotionEvent.ACTION_MOVE: { if (mVelocityTracker == null || mPaused) { break; } mVelocityTracker.addMovement(motionEvent); float deltaX = motionEvent.getRawX() - mDownX; float deltaY = motionEvent.getRawY() - mDownY; if (!mSwiping && Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) { mSwiping = true; mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop); } if (deltaX < 0 && !mSwipingLeft) mSwiping = false; if (deltaX > 0 && !mSwipingRight) mSwiping = false; if (mSwiping) { ViewCompat.setTranslationX(mDownView, deltaX - mSwipingSlop); ViewCompat.setAlpha(mDownView, Math.max(0f, Math.min(mAlpha, mAlpha * (1f - Math.abs(deltaX) / mViewWidth)))); return true; } break; } } return false; }
From source file:com.skyousuke.ivtool.windows.MainWindow.java
private void setTouchListener() { layout.setOnTouchListener(new View.OnTouchListener() { private WindowManager.LayoutParams latestParams = layoutParams; int x, y; float touchX, touchY; @Override/*from ww w . j a va 2 s . c o m*/ public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: x = latestParams.x; y = latestParams.y; touchX = motionEvent.getRawX(); touchY = motionEvent.getRawY(); break; case MotionEvent.ACTION_MOVE: latestParams.x = (int) (x + motionEvent.getRawX() - touchX); latestParams.y = (int) (y + motionEvent.getRawY() - touchY); WindowManagerUtils.keepInScreen(layout, latestParams, screenWidth, screenHeight); windowManager.updateViewLayout(layout, latestParams); break; case MotionEvent.ACTION_OUTSIDE: lostFocus(500); break; } return false; } }); }
From source file:org.creativecommons.thelist.swipedismiss.SwipeDismissRecyclerViewTouchListener.java
@Override public boolean onTouch(View view, MotionEvent motionEvent) { if (mViewWidth < 2) { mViewWidth = mRecyclerView.getWidth(); }//from w w w. j a v a 2 s . com switch (motionEvent.getActionMasked()) { case MotionEvent.ACTION_DOWN: { if (mPaused) { return false; } // TODO: ensure this is a finger, and set a flag // Find the child view that was touched (perform a hit test) Rect rect = new Rect(); int childCount = mRecyclerView.getChildCount(); int[] listViewCoords = new int[2]; mRecyclerView.getLocationOnScreen(listViewCoords); int x = (int) motionEvent.getRawX() - listViewCoords[0]; int y = (int) motionEvent.getRawY() - listViewCoords[1]; View child; for (int i = 0; i < childCount; i++) { child = mRecyclerView.getChildAt(i); child.getHitRect(rect); if (rect.contains(x, y)) { mDownView = child; break; } } if (mDownView != null) { mDownX = motionEvent.getRawX(); mDownY = motionEvent.getRawY(); mDownPosition = mRecyclerView.getChildPosition(mDownView); if (mCallbacks.canDismiss(mDownPosition)) { mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(motionEvent); } else { mDownView = null; } } return false; } //OnTouch case MotionEvent.ACTION_CANCEL: { if (mVelocityTracker == null) { break; } if (mDownView != null && mSwiping) { // cancel //TODO: DOES THIS WORK animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null); } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownPosition = ListView.INVALID_POSITION; mSwiping = false; mRefreshLayout.setEnabled(true); break; } //ACTION_CANCEL case MotionEvent.ACTION_UP: { if (mVelocityTracker == null) { break; } float deltaX = motionEvent.getRawX() - mDownX; mVelocityTracker.addMovement(motionEvent); mVelocityTracker.computeCurrentVelocity(1000); float velocityX = mVelocityTracker.getXVelocity(); float absVelocityX = Math.abs(velocityX); float absVelocityY = Math.abs(mVelocityTracker.getYVelocity()); // float velocityX = Math.abs(mVelocityTracker.getXVelocity()); // float velocityY = Math.abs(mVelocityTracker.getYVelocity()); boolean dismiss = false; boolean dismissRight = false; if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) { dismiss = true; dismissRight = deltaX > 0; } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity && absVelocityY < absVelocityX && mSwiping) { // dismiss only if flinging in the same direction as dragging dismiss = (velocityX < 0) == (deltaX < 0); dismissRight = mVelocityTracker.getXVelocity() > 0; } if (dismiss && mDownPosition != ListView.INVALID_POSITION) { // dismiss final View downView = mDownView; // mDownView gets null'd before animation ends final int downPosition = mDownPosition; ++mDismissAnimationRefCount; //TODO: add animation lock // synchronized (mAnimationLock){ // if(mAnimatedViews.contains(downView)){ // break; // } // ++mDismissAnimationRefCount; // mAnimatedViews.add(downView); // } animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0) .setDuration(mAnimationTime) .setListener(new com.nineoldandroids.animation.AnimatorListenerAdapter() { @Override public void onAnimationEnd(com.nineoldandroids.animation.Animator animation) { performDismiss(downView, downPosition); } }); } else { // cancel animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null); } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownPosition = ListView.INVALID_POSITION; mSwiping = false; mRefreshLayout.setEnabled(true); break; } //ACTION_UP case MotionEvent.ACTION_MOVE: { if (mVelocityTracker == null || mPaused) { break; } mVelocityTracker.addMovement(motionEvent); float deltaX = motionEvent.getRawX() - mDownX; float deltaY = motionEvent.getRawY() - mDownY; if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) { mSwiping = true; mRefreshLayout.setEnabled(false); mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop); mRecyclerView.requestDisallowInterceptTouchEvent(true); // CancelLogin ListView's touch (un-highlighting the item) MotionEvent cancelEvent = MotionEvent.obtain(motionEvent); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); mRecyclerView.onTouchEvent(cancelEvent); cancelEvent.recycle(); } if (mSwiping) { mDownView.setTranslationX(deltaX - mSwipingSlop); mDownView.setAlpha(Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); return true; // setTranslationX(mDownView, deltaX); // setAlpha(mDownView, Math.max(0f, Math.min(1f, // 1f - 2f * Math.abs(deltaX) / mViewWidth))); // return true; } break; } } return false; }
From source file:org.rm3l.ddwrt.tiles.admin.nvram.AdminNVRAMTile.java
public AdminNVRAMTile(@NotNull SherlockFragment parentFragment, @NotNull Bundle arguments, @Nullable Router router) {//ww w . java 2s. c o m super(parentFragment, arguments, router, R.layout.tile_admin_nvram, R.id.tile_admin_nvram_togglebutton); sortIds.put(R.id.tile_admin_nvram_sort_default, 11); sortIds.put(R.id.tile_admin_nvram_sort_asc, 12); sortIds.put(R.id.tile_admin_nvram_sort_desc, 13); this.mNvramInfoDefaultSorting = new NVRAMInfo(); mRecyclerView = (RecyclerView) layout.findViewById(R.id.tile_admin_nvram_ListView); // use this setting to improve performance if you know that changes // in content do not change the layout size of the RecyclerView // allows for optimizations if all items are of the same size: mRecyclerView.setHasFixedSize(true); // use a linear layout manager mLayoutManager = new LinearLayoutManager(mParentFragmentActivity); mLayoutManager.scrollToPosition(0); mRecyclerView.setLayoutManager(mLayoutManager); // specify an adapter (see also next example) mAdapter = new NVRAMDataRecyclerViewAdapter(mParentFragmentActivity, router, mNvramInfoDefaultSorting); mRecyclerView.setAdapter(mAdapter); //Create Options Menu final ImageButton tileMenu = (ImageButton) layout.findViewById(R.id.tile_admin_nvram_menu); if (!isThemeLight(mParentFragmentActivity, mRouter.getUuid())) { //Set menu background to white tileMenu.setImageResource(R.drawable.abs__ic_menu_moreoverflow_normal_holo_dark); } tileMenu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final PopupMenu popup = new PopupMenu(mParentFragmentActivity, v); popup.setOnMenuItemClickListener(AdminNVRAMTile.this); final MenuInflater inflater = popup.getMenuInflater(); final Menu menu = popup.getMenu(); inflater.inflate(R.menu.tile_admin_nvram_options, menu); //Disable menu item from preference Integer currentSort = null; if (mParentFragmentPreferences != null) { currentSort = sortIds.inverse().get(mParentFragmentPreferences.getInt(getFormattedPrefKey(SORT), sortIds.get(R.id.tile_admin_nvram_sort_default))); } if (currentSort == null) { currentSort = R.id.tile_admin_nvram_sort_default; } final MenuItem currentSortMenuItem = menu.findItem(currentSort); if (currentSortMenuItem != null) { currentSortMenuItem.setEnabled(false); } // Locate MenuItem with ShareActionProvider final MenuItem shareMenuItem = menu.findItem(R.id.tile_admin_nvram_share); // Fetch and store ShareActionProvider mShareActionProvider = (ShareActionProvider) shareMenuItem.getActionProvider(); popup.show(); } }); //Handle for Search EditText final EditText filterEditText = (EditText) this.layout.findViewById(R.id.tile_admin_nvram_filter); //Initialize with existing search data filterEditText.setText(mParentFragmentPreferences != null ? mParentFragmentPreferences.getString(getFormattedPrefKey(LAST_SEARCH), EMPTY_STRING) : EMPTY_STRING); filterEditText.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { final int DRAWABLE_LEFT = 0; final int DRAWABLE_TOP = 1; final int DRAWABLE_RIGHT = 2; final int DRAWABLE_BOTTOM = 3; if (event.getAction() == MotionEvent.ACTION_UP) { if (event.getRawX() >= (filterEditText.getRight() - filterEditText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) { //'Clear' button - clear data, and reset everything out //Reset everything filterEditText.setText(EMPTY_STRING); final Properties mNvramInfoDefaultSortingData = mNvramInfoDefaultSorting.getData(); //Update adapter in the preferences if (mParentFragmentPreferences != null) { final Integer currentSort = sortIds.inverse() .get(mParentFragmentPreferences.getInt(getFormattedPrefKey(SORT), -1)); if (currentSort == null || currentSort <= 0) { mNvramInfoToDisplay = new HashMap<>(mNvramInfoDefaultSortingData); } else { switch (currentSort) { case R.id.tile_admin_nvram_sort_asc: //asc mNvramInfoToDisplay = new TreeMap<>(COMPARATOR_STRING_CASE_INSENSITIVE); break; case R.id.tile_admin_nvram_sort_desc: //desc mNvramInfoToDisplay = new TreeMap<>(COMPARATOR_REVERSE_STRING_CASE_INSENSITIVE); break; case R.id.tile_admin_nvram_sort_default: default: mNvramInfoToDisplay = new HashMap<>(); break; } mNvramInfoToDisplay.putAll(mNvramInfoDefaultSortingData); } } else { mNvramInfoToDisplay = new HashMap<>(mNvramInfoDefaultSortingData); } ((NVRAMDataRecyclerViewAdapter) mAdapter).setEntryList(mNvramInfoToDisplay); mAdapter.notifyDataSetChanged(); if (mParentFragmentPreferences != null) { final SharedPreferences.Editor editor = mParentFragmentPreferences.edit(); editor.putString(getFormattedPrefKey(LAST_SEARCH), EMPTY_STRING); editor.apply(); } return true; } } return false; } }); filterEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { final String textToFind = filterEditText.getText().toString(); if (isNullOrEmpty(textToFind)) { //extra-check, even though we can be pretty sure the button is enabled only if textToFind is present return true; } final String existingSearch = mParentFragmentPreferences != null ? mParentFragmentPreferences.getString(getFormattedPrefKey(LAST_SEARCH), null) : null; if (mParentFragmentPreferences != null) { if (textToFind.equalsIgnoreCase(existingSearch)) { //No need to go further as this is already the string we are looking for return true; } final SharedPreferences.Editor editor = mParentFragmentPreferences.edit(); editor.putString(getFormattedPrefKey(LAST_SEARCH), textToFind); editor.apply(); } //Filter out (and sort by user-preference) final Properties mNvramInfoDefaultSortingData = mNvramInfoDefaultSorting.getData(); //Update adapter in the preferences final Map<Object, Object> mNvramInfoToDisplayCopy; if (mParentFragmentPreferences != null) { final Integer currentSort = sortIds.inverse() .get(mParentFragmentPreferences.getInt(getFormattedPrefKey(SORT), -1)); if (currentSort == null || currentSort <= 0) { mNvramInfoToDisplayCopy = new HashMap<>(mNvramInfoDefaultSortingData); } else { switch (currentSort) { case R.id.tile_admin_nvram_sort_asc: //asc mNvramInfoToDisplayCopy = new TreeMap<>(COMPARATOR_STRING_CASE_INSENSITIVE); break; case R.id.tile_admin_nvram_sort_desc: //desc mNvramInfoToDisplayCopy = new TreeMap<>(COMPARATOR_REVERSE_STRING_CASE_INSENSITIVE); break; case R.id.tile_admin_nvram_sort_default: default: mNvramInfoToDisplayCopy = new HashMap<>(); break; } //noinspection ConstantConditions for (Map.Entry<Object, Object> entry : mNvramInfoDefaultSortingData.entrySet()) { final Object key = entry.getKey(); final Object value = entry.getValue(); if (key == null) { continue; } if (containsIgnoreCase(key.toString(), textToFind) || containsIgnoreCase(value.toString(), textToFind)) { mNvramInfoToDisplayCopy.put(key, value); } } } } else { mNvramInfoToDisplayCopy = new HashMap<>(); //noinspection ConstantConditions for (Map.Entry<Object, Object> entry : mNvramInfoDefaultSortingData.entrySet()) { final Object key = entry.getKey(); final Object value = entry.getValue(); if (key == null) { continue; } if (containsIgnoreCase(key.toString(), textToFind) || containsIgnoreCase(value.toString(), textToFind)) { mNvramInfoToDisplayCopy.put(key, value); } } } ((NVRAMDataRecyclerViewAdapter) mAdapter).setEntryList(mNvramInfoToDisplayCopy); mAdapter.notifyDataSetChanged(); return true; } return false; } }); }
From source file:com.jinfukeji.jinyihuiup.indexBannerClick.ZhiboActivity.java
@Override public boolean dispatchTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: break;// ww w. java2 s . c om case MotionEvent.ACTION_MOVE: WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); int width = wm.getDefaultDisplay().getWidth(); int height = wm.getDefaultDisplay().getHeight(); float rawX = ev.getRawX(); float rawY = ev.getRawY(); float x = ev.getX(); float y = ev.getY(); if (rawX < width / 2) { if (rawY < y) { streamVolume++; changeAudio(streamVolume); } else { streamVolume--; changeAudio(streamVolume); } } else { if (rawY < y) { } else { } } break; case MotionEvent.ACTION_UP: break; case MotionEvent.ACTION_SCROLL: break; } return super.dispatchTouchEvent(ev); }
From source file:com.antew.redditinpictures.library.widget.SwipeListView.java
@Override public boolean onTouchEvent(MotionEvent event) { // Store width of this list for usage of swipe distance detection if (mViewWidth < 2) { mViewWidth = getWidth();//from w w w . j a va 2 s . com } switch (MotionEventCompat.getActionMasked(event)) { case MotionEvent.ACTION_DOWN: int[] viewCoords = new int[2]; // Figure out where the touch occurred. getLocationOnScreen(viewCoords); int touchX = (int) event.getRawX() - viewCoords[0]; int touchY = (int) event.getRawY() - viewCoords[1]; Rect rect = new Rect(); View child; int childCount = getChildCount(); for (int i = getHeaderViewsCount(); i <= childCount; i++) { // Go through each child view (excluding headers) and see if our touch pressed it. child = getChildAt(i); if (child != null) { //Get the child hit rectangle. child.getHitRect(rect); //If the child would be hit by this press. if (rect.contains(touchX, touchY)) { // DIRECT HIT! You sunk my battleship. Now that we know which view was touched, store it off for use if a move occurs. View frontView = child.findViewById(mFrontViewId); View backView = child.findViewById(mBackViewId); // Create our view pair. mViewPair = new SwipeableViewPair(frontView, backView); break; } } } if (mViewPair != null) { // If we have a view pair, record details about the inital touch for use later. mDownX = event.getRawX(); mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(event); } break; case MotionEvent.ACTION_UP: if (mVelocityTracker != null) { // Add the movement so we can calculate velocity. mVelocityTracker.addMovement(event); mVelocityTracker.computeCurrentVelocity(1000); float deltaX = event.getRawX() - mDownX; float velocityX = Math.abs(mVelocityTracker.getXVelocity()); float velocityY = Math.abs(mVelocityTracker.getYVelocity()); if (mViewPair != null) { boolean shouldSwipe = false; // If the view has been moved a significant enough distance or if the view was flung, check to see if we should swipe it. if ((Math.abs(deltaX) > mViewWidth / 2 && mState == State.SWIPING) || (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity && velocityX > velocityY)) { if (mSwipeDirection == SWIPE_DIRECTION_BOTH) { // If the list is setup to swipe in either direction, just let it go. shouldSwipe = true; } else if (mSwipeDirection == SWIPE_DIRECTION_LEFT && deltaX < 0) { // If the list is only setup to swipe left, then only allow swiping to the left. shouldSwipe = true; } else if (mSwipeDirection == SWIPE_DIRECTION_RIGHT && deltaX > 0) { // If the list is only setup to swipe right, then only allow swiping to the right. shouldSwipe = true; } } if (shouldSwipe) { // If a swipe should occur meaning someone has let go of a view they were moving and it was far/fast enough for us to consider it a swipe start the animations. mViewPair.mFrontView.animate().translationX(deltaX >= 0 ? mViewWidth : -mViewWidth).alpha(0) .setDuration(mAnimationTime); mViewPair.mBackView.animate().alpha(1).setDuration(mAnimationTime); // Now that the item is open, store it off so we can close it when we scroll if needed. mSwipedViews.put(mViewPair.hashCode(), mViewPair); // Clear out current variables as they are no longer needed and recycle the velocity tracker. resetState(); } else { // If the user let go of the view and we don't think the swipe was intended to occur (it was cancelled basically) reset the views. // Make sure the back disappears, since if it has buttons these can intercept touches from the front view. mViewPair.mBackView.setVisibility(View.GONE); mViewPair.mFrontView.animate().translationX(0).alpha(1).setDuration(mAnimationTime); // Clear out current variables as they are no longer needed and recycle the velocity tracker. resetState(); } } } break; case MotionEvent.ACTION_MOVE: if (mVelocityTracker != null && mState != State.SCROLLING) { // If this is an initial movement and we aren't already swiping. // Add the movement so we can calculate velocity. mVelocityTracker.addMovement(event); mVelocityTracker.computeCurrentVelocity(1000); float deltaX = event.getRawX() - mDownX; float velocityX = Math.abs(mVelocityTracker.getXVelocity()); float velocityY = Math.abs(mVelocityTracker.getYVelocity()); // If the movement has been more than what is considered slop, and they are clearing moving horizontal not vertical. if (Math.abs(deltaX) > mTouchSlop && velocityX > velocityY) { boolean initiateSwiping = false; if (mSwipeDirection == SWIPE_DIRECTION_BOTH) { // If the list is setup to swipe in either direction, just let it go. initiateSwiping = true; } else if (mSwipeDirection == SWIPE_DIRECTION_LEFT && deltaX < 0) { // If the list is only setup to swipe left, then only allow swiping to the left. initiateSwiping = true; } else if (mSwipeDirection == SWIPE_DIRECTION_RIGHT && deltaX > 0) { // If the list is only setup to swipe right, then only allow swiping to the right. initiateSwiping = true; } if (initiateSwiping) { ViewParent parent = getParent(); if (parent != null) { // Don't allow parent to intercept touch (prevents NavigationDrawers from intercepting when near the bezel). parent.requestDisallowInterceptTouchEvent(true); } // Change our state to swiping to start tranforming the item. changeState(State.SWIPING); // Make sure that touches aren't intercepted. requestDisallowInterceptTouchEvent(true); // Cancel ListView's touch to prevent it from being focused. MotionEvent cancelEvent = MotionEvent.obtain(event); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); super.onTouchEvent(cancelEvent); } else { // Otherwise we need to cancel the touch event to prevent accidentally selecting the item and also preventing the swipe in the wrong direction or an incomplete touch from moving the view. MotionEvent cancelEvent = MotionEvent.obtain(event); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); super.onTouchEvent(cancelEvent); } } if (mState == State.SWIPING && mViewPair != null) { // Make sure the back is visible. mViewPair.mBackView.setVisibility(View.VISIBLE); //Fade the back in and front out as they move. mViewPair.mBackView.setAlpha(Math.min(1f, 2f * Math.abs(deltaX) / mViewWidth)); mViewPair.mFrontView.setTranslationX(deltaX); mViewPair.mFrontView .setAlpha(Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); return true; } } break; } return super.onTouchEvent(event); }
From source file:com.davidtpate.swipelistview.SwipeListView.java
@Override public boolean onTouchEvent(MotionEvent event) { // Store width of this list for usage of swipe distance detection if (mViewWidth < 2) { mViewWidth = getWidth();//from w w w . ja v a 2 s.c o m } switch (MotionEventCompat.getActionMasked(event)) { case MotionEvent.ACTION_DOWN: int[] viewCoords = new int[2]; // Figure out where the touch occurred. getLocationOnScreen(viewCoords); int touchX = (int) event.getRawX() - viewCoords[0]; int touchY = (int) event.getRawY() - viewCoords[1]; Rect rect = new Rect(); View child; int childCount = getChildCount(); for (int i = getHeaderViewsCount(); i <= childCount; i++) { // Go through each child view (excluding headers) and see if our touch pressed it. child = getChildAt(i); if (child != null) { //Get the child hit rectangle. child.getHitRect(rect); //If the child would be hit by this press. if (rect.contains(touchX, touchY)) { // DIRECT HIT! You sunk my battleship. Now that we know which view was touched, store it off for use if a move occurs. View frontView = child.findViewById(mFrontViewId); View backView = child.findViewById(mBackViewId); // Create our view pair. mViewPair = new SwipeableViewPair(frontView, backView); break; } } } if (mViewPair != null) { // If we have a view pair, record details about the inital touch for use later. mDownX = event.getRawX(); mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(event); } break; case MotionEvent.ACTION_UP: if (mVelocityTracker != null) { // Add the movement so we can calculate velocity. mVelocityTracker.addMovement(event); mVelocityTracker.computeCurrentVelocity(1000); float deltaX = event.getRawX() - mDownX; float velocityX = Math.abs(mVelocityTracker.getXVelocity()); float velocityY = Math.abs(mVelocityTracker.getYVelocity()); if (mViewPair != null) { boolean shouldSwipe = false; // If the view has been moved a significant enough distance or if the view was flung, check to see if we should swipe it. if ((Math.abs(deltaX) > mViewWidth / 2 && mState == State.SWIPING) || (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity && velocityX > velocityY)) { if (mSwipeDirection == SWIPE_DIRECTION_BOTH) { // If the list is setup to swipe in either direction, just let it go. shouldSwipe = true; } else if (mSwipeDirection == SWIPE_DIRECTION_LEFT && deltaX < 0) { // If the list is only setup to swipe left, then only allow swiping to the left. shouldSwipe = true; } else if (mSwipeDirection == SWIPE_DIRECTION_RIGHT && deltaX > 0) { // If the list is only setup to swipe right, then only allow swiping to the right. shouldSwipe = true; } } if (shouldSwipe) { // If a swipe should occur meaning someone has let go of a view they were moving and it was far/fast enough for us to consider it a swipe start the animations. ViewPropertyAnimator.animate(mViewPair.mFrontView) .translationX(deltaX >= 0 ? mViewWidth : -mViewWidth).alpha(0) .setDuration(mAnimationTime); ViewPropertyAnimator.animate(mViewPair.mBackView).alpha(1).setDuration(mAnimationTime); // Now that the item is open, store it off so we can close it when we scroll if needed. mSwipedViews.put(mViewPair.hashCode(), mViewPair); // Clear out current variables as they are no longer needed and recycle the velocity tracker. resetState(); } else { // If the user let go of the view and we don't think the swipe was intended to occur (it was cancelled basically) reset the views. // Make sure the back disappears, since if it has buttons these can intercept touches from the front view. mViewPair.mBackView.setVisibility(View.GONE); ViewPropertyAnimator.animate(mViewPair.mFrontView).translationX(0).alpha(1) .setDuration(mAnimationTime); // Clear out current variables as they are no longer needed and recycle the velocity tracker. resetState(); } } } break; case MotionEvent.ACTION_MOVE: if (mVelocityTracker != null && mState != State.SCROLLING) { // If this is an initial movement and we aren't already swiping. // Add the movement so we can calculate velocity. mVelocityTracker.addMovement(event); mVelocityTracker.computeCurrentVelocity(1000); float deltaX = event.getRawX() - mDownX; float velocityX = Math.abs(mVelocityTracker.getXVelocity()); float velocityY = Math.abs(mVelocityTracker.getYVelocity()); // If the movement has been more than what is considered slop, and they are clearing moving horizontal not vertical. if (Math.abs(deltaX) > mTouchSlop && velocityX > velocityY) { boolean initiateSwiping = false; if (mSwipeDirection == SWIPE_DIRECTION_BOTH) { // If the list is setup to swipe in either direction, just let it go. initiateSwiping = true; } else if (mSwipeDirection == SWIPE_DIRECTION_LEFT && deltaX < 0) { // If the list is only setup to swipe left, then only allow swiping to the left. initiateSwiping = true; } else if (mSwipeDirection == SWIPE_DIRECTION_RIGHT && deltaX > 0) { // If the list is only setup to swipe right, then only allow swiping to the right. initiateSwiping = true; } if (initiateSwiping) { ViewParent parent = getParent(); if (parent != null) { // Don't allow parent to intercept touch (prevents NavigationDrawers from intercepting when near the bezel). parent.requestDisallowInterceptTouchEvent(true); } // Change our state to swiping to start tranforming the item. changeState(State.SWIPING); // Make sure that touches aren't intercepted. requestDisallowInterceptTouchEvent(true); // Cancel ListView's touch to prevent it from being focused. MotionEvent cancelEvent = MotionEvent.obtain(event); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); super.onTouchEvent(cancelEvent); } else { // Otherwise we need to cancel the touch event to prevent accidentally selecting the item and also preventing the swipe in the wrong direction or an incomplete touch from moving the view. MotionEvent cancelEvent = MotionEvent.obtain(event); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); super.onTouchEvent(cancelEvent); } } if (mState == State.SWIPING && mViewPair != null) { // Make sure the back is visible. mViewPair.mBackView.setVisibility(View.VISIBLE); //Fade the back in and front out as they move. ViewHelper.setAlpha(mViewPair.mBackView, Math.min(1f, 2f * Math.abs(deltaX) / mViewWidth)); ViewHelper.setTranslationX(mViewPair.mFrontView, deltaX); ViewHelper.setAlpha(mViewPair.mFrontView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); return true; } } break; } return super.onTouchEvent(event); }