In fact, there are many ways to achieve a layout drawer stretching, the most common way is to define a viewgroup, and then control the Click event. Control mobile and so on, the code of this method is much, and the implementation of complex, late maintenance to add other effects is also very troublesome, until today to see the Viewdraghelper this class, is specifically for the realization of the mobile view of the birth. I just tried to develop a drawer stretching effect, such as the following:
The control of all movements is implemented in the Viewdraghelper.callback. Mobile is implemented with DRAGHELPER.SMOOTHSLIDEVIEWTO, and callback integrates a lot of methods. Convenient for later maintenance or adding other functions.
First look at the core of the Draglayout code
public class Draglayout extends LinearLayout {private Viewdraghelper draghelper; Private View Mdragview, Contentview; private int dragrange; Public Draglayout (Context context) {super (context); Init (); } public Draglayout (context context, AttributeSet Attrs) {Super (context, attrs); Init (); } @TargetApi (Build.version_codes. LOLLIPOP) Public draglayout (context context, AttributeSet attrs, int defstyleattr, int defstyleres) {SUPER (cont Ext, Attrs, defstyleattr, defstyleres); Init (); } public Draglayout (context context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr) ; Init (); } private void Init () {DragHelper = Viewdraghelper.create (this, callback); } @Override protected void Onfinishinflate () {super.onfinishinflate (); Mdragview = Findviewbyid (R.id.dragview); Contentview = Findviewbyid (R.id.contentview); } Private ViewdraghElper. Callback Callback = new Viewdraghelper.callback () {@Override public boolean trycaptureview (View child, int Pointerid) {return child = = Mdragview; } @Override public void onviewpositionchanged (View changedview, int left, int top, int dx, int dy) { Contentview.layout (0, Top + mdragview.getheight (), getwidth (), Top + mdragview.getheight () + Dragrange); } @Override public int clampviewpositionvertical (View child, int top, int dy) {int topbound = ge Theight ()-dragrange-mdragview.getheight (); int bottombound = GetHeight ()-mdragview.getheight (); Final int newheight = Math.min (Math.max (Topbound, top), bottombound); return newheight; } @Override public int getviewverticaldragrange (View child) {return dragrange; } @Override public void onviewreleased (View releasedchild, float xvel, float yvel) {suPer.onviewreleased (Releasedchild, Xvel, Yvel); if (Yvel > 0) {smoothtobottom (); }else if (Yvel < 0) {smoothtotop (); } } }; @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (widthmeasuresp EC, HEIGHTMEASURESPEC); Dragrange = Contentview.getmeasuredheight (); } @Override protected void OnLayout (Boolean changed, int l, int t, int r, int b) {super.onlayout (changed, L, T, R, b); Mdragview.layout (0, GetHeight ()-Mdragview.getheight (), getwidth (), getheight ()); Contentview.layout (0, GetHeight (), getwidth (), getheight () + Dragrange); } @Override public boolean onintercepthoverevent (Motionevent event) {Final int action = motioneventcompat.ge Tactionmasked (event); if (action = = Motionevent.action_cancel | | action = = motionevent.action_up) {draghelper.cancel (); return false; } return Draghelper.shouldintercepttouchevent (event); } @Override public boolean ontouchevent (Motionevent event) {draghelper.processtouchevent (event); return true; } private void Smoothtotop () {if (Draghelper.smoothslideviewto (Mdragview, Getpaddingleft (), GetHeight ()-DRAGR Ange-mdragview.getheight ())) {viewcompat.postinvalidateonanimation (this); }} private void Smoothtobottom () {if (Draghelper.smoothslideviewto (Mdragview, Getpaddingleft (), GetHeight () -Mdragview.getheight ())) {viewcompat.postinvalidateonanimation (this); }} @Override public void Computescroll () {if (draghelper.continuesettling (True)) {Viewcompat . postinvalidateonanimation (this); } }}
In this case, initialize the viewdraghelper.
= Viewdraghelper. Create (Thiscallback);
Set the vertical moving distance, set here to the height of the listview:
@Override
public int clampviewpositionvertical (View child, int top, int dy) {
int topbound = GetHeight ()-dragrange-mdragview.getheight ();
int bottombound = GetHeight ()-mdragview.getheight ();
Final int newheight = Math.min (Math.max (Topbound, top), bottombound);
return newheight;
}
Monitor the location of the move, move the ListView. Let him always be under the Drawview:
@Overridedy) {contentview. layout (0 mdragviewmdragviewdragrange);}
Another layout in OnLayout. Hide ListView:
@Override protected void OnLayout (Boolean changed, int l, int t, int r, int b) { super.onlayout (changed, L, T, R, b); Mdragview.layout (0, GetHeight ()-Mdragview.getheight (), getwidth (), getheight ()); Contentview.layout (0, GetHeight (), getwidth (), getheight () + Dragrange); }
Next is the layout of the XML:
<cn.xm.weidongjian.verticaldrawerlayout.draglayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:orientation=" vertical " android:layout_width=" match_parent " android:layout_height= "Match_parent" > <view android:id= "@+id/dragview" android:layout_width= "Match_parent " android:layout_height= "80DP" android:background= "@android: Color/holo_blue_light"/> <listview android:scrollbars= "None" android:id= "@+id/contentview" android:layout_width= "Match_parent" android:layout_height= "200DP" android:background= "@android: Color/holo_green_light"/></ Cn.xm.weidongjian.verticaldrawerlayout.draglayout>
Very easy. Two view inside the view you define
The final mainactivity:
public class Mainactivity extends Appcompatactivity { string[] ListItems = {"Item 1", "Item 2", "List", "Android", "I TEM 3 "," Foobar "," Bar ",}; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); ListView ListView = (ListView) Findviewbyid (R.id.contentview); Listview.setadapter (New Arrayadapter (this, Android. R.layout.simple_list_item_1, ListItems));} }
Finally, this demo is just to achieve a very easy function, but probably can see the power of Viewdraghelper. It is highly recommended to understand that this is the address of two APIs
Viewdraghelper
Callback
Finally, attach the source code link
Android Development: Use Viewdraghelper to achieve a drawer stretching effect