温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何进行Gridview的实现

发布时间:2022-01-07 22:17:15 来源:亿速云 阅读:126 作者:柒染 栏目:移动开发

如何进行Gridview的实现,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

        我们知道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的实现问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI