Android realizes NetEase news client side-slip menu (ii)

Source: Internet
Author: User

The above has been said through the three-party open Source Library Slidemenu to achieve this effect, please refer to the Android implementation of NetEase news client side-slip menu (a)

Today, this feature is implemented by customizing view.

The code is as follows:

Slidemenu.java

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >package Com.jackie.slidemenu.view;import Android.content.context;import Android.graphics.canvas;import Android.util.attributeset;import Android.view.motionevent;import Android.view.view;import Android.view.viewconfiguration;import Android.view.viewgroup;import Android.widget.scroller;public class SlideMenu Extends ViewGroup {private int mmostrecentx;//last x-axis offset private final int menu_screen = 0;//Menu Interface Private final int main_s Creen = 1;//Main interface private int mcurrentscreen = main_screen;//The current screen shows the main interface private Scroller mscroller;private int Touchslop;pu Blic Slidemenu (Context context, AttributeSet Attrs) {Super (context, attrs); mscroller = new Scroller (context); Touchslop = Viewconfiguration.get (context). Getscaledtouchslop ();} /** * Measures the width and height of all sub-layouts */@Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure ( Widthmeasurespec, Heightmeasurespec); Measureview (Widthmeasurespec, heightmeasurespec);} /** * Measure the width of all sub-layoutsand high * @param widthmeasurespec parent layout is viewgroup width measurement specification * @param heightmeasurespec parent layout is viewgroup height measurement specification */private void MEAs Ureview (int widthmeasurespec, int heightmeasurespec) {//Measurement menu width and height view Menuview = getchildat (0); Menuview.measure ( Menuview.getlayoutparams (). width, heightmeasurespec);//measurement of the main interface width and height view MainView = getchildat (1); Mainview.measure ( Widthmeasurespec, Heightmeasurespec);//the width and height of the main interface is the same as the height of the parent control ViewGroup} @Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {//Layout menu location View Menuview = Getchildat (0); Menuview.layout (-menuview.getmeasuredwidth (), 0, 0, b )///Layout The main interface location View MainView = Getchildat (1); mainview.layout (0, 0, R, b);} @Overridepublic boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case Motionevent.action_down: Mmostrecentx = (int) event.getx (); break;case motionevent.action_move://latest x-axis offset int moveX = (int) event.getx ();//increment value int DeltaX = mmostrecentx-movex;//Assigns the latest x-axis offset to the member variable Mmostrecentx = movex;//Gets the offset after the x-axis is shifted int newscrollx = GetscrollX () + deltax;if (Newscrollx <-getchildat (0). GetWidth ()) {//The offset of the current screen x-axis exceeds the left edge of the menu//back to the left edge of the menu Scrollto (-getchildat (0 ). GetWidth (), 0);} else if (Newscrollx > 0) {//exceeds the right boundary of the main interface//back to the right edge of the main interface Scrollto (0, 0);} else {Scrollby (deltax, 0);} Break;case MotionEvent.ACTION_UP:int scrollx = Getscrollx (); X-axis The latest offset int menuxcenter =-getchildat (0). GetWidth ()/2;//menu X-Axis Center point if (Scrollx > Menuxcenter) {//switch to main interface mcurrentscre En = Main_screen;} else {//Switch to menu interface mcurrentscreen = Menu_screen;} Switchscreen (); break;default:break;} return true;} /** * Toggle Screen according to mcurrentscreen */private void Switchscreen () {int scrollx = GETSCROLLX ();//offset of current x-axis int dx = 0;IF (mcurrentscr een = = main_screen) {//switch to main interface//scrollto (0, 0);d x = 0-SCROLLX;} else if (Mcurrentscreen = Menu_screen) {//Switch to menu interface//scro Llto (-getchildat (0). GetWidth (), 0);d x =-getchildat (0). GetWidth ()-scrollx;} Mscroller.startscroll (scrollx, 0, DX, 0, Math.Abs (DX) * 5); invalidate ();//Invalidate Drawchild, Child.draw-&G T Computescroll}/** * InvaLidate departure This method, update the offset of the x-axis of the screen */@Overridepublic void Computescroll () {if (Mscroller.computescrolloffset ()) {///determine if the data is being simulated, True false data simulation completed Scrollto (Mscroller.getcurrx (), 0); invalidate ();//Computescroll call}}/** * Show Menu * @return */ public Boolean Isshowmenu () {return mcurrentscreen = = Menu_screen;} /** * Hide menu */public void Hidemenu () {mcurrentscreen = Main_screen;switchscreen ();} /** * Show menu */public void ShowMenu () {mcurrentscreen = Menu_screen;switchscreen ();} /** * Method of intercepting Events */@Overridepublic Boolean onintercepttouchevent (motionevent ev) {switch (ev.getaction ()) {case Motionevent . Action_down:mmostrecentx = (int) ev.getx (); break;case MotionEvent.ACTION_MOVE:int diffx = (int) (EV.GETX ()- MMOSTRECENTX); if (Math.Abs (DIFFX) > Touchslop) {return true;} Break;default:break;} return super.onintercepttouchevent (EV);}} </span>

Mainactivity.java

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >package Com.jackie.slidemenu;import Com.jackie.slidemenu.view.slidemenu;import Android.os.Bundle;import Android.app.activity;import Android.view.menu;import Android.view.view;import Android.view.View.OnClickListener; Import Android.view.window;import Android.widget.textview;import Android.widget.toast;public class MainActivity Extends Activity implements Onclicklistener {private slidemenu mslidemenu; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate);//Remove title, need to call Requestwindowfeature before Setcontentview ( Window.feature_no_title); Setcontentview (r.layout.activity_main); mslidemenu = (Slidemenu) findViewById ( R.id.slidemenu); Findviewbyid (R.id.iv_slidemenu_main_back). Setonclicklistener (this);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic void OnCLick (View v) {if (Mslidemenu.isshowmenu ()) {Mslidemenu.hidemenu ();} else {mslidemenu.showmenu ();}} public void Click (View v) {TextView TV = (TextView) v; Toast.maketext (this, Tv.gettext (), 0). Show ();}} </span>



Android realizes NetEase news client side-slip menu (ii)

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.