如何设置手势检测器YoutubePlayerFragment

如何设置手势检测器YoutubePlayerFragment

本文介绍了如何设置手势检测器YoutubePlayerFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 YoutubePlayerFragment 类来播放视频。我想添加一个 GestureListener ,但他们没有方法来设置手势检测器。我想:

I am using the YoutubePlayerFragment class to play videos. I want to add a GestureListener, but their is no method to set a gesture detector. I tried:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:id="@+id/player_holder"
    android:visibility="gone"
    android:layout_weight="40">

    <RelativeLayout
        android:layout_width="match_parent"
        android:duplicateParentState="true"
        android:layout_height="match_parent">

        <fragment
            android:id="@+id/player_fragment"
            android:duplicateParentState="true"
            android:name="com.google.android.youtube.player.YouTubePlayerFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <LinearLayout
            android:layout_width="match_parent"
            android:id="@+id/gestureCatcher"
            android:orientation="vertical"
            android:duplicateParentState="true"
            android:layout_height="match_parent" />

    </RelativeLayout>
</LinearLayout>

    private static final int SWIPE_MIN_DISTANCE       = 120;
    private static final int SWIPE_MAX_OFF_PATH       = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;

    class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) return false;
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Toast.makeText(NonStopActivity.this, "Left Swipe", Toast.LENGTH_SHORT)
                            .show();
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Toast.makeText(NonStopActivity.this, "Right Swipe", Toast.LENGTH_SHORT)
                            .show();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
    }

这是我如何设置的动作探测器探测到的LinearLayout

This is how I set the gesture detector to the LinearLayout

gestureDetector = new GestureDetector(this, new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
};

LinearLayout gestureCatcher = (LinearLayout) findViewById(R.id.gestureCatcher);
gestureCatcher.setOnTouchListener(gestureListener);

但覆盖块 YoutubePlayerFragment 。有没有解决这个问题的解决方案?

But the overlay blocks YoutubePlayerFragment. Is there a solution around this problem?

推荐答案

对于那些谁有同样的问题,最好的办法是延长 YouTubePlayerFragment

For those who had the same issue, the best way is to extend YouTubePlayerFragment:

public class DraggableYouTubePlayerFragment extends YouTubePlayerFragment {

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        final View view = super.onCreateView(layoutInflater, viewGroup, bundle);
        FrameLayout wrapper = new FrameLayout(viewGroup.getContext()) {
            @Override
            public boolean onInterceptTouchEvent(MotionEvent ev) {
                return true;
            }
        };
        wrapper.addView(view);
        return wrapper;
    }
}

这篇关于如何设置手势检测器YoutubePlayerFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 02:21