我们知道Gridview不能设置onClickListener和onLongClickListener,当GridView中出现了Blank cell,有时需要响应click事件,没有API可以调用。

        AbsListView中的pointToPositon方法可以返回某个点对应的adapter中的数据position,当返回-1时,说明该点不在可见点item上,为空白区域。利用这个方法在dispatchTouchEvent中设置回调,可以解决这个问题。

以下是我实现的可以增加了onClickListener 和onLongClickListener的Gridview,有需要的可以参考一下:

        

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.GridView;

public class ClickableGridView extends GridView {

    public ClickableGridView(Context context){
        super(context);
    }
    public ClickableGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public ClickableGridView(Context context, AttributeSet attrs,  int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private OnNoItemClickListener clickListener;
    private OnNoItemLongClickListener longClickListener;
    private boolean mHasPerformedLongPress = false;
    private boolean isPressed;
    private CheckForLongPress checkForLongPress;

    public interface OnNoItemClickListener {
        public void onNoItemClick();
    }

    public interface OnNoItemLongClickListener{
        public void onNoItemLongClick();
    }

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

    public void setOnNoItemLongClickListener(OnNoItemLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        // The pointToPosition() method returns -1 if the touch event
        // occurs outside of a child View.
        if (pointToPosition((int) event.getX(), (int) event.getY()) == -1) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    isPressed = true;
                    mHasPerformedLongPress = false;
                    if(checkForLongPress == null){
                        checkForLongPress = new CheckForLongPress();
                    }
                    postDelayed(checkForLongPress, ViewConfiguration.getLongPressTimeout());
                    break;
                case MotionEvent.ACTION_UP:
                    if(!mHasPerformedLongPress){
                        removeCallbacks(checkForLongPress);
                        if(clickListener != null){
                            clickListener.onNoItemClick();
                        }
                    }else{
                        mHasPerformedLongPress = false;
                    }
                    isPressed = false;
                    break;
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_MOVE:
                default:
                    removeCallbacks(checkForLongPress);
                    isPressed = false;
                    break;
            }
        }
        return super.dispatchTouchEvent(event);
    }
    private final class CheckForLongPress implements Runnable {

        @Override
        public void run() {
            if (isPressed){
                if (longClickListener != null) {
                    longClickListener.onNoItemLongClick();
                    mHasPerformedLongPress = true;
                }
            }
        }
    }
}