在我的RecyclerView.Adapter上:
@Override
public void onBindViewHolder(WorkoutViewHolder viewHolder, int i) {
....
viewHolder.mCreatorAvatar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "AVATAR", Toast.LENGTH_SHORT).show();
}
});
在包含RecyclerView的片段父级:
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(BrowseSessionsFragment.this.getActivity(),
mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
// do whatever
Toast.makeText(BrowseSessionsFragment.this.getActivity(), "ITEM" + position,
Toast.LENGTH_SHORT).show();
}
问题是:当我点击ImageView时,有2条消息: ITEMx然后AVATAR;在这种情况下如何触发AVATAR消息?
谢谢你们!
答案 0 :(得分:0)
如果您需要OnRetemClickListener for RecyclerView,您必须创建自己的界面,例如
public interface OnItemClick {
void onItemClick(int position);
}
然后在您的activity / fragment中实现它并将其设置为适配器。 在适配器集OnClickListener for element中,您需要在ViewHolder中将列表项的位置提供给您的接口,例如
static class ViewHolder extends RecyclerView.ViewHolder {
ImageView mCreatorAvatar;
public ViewHolder(View itemView, final OnItemClick onItemClick) {
super(itemView);
//...some code...
mCreatorAvatar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onItemClick != null){
onItemClick.onItemClick(getAdapterPosition());
}
}
}
}
在实现接口OnItemClick
的activity / fragment中public void onItemClick(int position) {
//do smth
}
如果你想接收完全列表项上的点击,只需使用类似的方式设置第二个OnItemClick到ViewHolder中的itemView