QQ is inseparable from the chat tool, convenient both practical, since the QQ update to 6.0 after the slide from the original after the main panel narrowed into a smooth, in the appearance of a great upgrade, so I was trying to understand the various logic, combined with relevant information, research and research.
Know that this is a major class is Viewdraghelper, then first we have to understand the Viewdraghelper class, is the so-called dozen snakes dozen seven inches, we will first look at the official document how to introduce, what peculiar function.
First Inheritance:
Java.lang.Object
Android.support.v4.widget.ViewDragHelper
The direct parent class is object.
class overview
Viewdraghelper is a utility class for writing custom viewgroups. It offers a number of useful operations and state tracking for allowing a user to drag and reposition views within their p Arent ViewGroup.
He is a tool class that writes custom ViewGroup, and the province provides a number of useful methods and states that allow users to drag and draw their paths and positions in the parent viewgroup.
Nested Classes (nested Class)
Viewdraghelper.callback
A Callback is used as a communication channel with the Viewdraghelper back to the parent view using it.
A callback is the interface used to communicate with Viewdraghelper and his parent view
A public static method:
We can see that the Viewdraghelper is constructed by the Create () method. This will be described in detail later.
Let's take a look at some of the ways we need to use it:
public Boolean Trycaptureview (view child, int pointerid) {} public
int getviewhorizontaldragrange (view child) {} public
int Clampviewpositionhorizontal (view child, int left, int dx) {} public
void onviewpositionchanged (view C Hangedview, int left, int top, int dx, int dy) {}
A few of the above methods will have detailed comments in the code, here just look at the methods we need to rewrite
Ok ha, say so much, we first come to a simple, is can achieve drag and drop (believe that when your two view can drag and drop, you will find, oh so simple a few steps):
The first step to achieve drag-and-drop function (simple 3-Step implementation )
1, through the static method initialization Operation Mdraghelper = Viewdraghelper.create (this, mcallback); /** * 2, pass touch event * * @param EV * @return/@Override public boolean onintercepttouchevent (motionevent ev) {//Let your control decide whether to
Intercept return mdraghelper.shouldintercepttouchevent (EV); @Override public boolean ontouchevent (Motionevent event) {try {//own to handle touch event mdraghelper.processtouchevent (event);} Cat
CH (Exception e) {e.printstacktrace ();}//return True in order to receive continuously, otherwise we will not pass but be intercepted return true; } viewdraghelper.callback mcallback = new Viewdraghelper.callback () {/** * Determines whether the current child can be dragged or dropped by the return result, if it is returned to the main panel, that The negative version is not to be called * @param the child is currently dragged view * @param pointerid to distinguish the multi-touch ID * @return Return true is all can drag * return child = = Mleftcontent Left can Move * return child = = Mmaincontent Right can move/@Override public boolean Trycaptureview (View child, int pointerid) {//Here to return true, or
However, you cannot drag and drop return true; /** * Modify the position to be moved according to the proposed value no real movement occurs at this time (right) * * @param child's current dragged view * @param left new position of the proposed value OldLeft + dx * @param dx Variation and variation Difference of Previous position * @return * * * @Override Public int Clampviewpositionhorizontal (View child, int left, int dx) {//returned drag range, no real restrictions on drag, only determines the speed of animation execution return to left;};
Well, the first effect picture can come out, is can drag and drop, is not very simple to achieve it:
However, if you make such a submission task, is not to do not want to work ha, okay, let's take control of the drag and drop position, can not let him disorderly dragged ha. 、
Step two, control the drag range
We want to control the drag range, first we need to get these two controls, take the properties of the two controls, we can go to operate. So we rewrote the method:
/**
* Finalize inflating a view from XML. This is called as the last phase
* of inflation and after all child views have been added.
* When the XML is filled out, we can find the two layouts we are going to manipulate
* <p>even if the subclass overrides Onfinishinflate, they should always B E
* Sure to call "Super method", so this we get called.
* *
@Override
protected void Onfinishinflate () {
super.onfinishinflate ();
/**
* According to the index to find * *
/**
* To get the left layout * * *
mleftcontent = (viewgroup) getchildat (0);
/**
* Get main main layout * * *
mmaincontent = (viewgroup) getchildat (1);
}
Next we're going to get about the width and height, and we know Onmessure () can get it, but look at it, and the following method is also available:
/**
* When the size changes to call * This is
called during layout then the size of this view has changed
* @param w
* @para M h
* @param oldw
* @param oldh
/@Override
protected void onsizechanged (int w, int h, int oldw, int OLDH) {
super.onsizechanged (W, H, OLDW, OLDH);
/**
* Screen width and height *
*
mheight = Getmeasuredheight ();
Mwidth = Getmeasuredwidth ();
/**
* Customize left View drag out distance * *
mrange = (int) (MWIDTH * 0.7);
}
Well, about the width, the height, the drag distance we need, we've got it, so we're going to limit it next.
All we need to do is add this in the Clampviewpositionhorizontal () method to restrict the main panel from moving to the left, and the left panel to move only to the Mrange position on the right. (You can try it here.)
if (child = = mmaincontent) {left
= Fixleft (left)
;
} /**
* Correction Method
* According to the scope to modify the view of the left side of the visible
* *
@param A * *
@return
/private int Fixleft ( int left] {
if (left < 0) {return
0;
} else if (left > Mrange) {return
Mrange
}
return to left;
}
This time, our basic framework has come out, but there is still a problem, that is, although we have reached the sliding effect (the right side is fixed, but when we slide the Leftview out, we can still slide to the right) we have to find a way to limit, is to limit, View on the left I can only slide right mrange the distance this time we are going to look at the method, which method can get the value of the changed position, and then to change:
/**
* When the view position is changed, handle the things to be done, update the status, accompany the animation, redraw the interface
* *
Now the view has changed position * *
@param changedview Change position view
* @param left new left-hand value
* @param top
* @param dx level variation
* @param dy */
@Override public
void on Viewpositionchanged (View changedview, int left, int top, int dx, int dy) {
super.onviewpositionchanged (Changedview, Left, top, DX, dy);
int newleft = left;
If we drag the left panel if
(Changedview = = mleftcontent) {
//The New left position is the left side of our main panel plus the level variation
Newleft = Mmaincontent.getleft () + dx;
}
To make an amendment (not exceeding the scope of our provisions)
Newleft = Fixleft (newleft);
if (Changedview = = mleftcontent) {
//when the left panel is moved, put back in force
mleftcontent.layout (0, 0, 0 + mwidth, 0 + mheight);
Mmaincontent.layout (newleft, 0, Newleft + mwidth, 0 + mheight);
}
Compatible low version force redraw
invalidate ();
}
Well, this time our general effect has come out, look at the effect of the picture. (Here the middle comes out of the white to belong to the recording problem, the Pro test is no problem)
Here, the approximate drag and drop already can be achieved, of course, ah, my present layout is the following simple implementation (to add some control first, this oneself in two linearlayout add)
<?xml version= "1.0" encoding= "Utf-8"?> <com.example.qqsliding.draglayout xmlns:android= "http://" Schemas.android.com/apk/res/android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_ Parent "android:layout_height=" match_parent "tools:context=" com.example.qqsliding.MainActivity "> < LinearLayout android:layout_width= "match_parent" android:layout_height= match_parent "android:background=" @mipmap /SIDEBAR_BG "> </LinearLayout> <linearlayout android:layout_width=" Match_parent "android:layout_height= "Match_parent" android:background= "#FFFFFF" > <relativelayout android:layout_width= "match_parent" Android: layout_height= "50DP" android:background= "#0CB8F6" android:gravity= "center_vertical" > <imageview android: Layout_width= "30DP" android:layout_height= "30DP" android:layout_marginleft= "15DP" android:src= "@mipmap/icon_ Avatar_white "/> <textview android:layout_width= wrap_content" android:layout_height= "Wrap_content" Android: Layout_cenTerhorizontal= "true" android:text= "Header"/> <imageview android:layout_width= "30DP" android:layout_height= " 30DP "android:layout_alignparentright=" true "android:layout_marginright=" 15DP "android:src=" @mipmap/icon_search " > </RelativeLayout> </LinearLayout> </com.example.qqsliding.DragLayout>
But careful may find, we drag and drop when, especially blunt, that is because we did not add animation effect, just to drag and drop out, the concrete add animation, define callback effect, Learn about the Android sliding optimization QQ6.0 sideslip menu (ii), I hope this article will help you to share.