gpt4 book ai didi

java - 可运行的 setClickListener 不起作用

转载 作者:行者123 更新时间:2023-12-02 02:19:29 25 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,该应用程序从服务器下载图像并将其显示在回收器 View 中。我在为每个图像启用 onclick 时遇到问题。

在我的MyRecyclerViewAdapterClass中:

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView myTextView;
public ImageView myImage;

ViewHolder(View itemView) {
super(itemView);
myTextView = (TextView) itemView.findViewById(R.id.info_text);
myImage = (ImageView) itemView.findViewById(R.id.image_test);
itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}

void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}

public interface ItemClickListener {
void onItemClick(View view, int position);
}

String getItem(int id){
return mData[id];
}
public void onItemClick(View view, int position){
Log.i("TAG","You clicked number" + getItem(position));
}

在我的主要 Activity 中:

public void postImage() {
runOnUiThread(new Runnable() {
@Override
public void run() {

//RecyclerView.
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rvNumbers);
recyclerView.setLayoutManager(new GridLayoutManager(context, numberOfColumns));
MyRecyclerViewAdapter.ItemClickListener itemClickListener;
adapter = new MyRecyclerViewAdapter(context,data,file_payload);

**adapter.setClickListener(this);**
adapter.mData = data;
adapter.PAYLOADS = allPayloads;
adapter.DATES = allDates;
recyclerView.setAdapter(adapter);

}
});}

当我调用adapter.setListener(this)时,出现错误:

setClickListener in my RecyclerView cannot be applied, anonymous.java.lang.Runnable

我尝试在可运行对象中创建一个itemClickListener,如您在代码中看到的那样,使用它来调用adaptersetClickListener(itemClickListener),但是我收到错误,因为itemClickListener未初始化。

如果我不调用 setClickListener 方法,则 mClickListener 永远不会设置并且始终为 null。调试时,我注意到点击已注册,但没有任何反应,因为 mClickListener 为空。

最佳答案

When I call adapter.setListener(this) I get an error:

因为这里 this 表示您实现的 Runnable 实例,并且您传递的是 Runnable 实例,而不是 MyRecyclerViewAdapter.ItemClickListener > 在适配器中。

我认为您正在尝试将 MyRecyclerViewAdapter.ItemClickListener 实例传递给 ViewHolder。如果我是对的,

为此:

  1. 您必须在 MainActivity 中实现 MyRecyclerViewAdapter.ItemClickListener
  2. 将该实例传递给适配器
  3. 并从 Adapter 将该实例传递给 ViewHolder

在主要 Activity 中:

MyRecyclerViewAdapter.ItemClickListener listener = new MyRecyclerViewAdapter.ItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Log.i("TAG","You clicked number" + position);
}
};

adapter = new MyRecyclerViewAdapter(context,data,file_payload);
adapter. setItemClickListener( listener);

在适配器中

public interface ItemClickListener {
void onItemClick(View view, int position);
}

ItemClickListener listener;

public void setItemClickListener(ItemClickListener listener){
this.listener = listener;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
viewHolder.setItemClickListener(listener);
return viewHolder;
}

在 ViewHolder 中:

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView myTextView;
public ImageView myImage;
MyRecyclerViewAdapter.ItemClickListener itemClickListener;

ViewHolder(View itemView) {
super(itemView);
myTextView = (TextView) itemView.findViewById(R.id.info_text);
myImage = (ImageView) itemView.findViewById(R.id.image_test);
itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if (itemClickListener != null) itemClickListener.onItemClick(view, getAdapterPosition());
}


void setItemClickListener(MyRecyclerViewAdapter.ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}


String getItem(int id){
return mData[id];
}
}

关于java - 可运行的 setClickListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48722941/

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