【问题标题】:Custom view `VideoView` has setOnTouchListener called on it but does not override performClick自定义视图 `VideoView` 调用了 setOnTouchListener 但不覆盖 performClick
【发布时间】:2018-04-21 18:21:18
【问题描述】:

我收到了 Warring 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 上应用崩溃。

提前感谢您的帮助。

【问题讨论】:

  • 为什么不直接覆盖这个方法?
  • 如果我只覆盖然后它显示另一个 onTouch 方法的交战。

标签: android


【解决方案1】:

我认为标准 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;
                }
            });
    }

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多