gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:47:27 27 4
gpt4 key购买 nike

我在 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) {

}
});
}

最佳答案

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

// 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;
}
});

关于android - TextView 上的 OnClick 事件停止 CardView 上的 RippleEffect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28182251/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com