- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 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/
我有一个布局,其中包含另一个布局: 我需要在包含的布局中添加 RippleEffect 和 StateListAnimator。 例子: RippleEffect 和 StateLis
我有一个 Android 项目,我正在使用 following approach 更改我的一个 View 中出现的波纹效果的颜色。 虽然它可以工作,但我确实需要根据我的风格将此颜色重置回默认值。下面我
正如标题。 rippleEffect 是 CATransition 私有(private) api 的类型吗? CATransition * canimation = [CATransition an
我在 CardView 中有一个 TextView。通过添加 OnClick 事件并添加属性在 CardView 上为 Lollipop 启用涟漪效果时: android:foreground="?a
我是一名优秀的程序员,十分优秀!