android-youtube-player icon indicating copy to clipboard operation
android-youtube-player copied to clipboard

Popup appearing while scrolling (YouTube player inside recycler view)

Open sandeepasterbyte opened this issue 2 years ago • 6 comments

@PierfrancescoSoffritti How can we remove this popup appearing while user scrolls down in recycler view?

WhatsApp Image 2022-04-29 at 5 23 08 PM .

sandeepasterbyte avatar Apr 29 '22 12:04 sandeepasterbyte

I noticed the same behaviour when using the RecyclerView example in the sample app. It requires further investigation.

PierfrancescoSoffritti avatar May 02 '22 19:05 PierfrancescoSoffritti

That sounds like a reasonable workaround. Thanks!

PierfrancescoSoffritti avatar May 08 '22 14:05 PierfrancescoSoffritti

You can custom the RecyclerView and override the dispatchTouchEvent like this.

public class CustomRecyclerView extends RecyclerView {
    private int touchSlop;
    private List<MotionEvent> touchEventTrack = new ArrayList<>();

    public CustomRecyclerView(@NonNull Context context) {
        super(context);
        touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public CustomRecyclerView(Context context, AttributeSet attributeSet){
        super(context,attributeSet);
        touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public CustomRecyclerView(@NonNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // TODO Only intercept vertical scroll
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:{
                touchEventTrack.clear();
                touchEventTrack.add(MotionEvent.obtain(ev));
                break;
            }
            case MotionEvent.ACTION_MOVE:{
                touchEventTrack.add(MotionEvent.obtain(ev));
                break;
            }
            case MotionEvent.ACTION_UP:{
                touchEventTrack.add(MotionEvent.obtain(ev));
                if (isClick(ev)) {
                    for (int i = 0; i < getChildCount();i++){
                        View view = getChildAt(i);
                        if (!isInView(ev, view)) {
                            continue;
                        }
                        boolean handle = false;
                        for (MotionEvent event : touchEventTrack) {
                            handle = dispatchTransformedTouchEvent(event, view);
                            if (!handle){
                                break;
                            }
                        }
                        if (handle) {
                            MotionEvent cancelEvent = MotionEvent.obtain(ev);
                            cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
                            return onTouchEvent(cancelEvent);
                        }
                    }
                }
                break;
            }
        }
        return onTouchEvent(ev);
    }

    private boolean isInView(MotionEvent ev, View view) {
        Rect rect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
        return rect.contains((int)ev.getX(), (int)ev.getY());
    }

    private boolean isClick(MotionEvent ev) {
        MotionEvent downEvent = this.touchEventTrack.get(0);
        return Math.abs(downEvent.getX() - ev.getX()) < touchSlop && Math.abs(downEvent.getY() - ev.getY()) < touchSlop;
    }


    private boolean dispatchTransformedTouchEvent(MotionEvent event, View child) {
        float offsetX = getScrollX() - child.getLeft();
        float offsetY = getScaleY() - child.getTop();
        event.offsetLocation(offsetX, offsetY);
        boolean handle = child.dispatchTouchEvent(event);
        if (!handle) {
            event.offsetLocation(-offsetX, -offsetY);
        }
        return handle;
    }
}

goyourfly avatar May 13 '22 09:05 goyourfly

@goyourfly's solution works like a charm. Thank you.

chintanbawa avatar May 16 '22 12:05 chintanbawa

@goyourfly Thanks a lot! It worked very fine! I'm grateful!

fcoconstante avatar Jun 08 '22 18:06 fcoconstante

Thank you @goyourfly, perfectly works

swhamdani avatar Jun 24 '22 08:06 swhamdani

@goyourfly Hi above custom recycle view working but seekbar not working only we have to click the the seekbar, tracking not working

rajkanna7 avatar Dec 02 '22 11:12 rajkanna7

@rajkanna7 Yes, the way to fix this problem is to only intercept vertical touch events

goyourfly avatar Dec 05 '22 07:12 goyourfly

if you have the solution please updated code here,

rajkanna7 avatar Dec 08 '22 13:12 rajkanna7

@goyourfly any idea on how to handle this in jetpack compose? am using this youtubeplayer inside AndroidView in a lazy column, and am encountering the same problem, any pointers would be appreciated

dmitrish avatar Jan 03 '23 01:01 dmitrish

Hi, I have updated the RecyclerView example to show you a possible solution to intercept clicks.

PierfrancescoSoffritti avatar Mar 18 '23 10:03 PierfrancescoSoffritti