0

I would like to create a RecyclerView in which a user can long click an image and preview the full sized image until they release the long click.

I have it mostly working but the issue I am having is that if I begin the long click, then drag my finger (while still holding the click down), the listener no longer waits for my ACTION_UP event and the preview image never goes away. Is there a way to sort of ignore the dragging/scrolling so that my preview image view goes away when I release the long click?

This is what I have for event listeners:

       /* 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;
            }
        });
jbmcle
  • 771
  • 3
  • 12
Isaac Perez
  • 570
  • 2
  • 15
  • 31

1 Answers1

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;
                    }
                });

This will work and your code is not working as longClickListener does't get's the event of action down(and neither of action down) and what you are doing currently is setting the listener for touch which never got Action_DOWN i.e by default the View's ontouch() return false on Action_Down so u have to override and return true before action down is called so that it get's action move and action up etc.

Anmol
  • 8,110
  • 9
  • 38
  • 63