Android:如何检测可点击的 RelativeLayout 上的触摸?

问题描述 投票:0回答:0

我有一个自定义布局“MyLayout”,它扩展了

RelativeLayout
并膨胀了
my_layout.xml

public class MyLayout extends RelativeLayout {

    // the constructors are all calling init(context);

    private void init(Context context) {
        if (context != null) {
            LayoutInflater.from(context).inflate(R.layout.my_layout, this);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        System.out.println("touched");
        return super.onTouchEvent(event);
    }
}

这是

my_layout.xml
,它有一个波纹动画:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relative_layout"
        android:clickable="false"
        android:background="@drawable/ripple_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

现在这是当我改变

android:clickable

的值时会发生什么
android:clickable="false"
android:clickable="true"
波纹动画不会出现 波纹动画按预期工作
onTouchEvent(MotionEvent)
将在触摸时被调用
onTouchEvent(MotionEvent)
永远不会被调用

我怎样才能让涟漪动画可见以及

onTouchEvent(MotionEvent)
在触摸时被调用?

  • 其他属性如
    android:focusable
    等对波纹动画的可见性没有任何影响
  • OnTouchListener 和 OnClickListener 永远不会被调用如果
    android:clickable="true"
android touch listener clickable ripple
© www.soinside.com 2019 - 2024. All rights reserved.