使ImageView在长按RecyclerView时显示,而在释放时消失?

时间:2018-09-09 04:18:36

标签: android android-recyclerview android-event

我想创建一个RecyclerView,用户可以在其中长按一下图像并预览完整尺寸的图像,直到他们松开长按。

我大多数情况下都可以正常工作,但是我遇到的问题是,如果我开始长按,然后拖动手指(同时保持按下状态),则侦听器将不再等待我的ACTION_UP事件,并且预览图像永远不会消失。有没有一种方法可以忽略拖动/滚动,以便在释放长按时我的预览图像视图消失?

  

这是事件监听器的功能:

       /* Long press will trigger hover previewer */
        holder.thumbnailImageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View pView) {

                holder.thumbnailImageView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View pView, MotionEvent pEvent) {
                        pView.onTouchEvent(pEvent);
                        // We're only interested in when the button is released.
                        if (pEvent.getAction() == MotionEvent.ACTION_UP) {
                            if (isImageViewPressed) {
                                // Do something when the button is released.
                                isImageViewPressed = false;
                                mHoverView.setVisibility(View.GONE);
                            }
                        }                                                 
                        return false;
                    }
                });

                isImageViewPressed = true;
                GlideApp.load(item.getUrl()).into(mHoverView);
                mHoverView.setVisibility(View.VISIBLE);
                return true;
            }
        });

1 个答案:

答案 0 :(得分:0)

/* Long press will trigger hover previewer */
        holder.thumbnailImageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View pView) {
                isImageViewPressed = true;
                GlideApp.load(item.getUrl()).into(mHoverView);
                mHoverView.setVisibility(View.VISIBLE);
                return true;
            }
        });

       holder.thumbnailImageView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View pView, MotionEvent pEvent) {
                        pView.onTouchEvent(pEvent);
                        // We're only interested in when the button is released.
                        if (isImageViewPressed && pEvent.getAction() == MotionEvent.ACTION_UP) {
                                // Do something when the button is released.
                                isImageViewPressed = false;
                                mHoverView.setVisibility(View.GONE);
                        }                                                 
                        return true;
                    }
                });

这将起作用,并且您的代码无法正常工作,因为longClickListener并没有引起动作下降(动作也没有下降)的事件,而您当前正在做的是将监听器设置为触摸< strong> 从未执行过Action_DOWN ,即默认情况下,视图的ontouch()在Action_Down上返回false,因此您必须重写并返回true ,这样才能调用向下操作它会采取行动并采取行动等。