来自runnable的setClickListener不起作用
setClickListener from runnable not working
我正在开发一个 Android 应用程序,它从服务器下载图像并将它们显示在 Recycler 视图中。
我在为每个图像启用 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));
}
在我的 main 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 并且始终为空。调试时,我注意到点击已注册但没有任何反应,因为 mClickListener 为空。
When I call adapter.setListener(this) I get an error:
因为这里 this
表示您实现的 Runnable
实例,并且您正在传递一个 Runnable
实例而不是适配器中的 MyRecyclerViewAdapter.ItemClickListener
。
我认为您正试图将 MyRecyclerViewAdapter.ItemClickListener
实例传递给 ViewHolder
。如果我是正确的,
为此:
- 您必须在 MainActivity
中实现 MyRecyclerViewAdapter.ItemClickListener
- 将该实例传递给
Adapter
。
- 然后从
Adapter
将该实例传递给 ViewHolder
。
在 MainActivity 中:
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];
}
}
我正在开发一个 Android 应用程序,它从服务器下载图像并将它们显示在 Recycler 视图中。 我在为每个图像启用 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));
}
在我的 main 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 并且始终为空。调试时,我注意到点击已注册但没有任何反应,因为 mClickListener 为空。
When I call adapter.setListener(this) I get an error:
因为这里 this
表示您实现的 Runnable
实例,并且您正在传递一个 Runnable
实例而不是适配器中的 MyRecyclerViewAdapter.ItemClickListener
。
我认为您正试图将 MyRecyclerViewAdapter.ItemClickListener
实例传递给 ViewHolder
。如果我是正确的,
为此:
- 您必须在 MainActivity 中实现
- 将该实例传递给
Adapter
。 - 然后从
Adapter
将该实例传递给ViewHolder
。
MyRecyclerViewAdapter.ItemClickListener
在 MainActivity 中:
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];
}
}