gpt4 book ai didi

java - RecyclerView onItemClickListener

转载 作者:行者123 更新时间:2023-11-30 10:32:21 29 4
gpt4 key购买 nike

我正在寻求构建一个程序,让用户从文本列表中点击,这会将他们带到另一个 Activity 。

我知道 RecyclerView 没有点击监听器,就像 ListView 一样,但是经过几次尝试,我无法让它工作。

下面是我尝试实现的代码:

MyChatRecyclerViewAdapter.java

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

private final List<Chat> mValues;
private final OnItemClickListener listener;
//private final OnListFragmentInteractionListener mListener;

public MyChatRecyclerViewAdapter(List<Chat> items, OnItemClickListener listener) {
this.mValues = items;
//mListener = listener;
this.listener = listener;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_chat, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(mValues.get(position).id);
holder.bind(mValues.get(position), listener);
holder.mContentView.setText(mValues.get(position).content);

holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null != listener) {
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
listener.onListFragmentInteraction(holder.mItem);
}
}
});
}

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


public interface OnItemClickListener {
//public void onItemClick(View view , int position);
void onItemClick(Chat item);
}


public class ViewHolder extends RecyclerView.ViewHolder{
public final View mView;
public final TextView mIdView;
public final TextView mContentView;
public Chat mItem;
OnItemClickListener mItemClickListener;

/*@Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(v, getPosition());
}
}*/

public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
mIdView = (TextView) itemView.findViewById(R.id.id);
mContentView = (TextView) itemView.findViewById(R.id.content);
}

public void bind(final Chat item, final OnItemClickListener listener){

itemView.setOnClickListener(new View.OnClickListener(){
@override
public void onClick(View v){
listener.onItemClick(item);
}
});
}

@Override
public String toString() {
return super.toString() + " '" + mContentView.getText() + "'";
}
}
}

这是我尝试遵循的教程:https://antonioleiva.com/recyclerview-listener/

最佳答案

RecyclerView 中, View 被回收以提高性能。您不应将 OnClickListener 放在 onBindViewHolder 中,因为位置并不总是正确的。尝试在 ViewHolder 的构造函数中移动 OnClickListener 并通过调用 getAdapterPosition() 获取当前位置:

public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
mIdView = (TextView) itemView.findViewById(R.id.id);
mContentView = (TextView) itemView.findViewById(R.id.content);

mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int position = getAdapterPosition();
if (null != listener) {
listener.onListFragmentInteraction(mValues.get(position));
}
}
});
}

关于java - RecyclerView onItemClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42721571/

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