Android Practice Notes (6): GridView click the blank area event extension, androidgridview

Source: Internet
Author: User

Android Practice Notes (6): GridView click the blank area event extension, androidgridview

 

We usually respond to the click Item event in ListView or GridView, but many times we also

You want to listen to the events in the blank area for more processing. This article uses the GridView as an example to provide an implementation

And can be used as a universal control. The implementation of ListView should be similar.

Well, I wrote another article before, but it monitors ACTION_UP events, but this article is more common.

The code is relatively simple. paste the code first and then explain it.

Public class MyGridView extends GridView {public interface OnTouchInvalidPositionListener {/*** motionEvent can be used by MotionEvent. ACTION_DOWN or MotionEvent. ACTION_UP and so on to determine * @ return whether to terminate the event route */boolean onTouchInvalidPosition (int motionEvent) as needed );} /*** response and processing interface when a blank area is clicked */public void setOnTouchInvalidPositionListener (OnTouchInvalidPositionListener) {mTouchInvalidPosListener = listener ;} @ Override public boolean onTouchEvent (MotionEvent event) {if (mTouchInvalidPosListener = null) {return super. onTouchEvent (event);} if (! IsEnabled () {// A disabled view that is clickable still consumes the touch // events, it just doesn't respond to them. return isClickable () | isLongClickable ();} final int motionPosition = pointToPosition (int) event. getX (), (int) event. getY (); if (motionPosition = INVALID_POSITION) {super. onTouchEvent (event); return mTouchInvalidPosListener. onTouchInvalidPosition (event. getActionMasked ();} return super. onTouchEvent (event );}}

It mainly reloads the touch processing function and uses the api pointToPosition to obtain the currently clicked item. When you click the blank area,

INVALID_POSITION is returned, so you can determine that the blank area is clicked.

 

The following code is used. In the onTouchInvalidPosition event, you can perform your own response operations,

Generally, "click a blank area to cancel the current operation" is used ". Here I used to make the parent container

Display the color-changing effect of the touch. Pay attention to the meaning of the return value. When false is returned, it means that it is handled by the parent control.

Normally, the Layout of the outer layer is clicked. When return true, it indicates that you have handled the event and not

Let the event be passed up.

MGridView. setOnTouchInvalidPositionListener (new OnTouchInvalidPositionListener () {@ Override public boolean onTouchInvalidPosition (int motionEvent) {return false; // do not terminate the routing event so that the parent control can process the event }});

  

As shown in the following figure, the entire Layout will change color when you click the Red Circle. Of course, you must set the corresponding drawable for Layout.

 

Routing events are a basic and important mechanism of all GUI frameworks. I will write an article to talk about it later.

The routing mechanism on Android.

 

Reprinted to indicate the original article location

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.