我的RecyclerView
有CardView
,我的CardView
包含一个GridView
和其他TextViews
。
public MyCardViewHolder(View view) {
super(view);
this.view=view;
gridView = (GridView) view.findViewById(R.id.gridView);
nickNameTextView =(TextView) view.findViewById(R.id.card_nickname_textView);
...
}
我在OnClickListener
上设置ViewHolder
但是当我点击GridView
时,点击事件永远不会被触发。以下是OnClickListener
的代码:
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
...
});
}
感谢您的帮助。
答案 0 :(得分:1)
通过以下代码,您可以轻松地使用任何元素继续点击事件:)
public class HomeRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
private List<String> mItemList;
RecyclerView mRecyclerView;
Context context;
HomeItemViewHolder mHomeItemholder;
public HomeRecyclerAdapter(List<String> itemList) {
mItemList = itemList;
}
public class HomeItemViewHolder extends RecyclerView.ViewHolder {
ImageView mImage;
public HomeItemViewHolder(View parent) {
super(parent);
mImage = (ImageView) parent.findViewById(R.id.Image);
}
}
public HomeRecyclerAdapter(List<String> itemList, Context context, RecyclerView mRecyclerView) {
this.mItemList = itemList;
this.context = context;
this.mRecyclerView = mRecyclerView;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
final View view = LayoutInflater.from(context).inflate(R.layout.item_home, parent, false);
return new HomeItemViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
mHomeItemholder = (HomeItemholder) viewHolder;
mHomeItemholder.mImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Handle your code
}
});
}
public int getBasicItemCount() {
return mItemList == null ? 0 : mItemList.size();
}
@Override
public int getItemCount() {
return getBasicItemCount(); // header
}
}