Java 由于容器本身上的“单击侦听器”,无法在“回收器”视图中处理对项目的单击
我有一个关于RecyclerView的活动,列表中的每个项目如下所示。星星应该是可点击的,当用户点击它时,它将被更改为暗星。如果用户单击列表项,则会进入一个新活动,其中会提供与所选列表项对应的更多详细信息: 这是列表项的XMLJava 由于容器本身上的“单击侦听器”,无法在“回收器”视图中处理对项目的单击,java,android,xml,android-recyclerview,Java,Android,Xml,Android Recyclerview,我有一个关于RecyclerView的活动,列表中的每个项目如下所示。星星应该是可点击的,当用户点击它时,它将被更改为暗星。如果用户单击列表项,则会进入一个新活动,其中会提供与所选列表项对应的更多详细信息: 这是列表项的XML <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cont_item_root"
android:layout_width="match_parent"
android:layout_height="85dp"
android:background="@drawable/background_state_drawable"
android:clickable="true"
>
<ImageView
android:id="@+id/im_item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:src="@mipmap/ic_tonality_black_36dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/lbl_item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/im_item_icon"
android:layout_marginLeft="72dp"
android:layout_marginRight="48dp"
android:ellipsize="end"
android:fontFamily="sans-serif"
android:singleLine="true"
android:text="Sois comme l'eau mon ami"
android:textColor="@android:color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/lbl_item_sub_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/lbl_item_text"
android:layout_marginLeft="72dp"
android:layout_marginRight="48dp"
android:ellipsize="end"
android:fontFamily="sans-serif"
android:singleLine="true"
android:text="Mononc' J"
android:textSize="14sp" />
<ImageView
android:id="@+id/im_item_icon_secondary"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="16dp"
android:src="@mipmap/ic_star_border_black_24dp"
android:background="@drawable/background_state_drawable"
/>
</RelativeLayout>
如果,我可以输入的第一部分,即我可以接收listItem click的click事件。但是,当用户单击星号时,它也被视为整个列表项被单击,因为星号位于容器内
我如何单独接收点击星型,以便点击星型不被视为点击列表项
编辑
在列表项的RelativeLayout
中添加行android:genderantfocusability=“blocksDescendants”
修复了该问题。但谁能解释一下它是如何修复的。根据名称,它将阻止子体并吃掉单击事件。但是,行为正好相反。反转if/else语句,并为星形图像添加一个单击侦听器。它应该可以工作,因为如果没有单击星号,您希望触发recyclerview项的侦听器
class DerpHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
private TextView title;
private TextView subTitle;
private ImageView thumbnail;
private ImageView secondaryIcon;
private View container;
public DerpHolder(View itemView) {
super(itemView);
title = (TextView)itemView.findViewById(R.id.lbl_item_text);
subTitle = (TextView)itemView.findViewById(R.id.lbl_item_sub_title);
thumbnail = (ImageView)itemView.findViewById(R.id.im_item_icon);
secondaryIcon = (ImageView)itemView.findViewById(R.id.im_item_icon_secondary);
container = (View)itemView.findViewById(R.id.cont_item_root);
container.setOnClickListener(this);
secondaryIcon.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Log.w("RecyclerView","Item with id : " + v.getId() + " touched");
if(v.getId() == R.id.im_item_icon_secondary)
{
Log.w("RecyclerView","star clicked");
itemClickCallback.onSecondaryIconClick(getAdapterPosition());
}
else
{
Log.w("RecyclerView","list item clicked");
itemClickCallback.onItemClick(getAdapterPosition());
}
}
}
希望这对您有所帮助因为您只想点击星号,您应该为星号设置OnClickListener:
public DerpHolder(View itemView) {
super(itemView);
title = (TextView)itemView.findViewById(R.id.lbl_item_text);
subTitle = (TextView)itemView.findViewById(R.id.lbl_item_sub_title);
thumbnail = (ImageView)itemView.findViewById(R.id.im_item_icon);
secondaryIcon = (ImageView)itemView.findViewById(R.id.im_item_icon_secondary);
secondaryIcon.setOnClickListener(this);
}
您忘记将onClick
listener添加到secondary图标中
试试这个:
class DerpHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
private TextView title;
private TextView subTitle;
private ImageView thumbnail;
private ImageView secondaryIcon;
private View container;
public DerpHolder(View itemView) {
super(itemView);
title = (TextView)itemView.findViewById(R.id.lbl_item_text);
subTitle = (TextView)itemView.findViewById(R.id.lbl_item_sub_title);
thumbnail = (ImageView)itemView.findViewById(R.id.im_item_icon);
secondaryIcon = (ImageView)itemView.findViewById(R.id.im_item_icon_secondary);
container = (View)itemView.findViewById(R.id.cont_item_root);
container.setOnClickListener(this);
secondaryIcon.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId() == secondaryIcon.getId())
{
Log.w("RecyclerView","star clicked");
itemClickCallback.onSecondaryIconClick(getAdapterPosition());
}
else
{
Log.w("RecyclerView","list item clicked");
itemClickCallback.onItemClick(getAdapterPosition());
}
}
}
希望这会有所帮助~您也可以在recycler adapter类中为以下行的特定视图声明onclick项
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.linRootMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Do your stuff here.
}
});
holder.imgStart.setOnClickListner(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Do your stuff here.
}
});
}
我希望这将对您有所帮助。可能是重复的或更好的。。。这仅在实际将单击侦听器添加到image@cricket_007谢谢你指出这一点!我以为是加上去的
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.linRootMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Do your stuff here.
}
});
holder.imgStart.setOnClickListner(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Do your stuff here.
}
});
}