setOnTouchListener报警告

警告详细说明为:

Custom view `view` has setOnTouchListener called on it but does not override performClick less... (Ctrl+F1)
If a View that overrides onTouchEvent or uses an OnTouchListener does not also implement performClick and call it when clicks are detected, the View may not handle accessibility actions properly. Logic handling the click actions should ideally be placed in View#performClick as some accessibility services invoke performClick when a click action should occur.  Issue id: ClickableViewAccessibility

翻译一下大概意思就是:有可能会和点击事件发生冲突.

会出现问题的情况为:

view.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
});
view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        
    }
});

在onTouch中返回ture,同时又添加了onClick监听,这时onClick就不会执行了,事件被onTouch消化掉了.来查看一下执行顺序就知道了,onTouchEvent=>performClick=>onClick,所以在onTouch返回ture时,同时又添加了onClick监听,正确的处理方法应该是在onTouch中适当的地方执行performClick方法,来触发onClick.

弄懂了原因以后可以添加一个注解就解决了

@SuppressLint("ClickableViewAccessibility")

 

你可能感兴趣的:(Android)