Pull down the ViewPager of a pig pod and view pager of a pig pod
There is a ViewPager layout in the application details page of pods. A view that can be expanded up or down is added to the layout header to display the application data. Basically, the idea is to use ViewDragHelper to drag to control the layout of the view in the header. When the TopView is visible, the gesture event is intercepted by the drag layer and controlled to fold and expand the TopView. When the TopView is invisible, the gesture event is controlled by ViewPager to achieve normal ListView scrolling. In the onScroll listener of the ListView, the system checks whether the ListView is reached. If you touch the top of the page, drop down and drag ViewPager to display the TopView.
The project has been uploaded to DragTopLayout of Github.
Let's take a look at it first:
Implementation Process
1. inherit a FrameLayout and control the sub-View dragging in FrameLayout. A ViewDragHelper object is instantiated in FrameLayout initialization.
ViewDragHelper.create(this, 1.0f, callback);
2. The callback parameter is the Callback of all events during the drag process, implementing a ViewDragHelper. callback ().
The method used in Callback is as follows:
// Determine whether the child is the dragging target tryCaptureView (View child, int pointerId) // process the dragging position. You can process the highest or lowest position clampViewPositionVertical (View child, int top, int dy) // change the onViewPositionChanged position of the view After dragging (View view, int left, int top, int dx, int dy) // handle onViewReleased (View releasedChild, float xvel, float yvel) after a drag gesture is released // onViewDragStateChanged (int state)
3. overwrite the onInterceptTouchEvent and onTouchEvent methods to intercept the event.
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { return shouldIntercept && dragHelper.shouldInterceptTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent event) { dragHelper.processTouchEvent(event); return true; }
4. overwrite the computeScroll method to implement the scroll effect after dragging
@Override public void computeScroll() { if (dragHelper.continueSettling(true)) { ViewCompat.postInvalidateOnAnimation(this); } }
5. Simple drag is almost implemented. The custom logic of the drag event needs to be processed in Callback.
Use tryCaptureView to determine whether the view of the current touch is the target to drag the view. If the return value is true, the drag operation is performed. If the return value is false, the drag operation is performed.
return child == dragContentView;
In the clampViewPositionVertical method, the maximum height of the drag cannot exceed the upper boundary.
@Override public int clampViewPositionVertical(View child, int top, int dy) { return Math.min(topViewHeight, Math.max(top, getPaddingTop())); }
In the onViewPositionChanged method, control the processing of the new position after dragging. The TopView needs to be processed accordingly during the drag process. Therefore, the top position of the drag is recorded in the method and the latest position is processed in the onLayout callback method.
@Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { super.onViewPositionChanged(changedView, left, top, dx, dy); contentTop = top; requestLayout(); }
After a gesture is released, the settleCapturedViewAt method is used to determine the final rolling position of the gesture. If the yvel parameter is greater than 0, the system slides down quickly. Otherwise, the system slides up quickly.
@Override public void onViewReleased(View releasedChild, float xvel, float yvel) { super.onViewReleased(releasedChild, xvel, yvel); // yvel > 0 Fling down || yvel < 0 Fling up int top; if (yvel > 0 || contentTop > topViewHeight) { top = topViewHeight + getPaddingTop(); } else { top = getPaddingTop(); } if (wizard.enableSliding) { dragHelper.settleCapturedViewAt(releasedChild.getLeft(), top); } postInvalidate(); }
In this way, all the drag events are processed. For specific code, refer to the Github project.
Contact info:
- Chenupt-chenupt@outlook.com
- Weibo: chenupt
- QQ: 753785666