Android development practice ViewGroup implements sliding window between left and right (1)

Source: Internet
Author: User

Use the holidays to summarize the previous things. Let's start with a simple one.
The effect is as follows:

I made a screenshot animation. The ratio is a bit incorrect. Let's take a look.

The entire window consists of three parts. The main interface in the middle is a list, the slide interface on the left is a menu, and the slide interface on the right is an edit box. The left menu is half screen, and the right menu is full screen.
In the end, I only used the sliding out window on the left, but I still completed all the left and right algorithms.
In addition, a method is designed to allow the activity to specify the width of two sliding windows.

Source code: http://files.cnblogs.com/inkheart0124/PushSlider.zip

 

1. Custom PushSlider class inherits ViewGroup and implements gesture listener

PushSlider. java

  PushSlider  ViewGroup  OnGestureListener
    SLIDER_PAGE_LEFT = 0    SLIDER_PAGE_MIDDLE = 1    SLIDER_PAGE_RIGHT = 2;

The index of the three Child views. Among them, SLIDER_PAGE_MIDDLE is fixed width (full screen width), and SLIDER_PAGE_LEFT and SLIDER_PAGE_RIGHT can be used to set the width of the View.

 

           setHapticFeedbackEnabled(     mContext =     mForbidden =      mGDetector =  GestureDetector(mContext,     focusedIndex =     mMoveFlag = MOVE_FLAG_ALLOW_LEFT|     mDensity =     mScroller =  Scroller(mContext,      mChildWidth =  [3              }

Initialize data:

Lines 5, mForbidden = false; the initial state allows slide by default. The activity can disable slide through the mPushView. pushForbid (true) method.

Line 6: mGDetector = GestureDetector (mContext, create a gesture listener object.

Seven rows: view of the initial focus.

Nine rows to obtain the pixel density, which is used later to calculate the location.

Row 10, Scroller is a helper class that implements smooth scrolling of View groups. MScroller and mGDetector are the main character behind the sliding gesture.

11 rows. The array mChildWidth [] records the width of each sub-view.

 

Override onMeasure and onLayout methods:

    onMeasure( widthMeasureSpec,            widthSize =      heightSize =     (mChild ==                  newSpec =   }      

The input parameter is the size of the parent view. When onMeasure is executed for the first time in row 7th, the Child view is initialized Based on the width and height of the parent view (which is equal to the width and height of the screen.

   initChildren( width,      (mChild ==          mChild =  View[3         mChildWidth[SLIDER_PAGE_FIX]=               ( i=SLIDER_PAGE_LEFT; i<=SLIDER_PAGE_RIGHT; i++         mChild[i] =         (mChild[i] !=                childWidth =             (childWidth >                 childWidth =                 mChildWidth[i] =               widthMeasureSpec =              heightMeasureSpec =     }            

The initChildren method initializes the array mChild and records the object of each sub-view.

For four rows, the sub-view with a fixed width is directly set to full screen width. The sub-view whose index is SLIDER_PAGE_MIDDLE is set to width.

7 ~ 19 rows to obtain each sub-View in the ViewGroup, and call their measure Method Based on the width setting of each sub-view.

Therefore, the main activity must execute the setPageWidth method of pushSlider in the onCreate method and set the width of the left and right views.

MPushView. setPageWidth (PushSlider. SLIDER_PAGE_LEFT, screenWidth/2 );
MPushView. setPageWidth (PushSlider. SLIDER_PAGE_RIGHT, screenWidth );

  setPageWidth( index, (index ===

 

OnLayout

    onLayout( changed,  l,  t,  r,                 (mChild[focusedIndex] !=               w =             (w == 0                 w = r-              mChild[focusedIndex].layout(l,t,l+                       ( next=SLIDER_PAGE_LEFT; next<=SLIDER_PAGE_RIGHT; next++             (next ==                               (mChild[next] !=                   w1 =                  x = 0                 ((next== SLIDER_PAGE_LEFT)&&(focusedIndex ==                     x = l-                 } ((next== SLIDER_PAGE_LEFT)&&(focusedIndex ==                     x = l- mChildWidth[SLIDER_PAGE_MIDDLE] -                 } ((next== SLIDER_PAGE_MIDDLE)&&(focusedIndex ==                     x = l+                 } ((next== SLIDER_PAGE_MIDDLE)&&(focusedIndex ==                     x = l-                 } ((next== SLIDER_PAGE_RIGHT)&&(focusedIndex ==                     x = l+                  ((next== SLIDER_PAGE_RIGHT)&&(focusedIndex ==                     x = l+mChildWidth[SLIDER_PAGE_LEFT]+                  mChild[next].layout(x,t,x+                        mLeftPosX = -         (mChild[SLIDER_PAGE_RIGHT] !=              mRightPosX =                      mRightPosX = 0     }          (mChild[focusedIndex] !=               w =             mChild[focusedIndex].layout(l,t,l+   }    

OnLayout is nothing special. It is to layout each sub-view and call the layout (l, t, r, B) method of the sub-view.

The screen width of my mobile phone is 720 pixels. The center view ranges from l = 0 to r = 719. The left side is-359 to 0, and the right side is 720 to 1439.






 OnPageChangedListener mPageChangedListener =   onPageChanged(View v,  =

Activity needs to call mPushView. setOnPageChangedListener (this); register the listener and implement onPageChanged (View v,

 

Related Article

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.