Android: Netease news client slide menu (2), android Netease news
We have already discussed how to achieve this through the third-party open-source library SlideMenu. For details, referAndroid: Netease news Client Side slidesSingle (1)
This feature is implemented through custom View today
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; // offset of the last X axis private final int MENU_SCREEN = 0; // menu interface private final int MAIN_SCREEN = 1; // main interface private int mCurrentScreen = MAIN_SCREEN; // The current screen displays the main interface private Scroller mScroller; private int touchSlop; public SlideMenu (Context context, AttributeSet attrs) {super (context, attrs); mScroller = new Scroller (context); touchSlop = ViewConfiguration. get (context ). getScaledTouchSlop ();}/*** measure the width and height of all sub-la S */@ Overrideprotected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasureSpec, heightMeasureSpec); measureView (widthMeasureSpec, heightMeasureSpec );} /*** measure the width and height of all sub-la ** @ param widthMeasureSpec parent layout, that is, the width measurement specification of ViewGroup * @ param heightMeasureSpec parent layout, that is, the height measurement specification of ViewGroup */private void measureView (int widthMeasureSpec, int heightMeasureSpec) {// width and height of the measurement menu menuView = getChildAt (0); menuView. measure (menuView. getLayoutParams (). width, heightMeasureSpec); // measure the width and height of the main interface mainView = getChildAt (1); mainView. measure (widthMeasureSpec, heightMeasureSpec); // the width and height of the main interface are the same as those of the viewgroup of the parent control.} @ Overrideprotected void onLayout (boolean changed, int l, int t, int r, int B) {// position of the layout menu View menuView = getChildAt (0); menuView. layout (-menuView. getMeasuredWidth (), 0, 0, B); // you can specify the position of the main interface. 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: // The latest X axis offset int moveX = (int) event. getX (); // increment value int deltaX = mMostRecentX-moveX; // assign the latest X axis offset to the member variable mMostRecentX = moveX; // get the offset int newScrollX = getScrollX () + deltaX; if (newScrollX <-getChildAt (0) after moving the X axis ). getWidth () {// the offset of the x-axis of the current screen exceeds the left boundary of the menu // return to the left boundary position of the menu scrollTo (-getChildAt (0 ). getWidth (), 0);} else if (newScrollX> 0) {// The right border of the main interface is exceeded // return to the right border of the main interface scrollTo (0, 0 );} else {scrollBy (deltaX, 0);} break; case MotionEvent. ACTION_UP: int scrollX = getScrollX (); // The latest offset of the X axis int menuXCenter =-getChildAt (0 ). getWidth ()/2; // if (scrollX> menuXCenter) of the menu X axis {// switch to mCurrentScreen = MAIN_SCREEN;} else {// switch to mCurrentScreen = MENU_SCREEN ;} switchScreen (); break; default: break;} return true;}/*** switch the Screen Based on mCurrentScreen */private void switchScreen () {int scrollX = getScrollX (); // offset of the current X axis int dx = 0; if (mCurrentScreen = MAIN_SCREEN) {// switch to the main interface // scrollTo (0, 0); dx = 0-scrollX ;} else if (mCurrentScreen = MENU_SCREEN) {// switch to the menu interface // scrollTo (-getChildAt (0 ). getWidth (), 0); dx =-getChildAt (0 ). getWidth ()-scrollX;} mScroller. startScroll (scrollX, 0, dx, 0, Math. abs (dx) * 5); invalidate (); // invalidate-> drawChild-> child. draw-> computeScroll}/*** invalidate to update the offset of the X axis of the screen */@ Overridepublic void computeScroll () {if (mScroller. computescroloffset () {// determines whether the simulated data is in progress. true indicates that the simulated data is in progress. getCurrX (), 0); invalidate (); // call that caused computeScroll}/*** show menu * @ return */public boolean isShowMenu () {return mCurrentScreen = MENU_SCREEN;}/*** hide menu */public void hideMenu () {mCurrentScreen = MAIN_SCREEN; switchScreen ();} /*** display menu */public void showMenu () {mCurrentScreen = MENU_SCREEN; switchScreen ();} /*** method of intercepting the event */@ 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); // removes the title. You must call requestWindowFeature (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 present. 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>