In the previous article, "Android's own definition of component family" 3 "-self-defined viewgroup realization of the side slip" to achieve the imitation of Facebook and Renren's side-slip effect, this one we will continue the previous article to achieve the effect of double-sided sliding.
1. Layout:
2. Core code
@Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec, HEIGHTMEASURESPEC); mwidth = Measurespec.getsize (widthmeasurespec); Gets the width of the Myscrollview mheight = Measurespec.getsize (Heightmeasurespec); Gets the height of the Myscrollview if (!islocked) {initx = Getscrollx (); isLocked = True;}}
Gets the initial view coordinate offset in the method Getscrollx ()
@Overridepublic boolean ontouchevent (Motionevent event) {float x = event.getx (); switch (Event.getaction ()) {case MotionEvent.ACTION_DOWN:System.out.println ("Action_down"); mdownx = x; Record the x-coordinate when pressed break;case MotionEvent.ACTION_UP:System.out.println ("action_up"); int dis = (int) (X-MDOWNX); Sliding Distance if (math.abs (dis) > (mwidth * mmenuweight/2)) {if (dis > 0) { //assuming >0 is sliding right torightmove ();} else{ //Assume that <0 is sliding to the left toleftmove ();}} Break;default:break;} return true;}
the Listener function records the screen coordinates that are pressed down and moved. The difference is calculated by moving the distance, assuming that the distance is greater than the threshold (Mwidth * mmenuweight/2) sliding
public void Torightmove () {System.out.println ("maxright =" + maxright); System.out.println ("X =" + GETSCROLLX ()); if (GETSCROLLX () >= initx) {int dx = (int) (mwidth * mmenuweight); Mscroller.startscroll (Getscrollx (), 0,-DX, 0,); F (Mlistener! = null) {mlistener.onchanged ();} invalidate (); }}
Let's say you swipe right. If it is currently the initial position (Centerview in the middle) and can swipe to the right (GETSCROLLX = = INITX), or the current left view can be seen, you can swipe right to move the Centerview to the middle (Getscrollx > Initx) There is a way to swipe left.
public void Toleftmove () {System.out.println ("Maxleft =" + Maxleft); System.out.println ("X =" + GETSCROLLX ()), if (Getscrollx () <= initx) {int dx = (int) (mwidth * mmenuweight); Mscroller.startscroll (GETSCROLLX (), 0, DX, 0, $); if (mlistener! = null) {mlistener.onchanged ();} Invalidate ();}}
3. All codes
Myscrollview.java
Package Com.example.testscrollto;import Android.content.context;import Android.util.attributeset;import Android.view.motionevent;import Android.view.view;import Android.view.viewgroup;import Android.widget.linearlayout;import Android.widget.scroller;public class Myscrollview extends LinearLayout{private Context mcontext;private int mwidth;private int mheight;private float mmenuweight = 3.0F/5; Menu interface ratio private View mmenuview; Menu interface Private View Mpriview; Content interface private View Mrightview; Right interface private int maxleft;private int maxright;private int initx;private Boolean isLocked = False;private Scroller mscroll Er;private Onmenuchangedlistener mlistener;public Myscrollview (context context, AttributeSet Attrs) {Super (context, ATTRS); mcontext = Context;mscroller = new Scroller (mcontext);} @Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {super.onlayout (changed, L, T, R, b); Mmenuvi Ew.layout (-(int) (Mwidth * mmenuweight), 0, 0, mheight); mpriview.layout (0, 0, mwidth, mheight); Mrightview.layout (mwidth, 0, mwidth + (int) (Mwidth * mmenuweight), mheight);} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec, HEIGHTMEASURESPEC); mwidth = Measurespec.getsize (Widthmeasurespec); Gets the width of the Myscrollview mheight = Measurespec.getsize (Heightmeasurespec); Gets the height of the Myscrollview if (!islocked) {initx = Getscrollx (); isLocked = True;}} /** Set right-sliding menu view*/public void SetMenu (View menu) {Mmenuview = Menu;addview (Mmenuview);} /** * Set main interface View */public void setprimary (view primary) {Mpriview = Primary;addview (Mpriview);} public void Setrightview (View rightview) {Mrightview = Rightview;addview (Mrightview);} private float Mdownx; @Overridepublic boolean ontouchevent (Motionevent event) {float x = event.getx (); switch ( Event.getaction ()) {case MotionEvent.ACTION_DOWN:System.out.println ("Action_down"); mdownx = x; Record the x-coordinate when pressed break;case MotionEvent.ACTION_UP:System.out.println ("action_up"); int dis = (int) (X-MDOWNX); The distance of the slideFrom if (Math.Abs (dis) > (mwidth * mmenuweight/2)) {if (dis > 0) {//assuming >0 is sliding to the right torightmove ();} else{//Assume that <0 is sliding to the left toleftmove ();}} Break;default:break;} return true;} @Overridepublic void Computescroll () {super.computescroll (); if (Mscroller.computescrolloffset ()) {ScrollTo ( Mscroller.getcurrx (), Mscroller.getcurry ());p ostinvalidate ();}} public void Torightmove () {System.out.println ("maxright =" + maxright); System.out.println ("X =" + GETSCROLLX ()); if (GETSCROLLX () >= initx) {int dx = (int) (mwidth * mmenuweight); Mscroller.startscroll (Getscrollx (), 0,-DX, 0,); F (Mlistener! = null) {mlistener.onchanged ();} invalidate (); }}public void Toleftmove () {System.out.println ("Maxleft =" + Maxleft); System.out.println ("X =" + GETSCROLLX ()), if (Getscrollx () <= initx) {int dx = (int) (mwidth * mmenuweight); Mscroller.sta Rtscroll (GETSCROLLX (), 0, DX, 0, $); if (mlistener! = null) {mlistener.onchanged ();} Invalidate ();}} public interface onmenuchangedlistener{public void onChanged ();} public void Setonmenuchangedlistener (Onmenuchangedlistener listener) {Mlistener = Listener;}} mainactivity.java
Package Com.example.testscrollto;import Android.app.activity;import Android.os.bundle;import Android.view.View; Import Com.example.testscrollto.myscrollview.onmenuchangedlistener;public class Mainactivity extends Activity { Private Myscrollview Mscrollview; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); Mscrollview = (Myscrollview) findViewById ( R.id.rightscrollview); Final View menu = Getlayoutinflater (). Inflate (R.layout.rightscrollview_menu, NULL); Final view Primary = Getlayoutinflater (). Inflate (r.layout.rightscrollview_primary, null); final View Rightview = Getlayoutinflater (). Inflate (R.layout.rightscrollview_right_menu, null); Mscrollview.setmenu (menu); Mscrollview.setprimary (primary); Mscrollview.setrightview (Rightview); Mscrollview.setonmenuchangedlistener (new Onmenuchangedlistener () {@Overridepublic void onChanged () {System.out.println ("form switched Once");}});}}Activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <com.example.testscrollto.myscrollview android:id= "@+id/rightscrollview" android:layout_width= "match_parent" android:layout_height= "Match_parent"/></linearlayout>
The remaining three view interfaces are unlimited. Can be defined freely, it is not posted here.
4. Execution Effect:
SOURCE Download: http://download.csdn.net/detail/lxq_xsyu/7232701
This will enable the left and right sliding, no matter what the bug?
In fact, this seems to be no problem, the code above to infer the interface location is logically plausible, but in the actual application occasionally there will be confusion.
Here we solve in the second way:
Package Com.example.jaohangui.view;import Android.content.context;import Android.util.attributeset;import Android.view.motionevent;import Android.view.view;import Android.widget.linearlayout;import Android.widget.scroller;public class Myscrollleftrightview extends Linearlayout{private Scroller mscroller;private View Mleftview; Coordinate interface private View Mmainview; The middle main interface private View Mrightview; Right interface private float mmeasurewight = 3.0F/5; Menu interface scale private int mwidth;private int mheight;private Boolean isLocked = false;private Boolean istoleft = False;private s tatic int center_page = 1;private static int left_page = 0;private static int right_page = 2;private int currentpage = CEN Ter_page;public Myscrollleftrightview (Context context, AttributeSet Attrs) {Super (context, attrs); mscroller = new Scroller (context);} @Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {super.onlayout (changed, L, T, R, b); Mleftvi Ew.layout (-(int) (Mwidth * mmeasurewight), 0, 0, mheight); MmainView.layout (0, 0, mwidth, mheight); Mrightview.layout (mwidth, 0, mwidth + (int) (Mwidth * mmeasurewight), mheight);} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec, HEIGHTMEASURESPEC); mwidth = Measurespec.getsize (widthmeasurespec); mheight = Measurespec.getsize (HeightMeasureSpec) ;} /** * Add left interface content * @param view */public void Setleftview (view view) {Mleftview = View;addview (Mleftview);} /** * Add main interface content * @param view */public void Setmainview (view view) {Mmainview = View;addview (Mmainview);} /** * Add the right interface content * @param view */public void Setrightview (view view) {Mrightview = View;addview (Mrightview);} private float Mdownx; @Overridepublic boolean ontouchevent (Motionevent event) {float x = event.getx (); switch ( Event.getaction ()) {Case MotionEvent.ACTION_DOWN:mDownX = x;break;case MotionEvent.ACTION_UP:int dis = (int) (X-MDOWNX) ; Slide the distance if (math.abs (dis) > (mwidth * mmeasurewight/3)) {if (dis > 0) {torightmove ();} Else{toleftmove ();}} Break;d Efault:break;} return true;} @Overridepublic void Computescroll () {super.computescroll (); if (Mscroller.computescrolloffset ()) {isLocked = true; ScrollTo (Mscroller.getcurrx (), Mscroller.getcurry ());p ostinvalidate (); Else{if (currentpage = = center_page) {if (istoleft) {currentpage = Right_page;} Else{currentpage = Left_page;}} Else{currentpage = Center_page;} isLocked = false;}} public void Torightmove () {if (currentpage = = Left_page | | isLocked) {return;} int dx = (int) (mwidth * mmeasurewight); Mscroller.startscroll (Getscrollx (), 0,-DX, 0, N); invalidate (); istoleft = false; }public void Toleftmove () {if (currentpage = = Right_page | | isLocked) {return;} System.out.println ("OK"); int dx = (int) (mwidth * mmeasurewight); Mscroller.startscroll (Getscrollx (), 0, DX, 0, 500); Invalidate (); istoleft = True;}}
The above uses two variables to infer and a lock state (not allowed to enter Toleftmove and Torightmove).
1, when entering the Toleftmove or Torightmove method, the first inference is whether the islocked is true, assuming true indicates that the current is sliding state, can not execute both methods. Suppose not to control it. When the interface is sliding, the currentpage on the top of the screen will go awry.
2, will slide before telling Computescroll () method, is from the Toleftmove and Torightmove of that method (using Istoleft).
3. Finally infer whether the scroller has stopped sliding (moving), assuming that the stop will change the current page state (currentpage value)
Sometimes we realize a function or some logic, and how to make this code more robust and effective more worthy of our serious thinking.
。。
Android defines its own component family "4"--Define ViewGroup to achieve double-side sliding