Android onTouchListener 不工作,只在 Padding 区域工作
Android onTouchListener not working, works only at the Padding area
我肯定做错了什么。我有一个 ReclyclerView
并且每个单元格都有一个 XML home_cells.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingRight="12dp"
android:paddingLeft="12dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:clickable="true">
<android.support.v7.widget.CardView
android:id="@+id/home_cv"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
......
android:paddingTop="16dp">
<LinearLayout
....
android:orientation="horizontal">
<ImageView
...
android:src="@drawable/abc_ratingbar_full_material" />
<LinearLayout
......
android:orientation="vertical">
<TextView
.....
android:textStyle="bold" />
<TextView
....
android:textColor="@color/secondaryTextColor" />
</LinearLayout>
<ImageView
....
android:src="@mipmap/ic_chevron_right_black_24dp" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
<!--<lay-->
</LinearLayout>
我有适配器...一切正常...完全没问题。
但是,当我尝试实施 onClickLister 来完成单元格视图时 mView
它不起作用。如果我触摸角落(我有填充物的地方),它就可以工作。
我不知道发生了什么。如果我将侦听器设置为单元格中的每个元素,它就可以工作...但不是整个单元格。
下面是我的viewHolder
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
CardView cv;
ImageView icon;
TextView heading;
TextView subheading;
View mView;
public MyViewHolder(View itemView) {
super(itemView);
Log.d(TAG, "setting images and items");
mView=itemView;
cv = (CardView)itemView.findViewById(R.id.home_cv);
icon =(ImageView) itemView.findViewById(R.id.home_rv_icon);
heading =(TextView) itemView.findViewById(R.id.heading);
subheading =(TextView) itemView.findViewById(R.id.subhead);
icon.setOnClickListener(this); // Working
mView.setOnClickListener(this); // NOT working
mView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// NOT Working
return true;
}
});
}
@Override
public void onClick(View view) {
//
Log.d(TAG,"on click");
if(clickListener!=null){
Log.d(TAG,"listner notnull");
clickListener.itemClick(view,getAdapterPosition());
}
}
}
public interface RVClickListener{
//OnClick will work, but for better coding standard i made this listener to call the onclick from the fragment it self..
public void itemClick(View view, int position );
}
经过几个小时的努力,终于修好了。问题来了。
由于我的布局有子视图(布局),onclicklistener 正在被它们捕获。
所以用一个简单的办法解决了。我关闭了所有子视图的可点击并将点击侦听器应用于父视图。
致所有子观点:
android:clickable="false"
从 CardView 布局的每个子视图中删除 longClickable = "true"
我肯定做错了什么。我有一个 ReclyclerView
并且每个单元格都有一个 XML home_cells.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingRight="12dp"
android:paddingLeft="12dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:clickable="true">
<android.support.v7.widget.CardView
android:id="@+id/home_cv"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
......
android:paddingTop="16dp">
<LinearLayout
....
android:orientation="horizontal">
<ImageView
...
android:src="@drawable/abc_ratingbar_full_material" />
<LinearLayout
......
android:orientation="vertical">
<TextView
.....
android:textStyle="bold" />
<TextView
....
android:textColor="@color/secondaryTextColor" />
</LinearLayout>
<ImageView
....
android:src="@mipmap/ic_chevron_right_black_24dp" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
<!--<lay-->
</LinearLayout>
我有适配器...一切正常...完全没问题。
但是,当我尝试实施 onClickLister 来完成单元格视图时 mView
它不起作用。如果我触摸角落(我有填充物的地方),它就可以工作。
我不知道发生了什么。如果我将侦听器设置为单元格中的每个元素,它就可以工作...但不是整个单元格。
下面是我的viewHolder
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
CardView cv;
ImageView icon;
TextView heading;
TextView subheading;
View mView;
public MyViewHolder(View itemView) {
super(itemView);
Log.d(TAG, "setting images and items");
mView=itemView;
cv = (CardView)itemView.findViewById(R.id.home_cv);
icon =(ImageView) itemView.findViewById(R.id.home_rv_icon);
heading =(TextView) itemView.findViewById(R.id.heading);
subheading =(TextView) itemView.findViewById(R.id.subhead);
icon.setOnClickListener(this); // Working
mView.setOnClickListener(this); // NOT working
mView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// NOT Working
return true;
}
});
}
@Override
public void onClick(View view) {
//
Log.d(TAG,"on click");
if(clickListener!=null){
Log.d(TAG,"listner notnull");
clickListener.itemClick(view,getAdapterPosition());
}
}
}
public interface RVClickListener{
//OnClick will work, but for better coding standard i made this listener to call the onclick from the fragment it self..
public void itemClick(View view, int position );
}
经过几个小时的努力,终于修好了。问题来了。
由于我的布局有子视图(布局),onclicklistener 正在被它们捕获。
所以用一个简单的办法解决了。我关闭了所有子视图的可点击并将点击侦听器应用于父视图。
致所有子观点:
android:clickable="false"
从 CardView 布局的每个子视图中删除 longClickable = "true"