[英]On Item Click Listener for RecyclerView - How can I use the position of an item in a recycler view to smoothscroll to another listview?
[英]Unable to handle click on an item in Recycler view due to click listener on container itself
我有一个RecyclerView活动,列表中的每个项目如下所示。 该星形应该是可单击的,并且当用户单击它时,应该将其更改为暗星形。 如果用户单击列表项,它将进入一个新的活动,其中提供了与所选列表项相对应的更多详细信息。 :
这是列表项的XML。
<?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>
Adapter中的嵌套类为我处理click事件。
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);
}
@Override
public void onClick(View v) {
Log.w("RecyclerView","Item with id : " + v.getId() + " touched");
if(v.getId() == R.id.cont_item_root)
{
Log.w("RecyclerView","list item clicked");
itemClickCallback.onItemClick(getAdapterPosition());
}
else
{
Log.w("RecyclerView","star clicked");
itemClickCallback.onSecondaryIconClick(getAdapterPosition()); //not able to come here
}
}
}
我可以输入if
的第一部分,即我可以接收listItem click的click事件。 但是,当用户单击星号时,由于星号位于容器内,因此也就好像单击了整个列表项一样。
如何分别获得对星号的点击,以使星号的点击不被视为点击列表项?
编辑
在列表项的RelativeLayout
中添加android:descendantFocusability="blocksDescendants"
行解决了该问题。 但是任何人都可以解释它如何解决它。 顾名思义,它有望阻止子孙后代并吞下click事件。 但是,行为是相反的。
反转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
侦听器添加到secondaryIcon
。
尝试这个:
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());
}
}
}
希望这会有所帮助〜
您也可以为行的特定视图声明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.
}
});
}
我希望这能帮到您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.