android-youtube-player
android-youtube-player copied to clipboard
Popup appearing while scrolling (YouTube player inside recycler view)
@PierfrancescoSoffritti How can we remove this popup appearing while user scrolls down in recycler view?
.
I noticed the same behaviour when using the RecyclerView example in the sample app. It requires further investigation.
That sounds like a reasonable workaround. Thanks!
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's solution works like a charm. Thank you.
@goyourfly Thanks a lot! It worked very fine! I'm grateful!
Thank you @goyourfly, perfectly works
@goyourfly Hi above custom recycle view working but seekbar not working only we have to click the the seekbar, tracking not working
@rajkanna7 Yes, the way to fix this problem is to only intercept vertical touch events
if you have the solution please updated code here,
@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
Hi, I have updated the RecyclerView example to show you a possible solution to intercept clicks.