我正在使用一个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));
}
在我的主要活动中:
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)时出现错误:
我的RecyclerView中的setClickListener无法应用,
onymous.java.lang.Runnable
我尝试在代码中看到在runnable中创建一个itemClickListener,并将其用于调用adaptersetClickListener(itemClickListener),但是由于未初始化itemClickListener,因此出现错误。
如果我不调用setClickListener方法,则不会设置mClickListener,并且始终为null。调试时,我注意到该点击已注册,但因为mClickListener为null而没有任何反应。
最佳答案
当我调用adapter.setListener(this)时出现错误:
因为这里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];
}
}