Discussion on the click event handling of parent View and child view in Android

  • 2020-05-09 19:18:12
  • OfStack

The event types in android are divided into key event and screen touch event. Touch event is the basic event of screen touch event, and it is necessary to have a deep understanding of it.

One of the simplest screen touch actions triggers series 1 Touch events: ACTION_DOWN- > ACTION_MOVE- > ACTION_MOVE- > ACTION_MOVE...- > ACTION_MOVE- > ACTION_UP

How does the android system handle the Touch event when the screen contains one ViewGroup and the ViewGroup contains one child view? Should ViewGroup handle Touch or child view handle Touch? I can only say no to you with certainty. Oh, why? Check out my survey below
I see.

Each subclass of View in the android system has the following three methods closely related to TouchEvent processing:
1) public boolean dispatchTouchEvent(MotionEvent ev) this method is used to distribute TouchEvent
2) public boolean onInterceptTouchEvent(MotionEvent ev) this method is used to intercept TouchEvent
3) public boolean onTouchEvent(MotionEvent ev) this method is used to process TouchEvent
When TouchEvent occurs, first Activity passes TouchEvent to View at the top level,
TouchEvent first reaches dispatchTouchEvent of view at the top, and then is distributed by dispatchTouchEvent method,
If dispatchTouchEvent returns true, it is handed over to onTouchEvent of this view,
If dispatchTouchEvent returns false, the interceptTouchEvent method of view is given to decide whether to intercept the event,
If interceptTouchEvent returns true, that is, intercepts it, then it is left to its onTouchEvent to handle,
If interceptTouchEvent returns false, it is passed to child view, and dispatchTouchEvent of child view begins the distribution of the event.
If the event is passed to the onTouchEvent of a sub-view of a layer 1, and this method returns false, then the event will be passed from this view up to onTouchEvent to be received.
If the onTouchEvent passed to the top also returns false, the event "disappears" and the next event is not received.
 
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; 
} 
// The destruction arg1 Position 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 
} 
// Gets the current interface number  
@Override 
public int getCount() { 
if (views != null) 
{ 
return views.size(); 
} 
return 0; 
} 
// Initialize the arg1 Position interface  
@Override 
public Object instantiateItem(View arg0, int arg1) { 
((ViewPager) arg0).addView(views.get(arg1), 0); 
return views.get(arg1); 
} 
// Determines whether the interface is generated by an 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 articles: