码迷,mamicode.com
首页 > 其他好文 > 详细

可点击的Gridview

时间:2016-05-08 20:04:13      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:gridview android onclick onlongclick

        我们知道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;
                }
            }
        }
    }
}


可点击的Gridview

标签:gridview android onclick onlongclick

原文地址:http://5232047.blog.51cto.com/5222047/1771211

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!