Add a response function for the GridView where a blank space is clicked

Source: Internet
Author: User

 

 

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.

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.