英文:
How can I solve a problem with click on item in horizontal RecyclerView?
问题
我有一个水平的可循环视图。其中有一些按钮。我将在下面提供我的代码。问题是,当我点击按钮时,什么也不会发生。它只是认为我没有点击它。我还有另一个可循环视图,但是是垂直的,代码非常相似,但第二个可循环视图是有效的。我已经试图找错误很长时间了。我真的希望您能帮助我。谢谢。
在我的主要代码中:
adapter1 = new RecyclerViewAdapterContinentsList(this, idList, continentList);
recyclerViewForContinents.setAdapter(adapter1);
adapter1.setClickListener(new RecyclerViewAdapterContinentsList.ItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(getApplicationContext(), "name", Toast.LENGTH_SHORT).show();
}
});
可循环视图类:
public class RecyclerViewAdapterContinentsList extends RecyclerView.Adapter<RecyclerViewAdapterContinentsList.ViewHolder>{
private List<String> mId;
private List<String> mName;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
Context context;
RecyclerViewAdapterContinentsList(Context context, List<String> id, List<String> name) {
this.mInflater = LayoutInflater.from(context);
this.mId = id;
this.mName = name;
this.context = context;
}
// 当需要时从 XML 中膨胀行布局
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.activity_recycler_view_adapter_countinent_list, parent, false);
return new ViewHolder(view);
}
// 将数据绑定到每行中的 TextView
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
String id = mId.get(position);
String name = mName.get(position);
holder.continentBtn.setText(name);
}
// 总行数
@Override
public int getItemCount() {
return mName.size();
}
// 将视图存储和回收为它们滚动出屏幕
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
Button continentBtn;
ViewHolder(View itemView) {
super(itemView);
continentBtn = itemView.findViewById(R.id.continentBtn);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null) {
Toast.makeText(context, "name", Toast.LENGTH_SHORT).show();
mClickListener.onItemClick(view, getAdapterPosition());
}
}
}
// 用于获取点击位置的数据的便捷方法
String getItem(int id) {
return mId.get(id);
}
void setClickListener(RecyclerViewAdapterContinentsList.ItemClickListener itemClickListener) {
Toast.makeText(context, "name", Toast.LENGTH_SHORT).show();
this.mClickListener = itemClickListener;
}
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
英文:
I have a horizontal recycler view. There are some buttons. I will provide my code below. So the problem is that when I click on a button, does not happen anything. It just thinks that I did not click to it. I have another recycler view, but vertical and the code is pretty similar, but the second one works. I am traying to find a mistake for ages. I really hope you can help me. Thank you.
in my main:
adapter1 = new RecyclerViewAdapterContinentsList(this, idList,continentList);
recyclerViewForContinents.setAdapter(adapter1);
adapter1.setClickListener(new RecyclerViewAdapterContinentsList.ItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(getApplicationContext(),"name",Toast.LENGTH_SHORT).show();
}
});
recyclerView class:
public class RecyclerViewAdapterContinentsList extends RecyclerView.Adapter<RecyclerViewAdapterContinentsList.ViewHolder>{
private List<String> mId;
private List<String> mName;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
Context context;
RecyclerViewAdapterContinentsList(Context context, List<String> id, List<String> name) {
this.mInflater = LayoutInflater.from(context);
this.mId = id;
this.mName = name;
this.context = context;
}
// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.activity_recycler_view_adapter_countinent_list, parent, false);
return new ViewHolder(view);
}
// binds the data to the TextView in each row
@Override
public void onBindViewHolder(ViewHolder holder,final int position) {
String id = mId.get(position);
String name = mName.get(position);
holder.continentBtn.setText(name);
}
// total number of rows
@Override
public int getItemCount() {
return mName.size();
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
Button continentBtn;
ViewHolder(View itemView) {
super(itemView);
continentBtn = itemView.findViewById(R.id.continentBtn);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null) {
Toast.makeText(context,"name",Toast.LENGTH_SHORT).show();
mClickListener.onItemClick(view, getAdapterPosition());}
}
}
// convenience method for getting data at click position
String getItem(int id) {
return mId.get(id);
}
void setClickListener(RecyclerViewAdapterContinentsList.ItemClickListener itemClickListener) {
Toast.makeText(context,"name",Toast.LENGTH_SHORT).show();
this.mClickListener = itemClickListener;
}
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
答案1
得分: 1
将您的 ViewHolder 代码更改,将 OnClickListener 附加到按钮而不是 itemView。
ViewHolder(View itemView)
{
super(itemView);
continentBtn = itemView.findViewById(R.id.continentBtn);
continentBtn.setOnClickListener(this); // <-- 按钮而不是 itemView
}
在您的 ViewHolder 中,itemView 是您布局 R.layout.activity_recycler_view_adapter_countinent_list
的根视图,RecyclerView 可以使用 setOnItemClickListener
监听整行的点击事件。
英文:
Change your ViewHolder code to attach the OnClickListener to the button and not the itemView.
ViewHolder(View itemView)
{
super(itemView);
continentBtn = itemView.findViewById(R.id.continentBtn);
continentBtn.setOnClickListener(this); // <-- button not itemView
}
In your ViewHolder, itemView is the root viewGroup of your layout R.layout.activity_recycler_view_adapter_countinent_list
, a RecyclerView can listen to click events on the entire row using setOnItemClickListener
.
答案2
得分: 1
这很简单,
首先,在你的按钮视图中添加一个id,然后在你的适配器的onBindViewHolder
方法中,
为视图设置监听器,并在监听器中执行任何事件。
@Override
public void onBindViewHolder(@NonNull final recycler_item_ads.ViewHolder holder, final int position) {
holder.installnow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//在这里打印toast
}
}
});
这里的holder.installnow
是我布局文件中的按钮。
英文:
It is Easy ,
First you give id in your button view Than in your Adapter in onBindViewHolder
set listner in view and perform any event in listner.
@Override
public void onBindViewHolder(@NonNull final recycler_item_ads.ViewHolder holder, final int position) {
holder.installnow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//here print toast
}
}
});
holfer.install now is my button in layout file
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论