How to use Android viewdraghelper
Help us realize various types of complex gesture operations. In its example, the viewdraghelper is created through a static factory, which is generally used inside a custom ViewGroup.
Initialization operation
mDrragHelper=ViewDragHelper.create(this,mCallback); The first operation represents the object of the current operation, the second parameter is the gesture operation sensitivity, and the third parameter is the interface for our gesture processing
We need to hand over the gesture operation to the viewfraghelper when distributing the view event
The focus is on these two sentences
We can decide when to give the event to our gesture operation class and then the callback class in onintercepttuchevent
The return value of trycaptureview indicates that the default return value of the child clapviewpositionhorizontal() method we allow to operate is 0. The return value represents the horizontal moving distance, that is, the left value. When the left value is returned, the view we operate will move with our dragging. Of course, there are methods in the numerical direction, If necessary, you can also override the method of vertical operation. Onviewpositionchanged() method calls back when we move. The left parameter here is the left returned by horizontal movement, DX is the relative change of horizontal distance. Onviewrelased() method calls back when the finger is raised (released). When the horizontal speed is slow, xvel is 0 per second, In pixels, yvel is the vertical velocity per second. There are positive and negative speeds, sliding edge:
Divided into sliding left edge or right edge: edge_ Left and edge_ Right, the following code sets the left edge that can handle sliding:
mDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT); If set as above, the onedgetouched method will be called when the left edge slides. In this case, there is generally no contact with the child view.
If you want to move a child view according to the sliding distance when the edge slides, you can implement the onedgedragstarted method and manually specify the child view to be moved in the onedgedragstarted method
slide
The finger can slide on the lower edge of the current view
Let's look at a method I called in ontochevent
The method above viewcompare.postinvalideonanimation (slideviewgroup. This) is to refresh the layout (redraw operation), and then call back the computerscroll of the secondary ViewGroup
In fact, the sliding itself is still the scrollto called to move according to the time percentage. After moving a fixed distance according to the proportion, it will not move. Therefore, we need to refresh repeatedly. We need to judge the critical condition, which may be time or distance. You can click the continuesetting method to return false, which means that the animation is completed. We can see it once we go in. We need to judge whether the sliding event is completed, If it is completed, it will not be refreshed. If it is not completed, it will be refreshed.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.