我有一个RecyclerView
,其中每个项目都有一个自定义视图。此自定义视图仅包含收藏夹ImageView
。我在我的holder类中设置了onclick,如下所示:
class DataViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private OnItemViewClickListener onItemViewClickListener;
private OnFavoriteImageViewClickListener onFavoriteImageViewClickListener;
private ImageView favoriteImageView;
DataViewHolder(View itemView, OnItemViewClickListener onItemViewClickListener,
OnFavoriteImageViewClickListener onFavoriteImageViewClickListener) {
super(itemView);
this.onItemViewClickListener = onItemViewClickListener;
itemView.setOnClickListener(this);
favoriteImageView = itemView.findViewById(R.id.favorite_image_view);
this.onFavoriteImageViewClickListener = onFavoriteImageViewClickListener;
favoriteImageView.setOnClickListener(this);
}
void bindData(Data data) {
//Bind data to views
}
@Override
public void onClick(View v) {
int position = getAdapterPosition();
Data clickedData = getItem(position);
onItemViewClickListener.onItemViewClick(clickedData);
onFavoriteImageViewClickListener.onFavoriteImageViewClick(clickedData);
}
}
public interface OnItemViewClickListener {
void onItemViewClick(Data data);
}
public interface OnFavoriteImageViewClickListener {
void onFavoriteImageViewClick(Data data);
}
您可以看到,我一次在整个视图上设置了点击监听器,一次在图像视图上设置了监听器。问题是,当我单击ImageView
时,都会触发两个单击事件,一个事件整个视图,另一个事件在ImageView
上。急于停止第一个(在整个视图中)并仅保留ImageView
吗?谢谢!
答案 0 :(得分:1)
您可以使用以下类似内容。在单个界面中使用两个方法定义,并将这些方法分别设置为View和ImageView的clickListeners
。然后在用来设置适配器的Activity中实现这些方法
class DataViewHolder extends RecyclerView.ViewHolder {
private dataViewClickListener dataViewClickListener;
private ImageView favoriteImageView;
DataViewHolder(View itemView, final dataViewClickListener dataViewClickListener) {
super(itemView);
this.dataViewClickListener = dataViewClickListener;
favoriteImageView = itemView.findViewById(R.id.favorite_image_view);
}
void bindData(final Data data) {
// Bind data to views
// then add the listeners
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataViewClickListener.onItemViewClick(data);
}
});
favoriteImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataViewClickListener.onFavoriteImageViewClick(data);
}
});
}
}
public interface dataViewClickListener {
void onItemViewClick(Data data);
void onFavoriteImageViewClick(Data data);
}
您可以从此answer
中找到更多信息答案 1 :(得分:1)
您可以通过评估其View
属性来找出点击了哪个id
,另请参见View.getId():
@Override
public void onClick(View view) {
int position = getAdapterPosition();
Data clickedData = getItem(position);
int id = view.getId();
if (id == R.id.favorite_image_view){
onFavoriteImageViewClickListener.onFavoriteImageViewClick(clickedData)
}
else {
onItemViewClickListener.onItemViewClick(clickedData);
}
}
答案 2 :(得分:0)
使用ID可以确定单击了哪个视图。您可以通过评估其ID属性来确定点击了哪个视图。您还可以使用简单if条件开关来管理操作。
@Override public void onClick(View v) {
Data clickedData = getItem(getAdapterPosition());
switch(v.getId()){
case R.id.favorite_image_view:
onFavoriteImageViewClickListener.onFavoriteImageViewClick(clickedData)
break;
// Add more case view id if needed
default:
onItemViewClickListener.onItemViewClick(clickedData);
}
}