In the previous article "Android custom components series [3] -- custom ViewGroup for slide", we achieved a side slide effect similar to Facebook and Renren, this article will be followed by 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); // get the width of MyScrollView mHeight = MeasureSpec. getSize (heightMeasureSpec); // obtain the height of MyScrollView if (! IsLocked) {initX = getScrollX (); isLocked = true ;}}
Get the initial view coordinate offset getScrollX () in this method ()
@ 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 break at press time; case MotionEvent. ACTION_UP: System. out. println ("ACTION_UP"); int dis = (int) (x-mDownX); // The moving distance from if (Math. abs (dis)> (mWidth * mMenuWeight/2) {if (dis> 0) {// if> 0, slide to the right toRightMove ();} else {// If <0, it is sliding toLeftMove () ;}} break; default: break;} return true;} to the left ;}
The monitoring function records the screen coordinates of pressing and moving, and calculates the moving distance. If the distance is greater than the threshold value (mWidth * mMenuWeight/2), it slides.
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, 500); if(mListener != null){ mListener.onChanged(); } invalidate(); }}
If it is sliding to the right, if it is the initial position (the centerView is in the middle), you can slide to the right (getScrollX = initX), or the current Left View can be seen, you can move centerView to the center (getScrollX> initX) by sliding to the right ). similarly, there is a sliding method to the 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, 500);if(mListener != null){mListener.onChanged();}invalidate();}}
3. All code
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 proportion private View mMenuView; // menu interface private View mPriView; // content interface private View mRightView; // private int maxLeft; private int maxRight; private int initX on the right; private boolean isLocked = false; private Scroller mScroller; private OnMenuChangedListener mListener; public MyScrollView (Context context, AttributeSet attrs) {super (context, attrs); mContext = context; mscroroller = new scroroller (mContext);} @ Overridep Rotected void onLayout (boolean changed, int l, int t, int r, int B) {super. onLayout (changed, l, t, r, B); mMenuView. 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, hei GhtMeasureSpec); mWidth = MeasureSpec. getSize (widthMeasureSpec); // obtain the width of MyScrollView mHeight = MeasureSpec. getSize (heightMeasureSpec); // obtain the height of MyScrollView if (! IsLocked) {initX = getScrollX (); isLocked = true ;}}/** set the right-sliding menu View */public void setMenu (View menu) {mMenuView = menu; addView (mMenuView);}/*** set the 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 = ev Ent. getX (); switch (event. getAction () {case MotionEvent. ACTION_DOWN: System. out. println ("ACTION_DOWN"); mDownX = x; // record the x coordinate break at press time; case MotionEvent. ACTION_UP: System. out. println ("ACTION_UP"); int dis = (int) (x-mDownX); // The moving distance from if (Math. abs (dis)> (mWidth * mMenuWeight/2) {if (dis> 0) {// if> 0, slide to the right toRightMove ();} else {// If <0, it is sliding toLeftMove () ;}} break; default: break;} return true ;}@ Overridepublic voi D computeScroll () {super. computeScroll (); if (mScroller. computescroloffset () {scrollTo (mScroller. getCurrX (), mScroller. getCurrY (); postInvalidate () ;}} 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,500); if (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. startScroll (getScrollX (), 0, dx, 0,500); if (mListener! = Null) {mListener. onChanged () ;}invalidate () ;}} public interface OnMenuChangedListener {public void onChanged () ;} public void setOnMenuChangedListener (OnMenuChangedListener) {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 ("window 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:orientation="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 unrestricted and can be freely defined. This will not be posted here.
4. Running effect:
Source code download: http://download.csdn.net/detail/lxq_xsyu/7232701