安卓:检测单击事件在GridView控件内的LinearLayout空控件、单击、事件、GridView

2023-09-05 07:54:52 作者:惡女嗵殺

我有以下问题。我有一个的GridView 的LinearLayout 如下形象。我想检测点击事件,当用户点击在的GridView的空,在图像中,我想要的位置在红色区域,绿色区域也在里面。

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.

如果我加入 onClickListener 的GridView :错误,因为适配器不能添加单击事件 如果我加入 onItemClickListener 的GridView :我只是发现,其中存在的项目(在图像中是白色的盒) 如果我加入 onClickListener 的LinearLayout 我只可以检测单击事件的绿地面积,而不是红色区域。 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>

感谢:)

推荐答案

由于父母的LinearLayout可以分配自己的 OnClickListener ,问题只是如何检测点击在出现的子视图外的GridView控件。你也可以继承GridView和覆盖 dispatchTouchEvent()的方法来做到这一点。使用 pointToPosition()的方法,我们可以判断触摸事件发生时,孩子的意见外,并用一个接口来通知监听器,如果它是。在下面的例子中, OnNoItemClickListener 接口提供该功能。

Since 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.

public class TouchyGridView extends GridView
{
    // Depending on how you're creating this View,
    // you might need to specify additional constructors.
    public TouchyGridView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    private OnNoItemClickListener listener;
    public interface OnNoItemClickListener
    {
        public void onNoItemClick();
    }

    public void setOnNoItemClickListener(OnNoItemClickListener listener)
    {
        this.listener = listener;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event)
    {
        // The pointToPosition() method returns -1 if the touch event
        // occurs outside of a child View.
        // Change the MotionEvent action as needed. Here we use ACTION_DOWN
        // as a simple, naive indication of a click.
        if (pointToPosition((int) event.getX(), (int) event.getY()) == -1
            && event.getAction() == MotionEvent.ACTION_DOWN)
        {
            if (listener != null)
            {
                listener.onNoItemClick();
            }
        }
        return super.dispatchTouchEvent(event);
    }
}