I have a following problem. I have a GridView inside LinearLayout as
image below. I want to detect click event when user click at empty space of GridView,
in the image, the location that I want is the red area and also inside the
green area.
But I have following problems.
- If I
add onClickListener for GridView: error because Adapter
cannot add click event.
- If I
add onItemClickListener for GridView: I just can detect
where exist items (in the image is the white box)
- If I
add onClickListener for LinearLayout I just can detect
click event on green area, not red area.
So how can I fix above problem.
@Edit: my layout looks like:
<LinearLayout>
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
Elena Glibart
19-Nov-2014Since the parent LinearLayout can be assigned its own OnClickListener, the issue is only how to detect a click within the GridView that occurs outside of its child Views. You can subclass GridView and override the dispatchTouchEvent() method to accomplish this. Using the pointToPosition()method, we can determine if a touch event occurs outside of the child Views, and use an interface to notify a listener if it is. In the following example, the OnNoItemClickListener interface provides that functionality.