gpt4 book ai didi

java - 如何向按钮的recycleview添加onItemclicklistener方法

转载 作者:行者123 更新时间:2023-12-01 18:47:09 26 4
gpt4 key购买 nike

我想创建一个 onItemclicklistener 方法来回收按钮的 View 以及每个按钮对 Activity 的 Intent 但 Activity 有 Textview (每个按钮都有不同的 Textview )

public class RecAdapter extends RecyclerView.Adapter<RecAdapter.ViewHolder>  {

public class ViewHolder extends RecyclerView.ViewHolder {

Button button;

public ViewHolder(View itemView) {
super(itemView);
button = itemView.findViewById(R.id.buttonn);
}
}

private Context context;
private List<items> items;

public RecAdapter(Context c, List<items> itemList) {
this.context = c;
items = itemList;
}

@Override
public RecAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.buttonss, parent, false);

return new ViewHolder(v);
}

@Override
public void onBindViewHolder(RecAdapter.ViewHolder holder, int position) {
items p = items.get(position);
holder.button.setText(p.getName());
}

@Override
public int getItemCount() {
return items.size();
}
}`

最佳答案

holderitemView 上添加 OnClickListener,如下所示:

@Override
public void onBindViewHolder(RecAdapter.ViewHolder holder, int position) {

items p = items.get(position);
holder.button.setText(p.getName());

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

// Do here
}
});
}

关于java - 如何向按钮的recycleview添加onItemclicklistener方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59809125/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com