我正在开发一个Android应用程序,它从服务器下载图像并在Recycler视图中显示它们。 我在为每个图像启用onclick时遇到问题。
在我的 MyRecyclerViewAdapterClass :
中sol = minimize(lambda w: lasso_var(w, *(train_sig, 5)),
x0=w_equal,
constraints=con1)
在我的主要活动中:
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));
}
当我调用adapter.setListener(this)时出现错误:
我的RecyclerView中的setClickListener无法应用, anonymous.java.lang.Runnable
我已经尝试在runnable中创建一个itemClickListener,就像你在代码中看到的一样,使用它来调用adaptersetClickListener(itemClickListener),但是因为itemClickListener没有初始化而得到错误。
如果我不调用setClickListener方法,则永远不会设置mClickListener并且始终为null。调试时,我注意到点击已注册,但没有任何反应,因为mClickListener为空。
答案 0 :(得分:2)
当我调用adapter.setListener(this)时出现错误:
因为此处this
表示您实施的Runnable
实例,并且您在适配器中传递Runnable
实例而不是MyRecyclerViewAdapter.ItemClickListener
。
我认为您正在尝试将MyRecyclerViewAdapter.ItemClickListener
个实例传递给ViewHolder
。如果我是对的,
为此:
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];
}
}