我确实在做错事。我有一个ReclyclerView
和每个单元都有一个XML home_cells.xmlAndroid onTouchListener无法正常工作,只能在Padding区域工作
<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);
}
每个孩子视图在'mView'你想实现an' onClickListener'或删除longClickable = “真”一个onTouchListener?另外,你确定它不起作用吗?你在那里打印任何陈述?另外,什么是'clickListener'变量?你在哪里初始化? –