OnInterceptTouchEvent solves the problem of coexistence of the focus of GridView and ImageButton.

Source: Internet
Author: User

We know that most of the Child classes in ViewGroup have Layout, while the child classes in View are mainly various controls. When you click on the screen, what is its event processing?

OnInterceptTouchEvent is the method in ViewGroup. After you click the screen:

Activity -------> onInterceptTouchEvent ();

ViewGroup --------> onInterceptTouchEvent ();

View ----------------> onInterceptTouchEvent ();

By default, the returned value of onInterceptTouchEvent () is false, so that the touch event can be passed to the next layer. If it is set to true, it cannot be passed.

To block the ImageButton focus in the GridView, you can use the following idea to regard the GridView as a ViewGroup. The Code is as follows:

In java code:

Public class MyGridView extends GridView {
Public MyGridView (Context context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
// TODO Auto-generated constructor stub
}
Public MyGridView (Context context ){
Super (context );
// TODO Auto-generated constructor stub
}
Public MyGridView (Context context, AttributeSet attrs ){
Super (context, attrs );
// TODO Auto-generated constructor stub
}
@ Override
Public boolean onInterceptTouchEvent (MotionEvent event ){
// TODO Auto-generated method stub
Return true;
}
}


In xml code:

<Com. example. filemanage. MyGridView
Android: id = "@ + id/gridView"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_below = "@ + id/Title"
Android: layout_above = "@ + id/btnSel"
Android: scrollbars = "vertical"
Android: numColumns = "auto_fit"
Android: verticalSpacing = "10dp"
Android: horizontalSpacing = "10dp"
Android: stretchMode = "columnWidth"/>


Finally, add the android: descendantFocusability = "blocksDescendants" to the layout of the item xml file in the GridView"

In this way, the ImageButon response can be blocked.

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.