GridView provides OnItemClickListener for us. However, sometimes we need to obtain the event of the user clicking the blank area for some operations.
For example, in the group management interface, you can click a blank area to cancel the current operation status when deleting a member. Unfortunately, this is a basic function.
The SDK was not provided directly, and I rarely found any information on the Internet, so I expanded it.
First, you use a custom class, such
Public class MyGridView extends GridView, and then add the following code
Public interface ontouchblkpositionlistener {/***** @ return indicates whether to terminate the event route */boolean ontouchblkposition ();} public void listener (ontouchblkpositionlistener) {mtouchblkposlistener = listener ;} @ Override public boolean onTouchEvent (MotionEvent event) {if (mtouchblkposlistener! = Null) {if (! IsEnabled () {// A disabled view that is clickable still consumes the touch // events, it just doesn't respond to them. return isClickable () | isLongClickable ();} if (event. getActionMasked () = MotionEvent. ACTION_UP) {final int motionPosition = pointToPosition (int) event. getX (), (int) event. getY (); if (motionPosition = Blank_POSITION) {return mtouchblkposlistener. ontouchblkposition () ;}} return super. onTouchEvent (event );}
Note the following two details:
1. During Processing, ontouchbalankposition must return true to terminate the propagation of the routing event. 2. to capture the ACTION_UP event, if ACTION_DOWN is used, the problem of clicking twice appears, it may be because one touch may cause multiple ACTION_DOWN events.