如何解决水平RecyclerView中点击项目的问题?

huangapple go评论83阅读模式
英文:

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(),&quot;name&quot;,Toast.LENGTH_SHORT).show();
}
});

recyclerView class:

public class RecyclerViewAdapterContinentsList extends RecyclerView.Adapter&lt;RecyclerViewAdapterContinentsList.ViewHolder&gt;{
private List&lt;String&gt; mId;
private List&lt;String&gt; mName;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
Context context;
RecyclerViewAdapterContinentsList(Context context, List&lt;String&gt; id, List&lt;String&gt; 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,&quot;name&quot;,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,&quot;name&quot;,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);  // &lt;-- 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

huangapple
  • 本文由 发表于 2020年10月20日 19:06:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64443901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定