自定义视图`VideoView`已调用setOnTouchListener,但不会覆盖performClick

时间:2017-11-08 13:59:15

标签: android

我得到了一个战国Custom view VideoView has setOnTouchListener called on it but does not override performClick

段:

VideoView mContentView = (VideoView) findViewById(R.id.videoView);
 // Set up the user interaction to manually show or hide the system UI.
mContentView.setOnTouchListener(mDelayHideTouchListener);


private final View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        show();
        if (AUTO_HIDE) {
            delayedHide(AUTO_HIDE_DELAY_MILLIS);
        }
        return false;
    }
};

有了这个警告,应用程序在Android API24上正常运行,但应用程序在Android API23上崩溃。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我认为标准的VideoView不会覆盖performClick方法。 因此,您可以通过扩展自定义VideoView来处理此问题。

<强> myVideoView.java

package your.packagename.here;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.VideoView;

    public class myVideoView extends VideoView {
        public myVideoView(Context context) {
            super(context);
    }

    public myVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public myVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean performClick() {
        super.performClick();
        return true;
    }
}

<强> playerActivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    tools:context="your.packagename.here.playerActivity">
    <your.packagename.here.myVideoView
        android:id="@+id/fullscreen_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"/>
</RelativeLayout>

<强> playerActivity.java

// GLOBAL VARIABLE: isTouched (boolean)
        private void initActivity() {
            myVideoView vPlayer = findViewById(R.id.fullscreen_content);
            vPlayer.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    if(isTouched) return false;
                    switch (motionEvent.getAction()) {
                        case MotionEvent.ACTION_UP:
                            myVideoView vPlayer = (myVideoView)view;
                            vPlayer.seekTo(vPlayer.getDuration());
                            isTouched = true;
                            break;
                            default:
                                break;
                    }
                    view.performClick();
                    return true;
                }
            });
    }