在TextView的OnClick事件停止RippleEffect上CardView [英] OnClick event on TextView stops RippleEffect on CardView
问题描述
我有一个CardView里面一个TextView。当通过添加OnClick事件,并添加属性为实现棒棒糖上CardView的连锁反应:
的android:前景=机器人:ATTR / selectableItemBackground
它工作正常。但添加一个onclick事件TextView的后,产生的连锁反应仍显示当我点击的TextView之外,但在TextView的区域点击时不会显示。
有没有去甚至显示纹波当我点击的TextView?
下面是XML:
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingLeft =@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
机器人:paddingBottom会=@扪/ activity_vertical_margin
工具:上下文=MainActivity。> < android.support.v7.widget.CardView
机器人:ID =@ + ID / news_card
机器人:前景=机器人:ATTR / selectableItemBackground
机器人:layout_width =match_parent
机器人:layout_height =match_parent> <的TextView
机器人:ID =@ + ID /文
机器人:文字=测试字符串
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT/> < /android.support.v7.widget.CardView>< / RelativeLayout的>
下面是code:
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); 视图V = findViewById(R.id.news_card);
v.setOnClickListener(新View.OnClickListener(){
@覆盖公共无效的onClick(最终查看V){ }
}); 查看的TextView = findViewById(R.id.text);
textView.setOnClickListener(新View.OnClickListener(){
@覆盖公共无效的onClick(最终查看V){ }
});
}
一种选择是通过向父视图转发pressed状态和热点的位置。
//由于主机观点实际上支持点击,就可以返回false
//从你的触摸监听器,并继续接收事件。
myTextView.setOnTouchListener(新View.OnTouchListener(){
@覆盖
公共布尔onTouch(视图V,MotionEvent五){
//转换为卡片视图坐标。假定主机的看法是
//直接子和卡观点是不滚动的。
浮X = e.getX()+ v.getLeft();
浮Y = e.getY()+ v.getTop(); //模拟在卡片视图的议案。
myCardView.drawableHotspotChanged(X,Y); //模拟pressed卡上的视图状态。
开关(e.getActionMasked()){
案例MotionEvent.ACTION_DOWN:
myCardView.set pressed(真);
打破;
案例MotionEvent.ACTION_UP:
案例MotionEvent.ACTION_CANCEL:
myCardView.set pressed(假);
打破;
} //通过主机视图通过所有事件。
返回false;
}
});
I have a TextView inside a CardView. When enabling the ripple effect for Lollipop on the CardView by adding a OnClick event and adding the property:
android:foreground="?android:attr/selectableItemBackground"
It works fine. But after adding a OnClick event to the TextView, the ripple effect still shows up when I click outside of the TextView, but it won't show when clicking on the TextView area.
Is there away to show the ripple even when I click the TextView?
Here is 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>
Here is the code:
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) {
}
});
}
One option is to forward the pressed state and hotspot position through to the parent 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;
}
});
这篇关于在TextView的OnClick事件停止RippleEffect上CardView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!