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

Source: Internet
Author: User

Next

<Android development practice ViewGroup realize sliding window (a) http://www.cnblogs.com/inkheart0124/p/3532862.html>

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

There are three functions for processing touch events in ViewGroup.

Public boolean dispatchTouchEvent (MotionEvent ev); event dispatch

Public boolean onInterceptTouchEvent (MotionEvent ev); event Interception

Public boolean onTouchEvent (MotionEvent ev); Event Processing

(There are only two views, dispatchTouchEvent and onTouchEvent. ViewGroup inherits the View and adds onInterceptTouchEvent)

 

A complete touch event: ACTION_DOWN-> ACTION_MOVE...-> ACTION_UP

The returned values for ACTION_DOWN processing are important. If false is returned for dispatchTouchEvent, this indicates that this touch event is not required by me, and subsequent move and up operations will not be passed to my ViewGroup.

 

 action ==((gdHandled) && (action == + action +   + action +  action ==((gdHandled) && (action == + action +   + action +  action = 

Rewrite these three functions. First, the event is directly thrown to the mGDetector for processing, and then the message is transferred to the sub View by calling the system's dispatch mechanism.

Note that the Down message cannot be intercepted. The ViewGroup uses the move and up operations to differentiate gestures. When a down message is received, we are not sure whether the user wants to slide or operate the control in the Child view. If a Down message is intercepted, it means that the message group will not be passed to the subview.

 

OnTouchEvent does not do anything and returns true directly. This method will call super in the dispatchTouchEvent function. when dispatchTouchEvent (ev) is run, its return value is super. the return value of dispatchTouchEvent (ev). Here, at least true must be returned when the message is down. Otherwise, as mentioned above, the whole group of messages will not be passed to the pushslider.

 

Gesture:

The PushSlider class implements the android. view. GestureDetector. OnGestureListener interface. In the previous constructor, we created a gesture object and registered the listener.

mGDetector =  GestureDetector(mContext,);

The following describes how to implement OnGestureListener.

Public boolean onDown (MotionEvent ev); process down messages

Public void onLongPress (MotionEvent arg0); long press

Public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY); drag it to

Public boolean onFling (MotionEvent eDown, MotionEvent eMove, float velocityX, float velocityY );

Public void onShowPress (MotionEvent arg0); after a down message occurs, it is used to display highlighted and other visual processing.

Public boolean onSingleTapUp (MotionEvent ev); process one touch

The detailed explanation will not be written. The android developer official website will explain in detail. Here I only need to deal with onFling and onSingleTapUp. The onScroll code is useless and blocked later.

 

OnFling is the occurrence of donw-move .... -up is called at the end of the up event. Based on the input parameters float velocityX and float velocityY, it is determined whether this is the desired slide-throwing gesture.

  boolean onFling(MotionEvent eDown, MotionEvent eMove, velocityX,     log( + eDown.getX() +  + eMove.getX() + +           ((eDown.getX() - eMove.getX() > FLING_DISTANCE*mDensity) && (Math.abs(velocityX)>                      flingTo(MOVE_FLAG_ALLOW_LEFT,             log(                                   ((eMove.getX() - eDown.getX() > FLING_DISTANCE*mDensity) && (Math.abs(velocityX)>                      flingTo(MOVE_FLAG_ALLOW_RIGHT,             log(                     log(       }

5 ~ 11 rows. The gesture is from right to left <--

14 ~ 20 rows, gesture from left to right -- >>>

Determine whether to slide to the next page based on the relative displacement of eMove and eDown and the speed of X.

 

OnSingleTapUp is called when a click (touch) such as down-up occurs.

       Rect focusRect =        x =      left = focusRect.left-      right = focusRect.right-              ((x < left) &&         flingTo(MOVE_FLAG_ALLOW_RIGHT,         log(               } ((x > right) &&         flingTo(MOVE_FLAG_ALLOW_LEFT,         log(                log(              }

3 or 4 rows to obtain the area of the Child view of the current focus;

Lines 6 and 7. The obtained HitRect is the relative coordinate of viewgroup. It must be converted to the actual coordinates. mScroller. getCurrX () is the value of the x position of viewgroup relative to the screen origin.

Because I have a semi-screen sliding sub-view, when the set menu of this semi-screen is called, I hope to close the menu outside the menu and return to the main interface. The onSingleTapUp method is rewritten to handle this situation.

 

FlingTo, there are actually many ways to implement sliding. The most intuitive way is to calculate the position of each sub-View as we did in onLayout, and then layout again.

However, I think using view's own Scroller class is the simplest and most worry-free.

   flingTo(                     startX =      endX=           (direct ==         focusedIndex ++          (focusedIndex ==             endX =           (focusedIndex ==             endX =          dx = endX -         mScroller.startScroll(startX, , dx, ,(fast?:     }         focusedIndex --         (focusedIndex ==             endX =           (focusedIndex ==             endX =          dx = endX -         mScroller.startScroll(startX, , dx, ,(fast?:      log( +   }

Row 5, startx, to obtain the position of the current ViewGroup (relative to the screen origin );

10 ~ 15 rows. switch the focus to get the endX based on the new focus and the position of the ViewGroup After sliding;

Line 18: directly call the startScroll method of Scroller and start to slide. It's easy!

 

StartScroll prototype: public void startScroll (int startX, int startY, int dx, int dy, int duration)

The meaning of the five input parameters is clear at a glance.

 

The direct input parameter of the flingTo function is the moving direction, and a boolean fast parameter represents the sliding speed. It is used to determine the duration of startScroll. 100 and 250 are the actual experience values.

Fast = false. Let the user see a clear Sliding Process.

When you press the menu or cancel key, fast = true. Quickly call out or close the interface.

 

The menu key and canel key are not defined in the management scope of PushSlider. The two key values are still processed by activity, and PushSlider only needs to provide a public method.

In short, PushSlider manages its three sub-views, including display, position, sliding, and focus. All the things in specific functions are active activities.

Therefore, in addition to changing the focus of the activity listener (see the previous article), PushSlider also needs to provide some query and operation interfaces, such as querying the current focus and querying whether the current focus is moving.

There are two important operations:

Public void pushForbid (boolean forbid ){
MForbidden = forbid;
}

In some cases, you can use pushForbid to prevent users from sliding the focus.

 

In another public void moveToPageById (int index), the activity can go directly to a subview. For example, you can press the menu key to call the subview of the set menu.

If the code is not pasted, The flingTo (fast) mode is still called.

 

PushSlider: Let's see how to use it in the activity.

Layout/main. xml

                                                                                                                                      

6 ~ Add PushSlider to 15 rows and insert three sub-views, which are

 

                     mPushView =      screenWidth =     mPushView.setPageWidth(PushSlider.SLIDER_PAGE_LEFT, screenWidth/2      mPushView.setOnPageChangedListener(             } 

Set main. xml to contect view;

7 ~ Row 13: Obtain the PushSlider object handle, set the width of the left and right views, and set the focus switching listener.


Oh ~~~ Complete the entire article ~~

New year's collection of character, later sent the source code, do not know how to upload the source code?

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.