How to handle click events of parent View and child view in Android

Source: Internet
Author: User

The event types in android include button events and screen Touch events. Touch events are the basic events of screen Touch events, so it is necessary to have a deep understanding of them.

A simple screen Touch action triggers a series of Touch events: ACTION_DOWN-> ACTION_MOVE...-> ACTION_MOVE-> ACTION_UP

When the screen contains a ViewGroup and the ViewGroup contains a sub-view, how does the android system handle Touch events? Is ViewGroup used to process Touch events or child views used to process Touch events? I can only say no to you for sure. Why? Let's take a look at my survey results.
You will understand.

Each View subclass in the android system has the following three methods closely related to TouchEvent processing:
1) public boolean dispatchTouchEvent (MotionEvent ev) is used to distribute TouchEvent
2) public boolean onInterceptTouchEvent (MotionEvent ev) This method is used to intercept TouchEvent
3) The public boolean onTouchEvent (MotionEvent ev) method is used to process TouchEvent
When a TouchEvent occurs, the Activity first passes the TouchEvent to the top-level View,
TouchEvent first arrives at the top-level view's dispatchTouchEvent, which is then distributed by the dispatchTouchEvent method,
If the dispatchTouchEvent returns true, it is handed to the onTouchEvent of this view for processing,
If dispatchTouchEvent returns false, the view's interceptTouchEvent method is used to determine whether to intercept the event,
If interceptTouchEvent returns true, that is, if it is intercepted, it is handed over to its onTouchEvent for processing,
If interceptTouchEvent returns false, it is passed to the sub-view. The dispatchTouchEvent of the sub-view is used to distribute the event.
If the event is passed to the onTouchEvent of the subview of a certain layer, the method returns false, and the event will be passed up from this view, which is received by onTouchEvent.
If false is returned when the onTouchEvent is passed to the top, the event will disappear and the next event will not be received.Copy codeThe Code is as follows: private LayoutInflater inflater
Public View fristView;
Public View secondView;
Private MyViewPager myViewPager;
Public ViewPagerAdapter mViewPagerAdapter;
Private List <View> views;
Public Gallery mGallery;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Inflater = getLayoutInflater ();
FristView = inflater. inflate (R. layout. main1, null );
SecondView = inflater. inflate (R. layout. main2, null );
Views = new ArrayList <View> ();
Views. add (fristView );
Views. add (secondView );
MGallery = (Gallery) fristView. findViewById (R. id. gallery );
MGallery. setAdapter (new ImageAdapter (this ));
MyViewPager = (MyViewPager) findViewById (R. id. pager );
MViewPagerAdapter = new ViewPagerAdapter (views );
MyViewPager. setAdapter (mViewPagerAdapter );
}
// Interface list
Private List <View> views;
Public ViewPagerAdapter (List <View> views ){
This. views = views;
}
// Destroy the arg1 location Interface
@ Override
Public void destroyItem (View arg0, int arg1, Object arg2 ){
(ViewPager) arg0). removeView (views. get (arg1 ));
}
@ Override
Public void finishUpdate (View arg0 ){
// TODO Auto-generated method stub
}
// Obtain the number of current interfaces
@ Override
Public int getCount (){
If (views! = Null)
{
Return views. size ();
}
Return 0;
}
// Initialize the arg1 location Interface
@ Override
Public Object instantiateItem (View arg0, int arg1 ){
(ViewPager) arg0). addView (views. get (arg1), 0 );
Return views. get (arg1 );
}
// Determine whether the interface is generated by the object
@ Override
Public boolean isViewFromObject (View arg0, Object arg1 ){
Return (arg0 = arg1 );
}
@ Override
Public void restoreState (Parcelable arg0, ClassLoader arg1 ){
// TODO Auto-generated method stub
}
@ Override
Public Parcelable saveState (){
// TODO Auto-generated method stub
Return null;
}
@ Override
Public void startUpdate (View arg0 ){
// TODO Auto-generated method stub
}

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.