We usually respond by clicking on the item event in the ListView or GridView, but many times we also
Want to hear the click on the blank area of the event to do more processing. This article takes the GridView as an example to give a realization
Method that extends the GridView and can be used as a generic control. The ListView implementation should also be similar
Well, I've written another article before, but it's a action_up event, but it's more generic.
The code is relatively simple, put the code before the explanation
Public classMyGridViewextendsGridView { Public InterfaceOntouchinvalidpositionlistener {/*** Motionevent can use Motionevent.action_down or motionevent.action_up to make judgments as needed *@returnwhether to terminate the route of the event*/ BooleanOntouchinvalidposition (intmotionevent); } /*** response and processing interface when clicking on an empty area*/ Public voidSetontouchinvalidpositionlistener (Ontouchinvalidpositionlistener listener) {Mtouchinvalidposlistener=Listener; } @Override Public Booleanontouchevent (Motionevent event) {if(Mtouchinvalidposlistener = =NULL) { return Super. Ontouchevent (event); } if(!isenabled ()) { //A disabled view that's clickable still consumes the touch//events, it just doesn ' t respond to them. returnIsclickable () | |islongclickable (); } Final intMotionposition = Pointtoposition ((int) Event.getx (), (int) event.gety ()); if(Motionposition = =invalid_position) { Super. Ontouchevent (event); returnmtouchinvalidposlistener.ontouchinvalidposition (event.getactionmasked ()); } return Super. Ontouchevent (event); }}
The main is to overload the touch processing function, and take advantage of API Pointtoposition to get the item currently clicked, when you click on the blank area,
Returns Invalid_position, which makes it possible to determine the click of a blank area.
Use the following code, as shown. In the Ontouchinvalidposition event you can do your own response action,
It is usually used more often to "click on a blank place to cancel the current operation." Here I am used to implement click Blank Place let parent container
Displays the touch discoloration effect. Notice the meaning of the return value, and when it returns false, it is referred to the parent control for processing, where the
The effect usually causes the layout of the outer layer to show the clicked effect. When return true indicates that you have handled the event and is not
Let the event flow up again.
Mgridview.setontouchinvalidpositionlistener (new Ontouchinvalidpositionlistener () { @Override Public boolean ontouchinvalidposition (int motionevent) { returnfalse // do not terminate a routed event to let a parent control handle an event } });
As below, click the red circle where the entire layout will change color, of course, the color of your layout must first set the corresponding drawable.
Routed events are a set of basic and important mechanisms for all GUI frameworks, and I'll write an article specifically to talk about the next
The routing mechanism on Android.
Reprint please indicate the original location
Android Practice Note (vi): GridView Click on an empty place event extension