Android self-defined component family "3"--define viewgroup to achieve side slip

Source: Internet
Author: User

ViewGroup about their own definition of the article has been very much, why I write this article, for people who have just started to learn or to define their own components of the unfamiliar friend although able to take doctrine use, but to step-by-step realization and understanding of the process and the principle of talent really out of someone else's code, Extrapolate but not easy, very dogbo the Lord actually does not want to write step, this is very time-consuming, but the assumption can be helpful to the reader, can learn from this article to define their own components to achieve my purpose.

The first step: build a framework to achieve a 3/5 and 2/5 split screen interface, such as the following:


The outermost layer is a self-defined viewgroup layout file such as the following:

Package Com.example.testscrollto;import Android.content.context;import Android.util.attributeset;import Android.view.view;import Android.view.viewgroup;import Android.view.view.measurespec;public class MyScrollView extends viewgroup{private int mwidth;private int mheight;private float mmenuweight = 3.0F/5;//menu interface ratio private View MMENUV   Iew;  Menu interface Private View Mpriview; Content Interface Public Myscrollview (context context, AttributeSet Attrs) {Super (context, attrs);} @Overrideprotected void OnLayout (boolean arg0, int arg1, int arg2, int arg3, int arg4) {System.out.println ("Run OnLayout"); Mmenuview.layout (0, 0, (int) (Mwidth * mmenuweight), mheight); mpriview.layout ((int) (Mwidth * mmenuweight), 0, Mwidth, Mheight);} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {System.out.println ("Run Onmeasure"); Super.onmeasure (Widthmeasurespec, HEIGHTMEASURESPEC);/* * Onmeasure The two parameters passed in is the size passed in by the previous control, and in many cases, overriding the method requires the actual size of the calculated control, * Then call setmeasureddimension (int, int) to set the actual size. * Onmeasure Incoming WidtHmeasurespec and Heightmeasurespec are not general dimensional values, but are values that combine patterns and dimensions. * We need to get the pattern through int mode = Measurespec.getmode (WIDTHMEASURESPEC), * size with int size = Measurespec.getsize (Widthmeasurespec). * Mode together has three cases, the values are measurespec.unspecified, measurespec.exactly, Measurespec.at_most. * measurespec.exactly is the exact size when we specify the control's Layout_width or Layout_height as a detailed value, such as andorid:layout_width= "50dip", or Fill_ The parent is a case where the size of the control has been determined, all of which are exact dimensions. * Measurespec.at_most is the maximum size, when the control's layout_width or layout_height is specified as wrap_content, the size of the control generally varies with the control's subspace or content. The size of the control can only be larger than the maximum size that the parent control consents to. Therefore, mode at this point is at_most,size gives the maximum size that the parent control agrees to. * Measurespec.unspecified is not a specified size, so the situation is not much, is generally the parent control is adapterview, through the measure method passed in mode.  */mwidth = Measurespec.getsize (Widthmeasurespec); Gets the width of the Myscrollview mheight = Measurespec.getsize (Heightmeasurespec); Gets the height of the Myscrollview}/** 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);}}
Step two: Set the interface position as needed

@Overrideprotected void OnLayout (boolean arg0, int arg1, int arg2, int arg3, int arg4) {mmenuview.layout (-(int)) (Mwidth * m Menuweight), 0, 0, mheight); mpriview.layout (0, 0, mwidth, mheight);}

Step three: Achieve left and right swipe

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.Scroller;  public class Myscrollview extends viewgroup{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 Boolean misshowmenu;private Scroller mscroller;public Myscrollview (context context, AttributeSet Attrs) { Super (context, attrs); mcontext = Context;mscroller = new Scroller (mcontext);} @Overrideprotected void OnLayout (boolean arg0, int arg1, int arg2, int arg3, int arg4) {mmenuview.layout (-(int)) (Mwidth * m Menuweight), 0, 0, mheight); mpriview.layout (0, 0, mwidth, mheight);} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec,  HEIGHTMEASURESPEC); mwidth = Measurespec.getsize (Widthmeasurespec); Get MysCrollview width mheight = measurespec.getsize (Heightmeasurespec); Gets the height of the Myscrollview}/** 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);} 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); Sliding Distance if (math.abs (dis) > (mwidth * mmenuweight/2)) {if (dis > 0) {//assuming >0 is sliding right showmenu ();} else{//Assume that <0 is sliding to the left Hidemenu ();}} Break;default:break;} return true;} @Overridepublic void Computescroll () {super.computescroll (); if (Mscroller.computescrolloffset ()) {ScrollTo ( Mscroller.getcurrx (), Mscroller.getcurry ());p ostinvalidate ();}} public Boolean Isshowmenu () {return misshowmenu;} public void ShowMenu () {if(Misshowmenu) {return;}  Misshowmenu = true;  The tag menu already shows int dx = (int) (mwidth * mmenuweight); Distance Mscroller.startscroll (GETSCROLLX (), 0,-DX, 0, 500) sliding to the target location; Invalidate ();} public void Hidemenu () {if (!misshowmenu) {return;} Misshowmenu = False;int dx = (int) (mwidth * mmenuweight); Mscroller.startscroll (Getscrollx (), 0, DX, 0,); invalidate (); } }
You can see the following two lines of code triggering the Computescroll () method from the above code

Mscroller.startscroll (GETSCROLLX (), 0,-DX, 0, a); invalidate ();
Fourth step: Join the form State Switch listener interface
public interface onmenuchangedlistener{public void onChanged (Boolean isshow); public void Setonmenuchangedlistener (Onmenuchangedlistener listener) {Mlistener = listener;}
Change the ShowMenu () method and the Hidemenu () method such as the following:

public void ShowMenu () {if (Misshowmenu) {return;} Misshowmenu = true; int dx = (int) (mwidth * mmenuweight); Mscroller.sta Rtscroll (GETSCROLLX (), 0,-DX, 0, 500); if (Mlistener! = null) {mlistener.onchanged (misshowmenu);} invalidate ();} public void Hidemenu () {if (!misshowmenu) {return;} Misshowmenu = False;int dx = (int) (mwidth * mmenuweight); Mscroller.startscroll (Getscrollx (), 0, DX, 0, 500); if (Mlistener! = null) {mlistener.onchanged (misshowmenu);} invalidate ();}
adding monitoring to the mainactivity

Mscrollview.setonmenuchangedlistener (New Onmenuchangedlistener () {@Overridepublic void onChanged (Boolean isshow) { System.out.println ("form switched Once");});
Fifth Step: Implement the Menu list and interface (directly post code) according to the detailed business and requirements

Mainactivity.java

Package Com.example.testrefreshview;import Android.app.activity;import Android.graphics.color;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.arrayadapter;import Android.widget.button;import Android.widget.listview;import com.example.testrefreshview.rightscrollview.onmenuchangedlistener;/** * Test Viewgroup,rigthscrollview with right-sliding menu function *@ Author LQH */public class Mainactivity extends Activity {private Rightscrollview mrightscrollview;private Button mshowmen Ubtn;private ListView mmenulist;private arrayadapter<string> madapter;private string[] menus = {"People near", "My Profile", " Settings "," Games "," Instant Chat "}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.rightscrollview_test); Mrightscrollview = (Rightscrollview) Findviewbyid ( R.id.rightscrollview); final View menu = Getlayoutinflater (). inflATE (R.layout.rightscrollview_menu, null), final View primary = Getlayoutinflater (). Inflate (R.layout.rightscrollview_ primary, null); mmenulist = (ListView) Menu.findviewbyid (r.id.list_right_menu); mshowmenubtn = (Button) Primary.findviewbyid (r.id.btn_showmenu); madapter = new Arrayadapter<string> (this, Android. r.layout.simple_list_item_1, menus); Mmenulist.setadapter (Madapter); Mshowmenubtn.setonclicklistener (new Onclicklistener () {public void OnClick (View v) {if (Mrightscrollview.isshowmenu ()) {Mrightscrollview.hidemenu ();} Else{mrightscrollview.showmenu ();}}); Mrightscrollview.setonmenuchangedlistener (New Onmenuchangedlistener () {public void onChanged (Boolean isshow) {if ( Isshow) {mshowmenubtn.settext ("Hide Menu");} Else{mshowmenubtn.settext ("Show Menu");}}); Mrightscrollview.setmenu (menu); mrightscrollview.setprimary (primary); Mmenulist.setonitemclicklistener (new Onitemclicklistener () {public void Onitemclick (adapterview<?> parent, view view, int position, long ID) {switch (pos ition) {Case 0:primaRy.setbackgroundcolor (Color.cyan); Break;case 1:primary.setbackgroundcolor (Color.Blue); Break;case 2: Primary.setbackgroundcolor (Color.gray); Break;case 3:primary.setbackgroundcolor (Color.magenta); Break;case 4: Primary.setbackgroundcolor (color.yellow); break;} Mrightscrollview.hidemenu ();}});}}
Rightscrollview.java

Package Com.example.testrefreshview;import Android.content.context;import Android.util.attributeset;import Android.view.motionevent;import Android.view.view;import Android.view.viewgroup;import android.widget.Scroller;/* * * ViewGroup with right-sliding menu, similar to Facebook's main interface * @author LQH */public class Rightscrollview extends ViewGroup {private Context Mcont Ext;private Scroller mscroller;private View mmenuview;private view mpriview;private int mwidth;private int mHeight; Private Boolean misshowmenu;private float mmenuweight = 3.0f/5;private Onmenuchangedlistener mlistener;public RightScro Llview (Context context) {This (context, null);} Public Rightscrollview (context context, AttributeSet Attrs) {Super (context, attrs); mcontext = Context;mscroller = new SCR Oller (Mcontext);} /** 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 Boolean Isshowmenu () {return misshowmenu;} public void SEtonmenuchangedlistener (Onmenuchangedlistener listener) {Mlistener = listener;} public void ShowMenu () {if (Misshowmenu) {return;} Misshowmenu = true; int dx = (int) (mwidth * mmenuweight); Mscroller.sta Rtscroll (GETSCROLLX (), 0,-DX, 0, 500); if (Mlistener! = null) {mlistener.onchanged (misshowmenu);} invalidate ();} public void Hidemenu () {if (!misshowmenu) {return;} Misshowmenu = False;int dx = (int) (mwidth * mmenuweight); Mscroller.startscroll (Getscrollx (), 0, DX, 0, 500); if (Mlistener! = null) {mlistener.onchanged (misshowmenu);} invalidate ();} 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) if (Math.Abs (dis) > (mwidth * mmenuweight/2)) {if (dis > 0) {showmenu ();} Else{hidemenu ();}} break;} return true;} @Overridepublic void Computescroll () {super.computescroll (); if (Mscroller.computescrolloffset ()) {ScrollTo (mscrollEr.getcurrx (), Mscroller.getcurry ());p ostinvalidate ();}} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {mwidth = Measurespec.getsize ( WIDTHMEASURESPEC); mheight = Measurespec.getsize (Heightmeasurespec); Setmeasureddimension (MWidth, mHeight); int Widthspec = Measurespec.makemeasurespec ((int) (Mwidth * mmenuweight), measurespec.exactly); int heightspec = Measurespec.makemeasurespec (Mheight, measurespec.exactly); Mmenuview.measure (Widthspec, heightSpec); WidthSpec = Measurespec.makemeasurespec (Mwidth, measurespec.exactly); Mpriview.measure (Widthspec, heightSpec);} @Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {mmenuview.layout (-(int) (Mwidth * Mmenuweigh T), 0, 0, mheight); mpriview.layout (0, 0, mwidth, mheight);} public interface onmenuchangedlistener{public void onChanged (Boolean isshow);}}
Operating Effect:



SOURCE Download: http://download.csdn.net/detail/lxq_xsyu/7231677

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.