关于AdapterView点击空白处事件

来源:互联网 发布:centos 安装php 编辑:程序博客网 时间:2024/05/01 23:41

在项目中遇到GridView在item不满时,要求点击空白处也能响应某些事件,发现AdapterView是没有OnClick事件的;

解决方案是重写他的onTouch事件,来判断不是他的item,从而实现空白处点击事件。


<span style="font-size:18px;">   @Override    public boolean onTouchEvent(MotionEvent ev) {        if (mTouchInvalidPosListener == null) {            return super.onTouchEvent(ev);        }        if (!isEnabled()) {            // A disabled view that is clickable still consumes the touch            // events, it just doesn't respond to them.            return isClickable() || isLongClickable();        }        if (ev.getActionMasked() == MotionEvent.ACTION_UP) {            //判断当前所在位置是否是他的item位置pos            final int motionPosition = pointToPosition((int) ev.getX(), (int) ev.getY());            if (motionPosition == INVALID_POSITION) {                super.onTouchEvent(ev);                return mTouchInvalidPosListener.clickInvalidPosition(ev.getActionMasked());            }        }        return super.onTouchEvent(ev);    }    /**     * 点击空白区域的响应事件     */    public interface OnClickInvalidPositionListener {        boolean clickInvalidPosition(int pos);    }</span>


0 0
原创粉丝点击