TextView 上的 OnClick 事件停止 CardView 上的 RippleEffect

2024-03-10

我在 CardView 中有一个 TextView。通过添加 OnClick 事件并添加属性来在 CardView 上启用 Lollipop 的涟漪效果时:

android:foreground="?android:attr/selectableItemBackground"

效果很好。但是在TextView中添加OnClick事件后,当我在TextView之外单击时,仍然会显示波纹效果,但在单击TextView区域时不会显示。

即使我单击 TextView 也可以显示波纹吗?

这是 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">

    <android.support.v7.widget.CardView
        android:id="@+id/news_card"
        android:foreground="?android:attr/selectableItemBackground"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/text"
            android:text="Test String"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </android.support.v7.widget.CardView>

</RelativeLayout>

这是代码:

protected void onCreate (Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView (R.layout.activity_main);

    View v = findViewById (R.id.news_card);
    v.setOnClickListener (new View.OnClickListener () {
        @Override public void onClick (final View v) {

        }
    });

    View textView = findViewById (R.id.text);
    textView.setOnClickListener (new View.OnClickListener () {
        @Override public void onClick (final View v) {

        }
    });
}

一种选择是将按下状态和热点位置转发到父视图。

// Since the host view actually supports clicks, you can return false
// from your touch listener and continue to receive events.
myTextView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent e) {
        // Convert to card view coordinates. Assumes the host view is
        // a direct child and the card view is not scrollable.
        float x = e.getX() + v.getLeft();
        float y = e.getY() + v.getTop();

        // Simulate motion on the card view.
        myCardView.drawableHotspotChanged(x, y);

        // Simulate pressed state on the card view.
        switch (e.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                myCardView.setPressed(true);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                myCardView.setPressed(false);
                break;
        }

        // Pass all events through to the host view.
        return false;
    }
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TextView 上的 OnClick 事件停止 CardView 上的 RippleEffect 的相关文章