HorizontalDragLayout-slides and deletes items on the QQ client.
First, thank you. ViewDragHelper exists in v4 packages to help us customize ViewGroup.
First:
Step 1: declare ViewDragHelper:
ViewDragHelper.create(this, 1.0f, new ViewDragCallback());
1.0f indicates that the smallest sliding distance touchSlop is the system's default multiple.
Step 2: Inherit the methods in ViewDragHelper. CallBack implementation to meet the sliding requirement.
private class ViewDragCallback extends ViewDragHelper.Callback { @Override public boolean tryCaptureView(View view, int i) { return view == mContentView || view == mDeleteView; } @Override public int clampViewPositionHorizontal(View child, int left, int dx) { Log.w(TAG, "clampViewPositionHorizontal = " + left + "/" + dx); int realLeft = left; if (child == mContentView) { if (left > 0) {// right scroll realLeft = left > mDeleteView.getWidth() ? mDeleteView.getWidth() : left; } else if (left < 0) {// left scroll realLeft = Math.abs(left) > mDeleteView.getWidth() ? -mDeleteView.getWidth() : left; } } return realLeft; } @Override public void onViewReleased(View releasedChild, float xvel, float yvel) { super.onViewReleased(releasedChild, xvel, yvel); Log.w(TAG, "--- scroll finish when finger up ---"); Log.w(TAG, "onViewReleased = " + xvel + "/" + yvel); if (releasedChild == mContentView) { if (xvel > 0) { mDragHlper.settleCapturedViewAt(point.x, point.y); } else { mDragHlper.settleCapturedViewAt(point.x - mDeleteView.getWidth(), point.y); } invalidate(); } } @Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { super.onViewPositionChanged(changedView, left, top, dx, dy); Log.w(TAG, "--- when view position changed = " + left + "/" + dx); if (changedView == mContentView) { mDeleteView.offsetLeftAndRight(dx); } else if (changedView == mDeleteView) { mContentView.offsetLeftAndRight(dx); } invalidate(); } }
TryCaptureView: This method slides only when the return value is true. Otherwise, this method has no effect.
OnViewReleased.
OnViewPositionChanged: When CaptureView slides, DeleteView is also set to move to the corresponding place.
ClampViewPositionHorizontal: sets the sliding area of the ContentView in this ViewGoup.
Step 3: rewrite finger control method:
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { int action = MotionEventCompat.getActionMasked(ev); if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { return false; } return mDragHlper.shouldInterceptTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent event) { mDragHlper.processTouchEvent(event); return true; }
ViewDragHelper has the following points:
ViewDragHelper. Callback is the bridge between ViewDragHelper and view (this view generally refers to the container with sub-view, that is, parentView );
ViewDragHelper instances are created using the static factory method;
You can specify the direction of the drag;
ViewDragHelper can detect whether the edge is reached;
ViewDragHelper does not directly act on the View to be dragged, but enables the Sub-View in the view container controlled by ViewDragHelper to be dragged. If you want to specify the behavior of a sub-View, you need to find a solution in Callback;
The essence of ViewDragHelper is to analyze the MotionEvent parameters of onInterceptTouchEvent and onTouchEvent, and then change the position of the dragged sub-View in a container based on the analysis results (by offsetTopAndBottom (int offset) and offsetLeftAndRight (int offset), which can be used to determine the Child View that is being dragged during touch;
Although ViewDragHelper's instance method ViewDragHelper create (ViewGroup forParent, Callback cb) can specify an object for ViewDragHelper to process drag events, however, the ViewDragHelper class design determines its applicability to be included in a custom ViewGroup, rather than using ViewDragHelper for view containers on any layout.
Source Code address:
Https://github.com/Neacy/HoriazontalDragView
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.