我在RecyclerView上实现onItemClickListener()。但建议通过实现View.OnClickListener
在viewHolder上执行此操作这是我的代码
public class FeedAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
..........
............
private OnItemClickListener onFeedItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
.........
if (position % 2 == 0) {
.........
} else {
..........
}
holder.btnComments.setOnClickListener(this);
holder.btnComments.setTag(position);
holder.btnMore.setOnClickListener(this);
holder.btnMore.setTag(position);
}
.........
public static class CellItemViewHolder extends RecyclerView.ViewHolder {
@InjectView(R.id.btnComments)
ImageButton btnComments;
@InjectView(R.id.btnMore)
ImageButton btnMore;
public CellItemViewHolder(View view) {
super(view);
ButterKnife.inject(this, view);
}
}
public interface OnItemClickListener {
public void onCommentsClick(View v, int position);
public void onMoreClick(View v, int position);
}
我无法以正确的方式做到这一点。因为我得到以太无效或无响应我的点击 有什么想法吗?
答案 0 :(得分:1)
不明白你的意思。这可以解决你的问题吗?
public class FeedAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements CellItemViewHolder.OnItemClickListener{
private Context context;
public FeedAdapter(Context context) {
this.context = context;
}
@Override
public int getItemCount() {
return 0;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
CellItemViewHolder messageRecycleViewHolder = (CellItemViewHolder)viewHolder;
messageRecycleViewHolder.setOnItemClickListener(this);
}
@Override
public CellItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.main_item, viewGroup, false);
return new CellItemViewHolder(view);
}
@Override
public void onCommentsClick(View v, int position) {
Toast.makeText(getApplicationContext(), "onCommentsClick,position:" + position, Toast.LENGTH_LONG).show();
}
@Override
public void onMoreClick(View v, int position) {
Toast.makeText(getApplicationContext(), "onMoreClick,position:" + position, Toast.LENGTH_LONG).show();
}
}
public static class CellItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
View viewLayout;
Button moreBtn;
Button commentBtn;
OnItemClickListener onItemClickListener;
int position = 1;
public CellItemViewHolder(View itemView) {
super(itemView);
viewLayout = itemView;
init();
}
private void init() {
moreBtn = (Button)viewLayout.findViewById(R.id.more_btn);
commentBtn = (Button)viewLayout.findViewById(R.id.comment_btn);
moreBtn.setOnClickListener(this);
commentBtn.setOnClickListener(this);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public int getMyPosition() {
return position;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.more_btn:
if(onItemClickListener != null) {
onItemClickListener.onMoreClick(v, getMyPosition());
}
break;
case R.id.comment_btn:
if(onItemClickListener != null) {
onItemClickListener.onCommentsClick(v, getMyPosition());
}
break;
default:
break;
}
}
public interface OnItemClickListener {
public void onCommentsClick(View v, int position);
public void onMoreClick(View v, int position);
}
}