public AdapterView.OnItemClickListener mListener; // // public interface OnItemClickListener { // void onItemClick(int position); // } // public void setOnItemClickListener(OnItemClickListener listener) { // mListener = (AdapterView.OnItemClickListener) listener; // } public MyAdapter(List<DataBean> mlist, Context mcontext) { this.mlist = mlist; this.mcontext = mcontext; } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View inflate = LayoutInflater.from(mcontext).inflate(R.layout.item, parent, false); ViewHolder holder = new ViewHolder(inflate); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { if (TextUtils.isEmpty(mlist.get(position).getThumbnail_pic_s())&&TextUtils.isEmpty(mlist.get(position).getThumbnail_pic_s03())){ Picasso.get().load(mlist.get(position).getThumbnail_pic_s02()).into(holder.img1); holder.img.setVisibility(View.GONE); holder.img2.setVisibility(View.GONE); holder.tv.setText(mlist.get(position).getTitle()); holder.tv1.setText(mlist.get(position).getAuthor_name()); holder.tv2.setText(mlist.get(position).getCategory()); }else if (TextUtils.isEmpty(mlist.get(position).getThumbnail_pic_s02())) { Picasso.get().load(mlist.get(position).getThumbnail_pic_s()).into(holder.img); Picasso.get().load(mlist.get(position).getThumbnail_pic_s03()).into(holder.img2); holder.img1.setVisibility(View.GONE); holder.tv.setText(mlist.get(position).getTitle()); holder.tv1.setText(mlist.get(position).getAuthor_name()); holder.tv2.setText(mlist.get(position).getCategory()); }else { Picasso.get().load(mlist.get(position).getThumbnail_pic_s()).into(holder.img); Picasso.get().load(mlist.get(position).getThumbnail_pic_s02()).into(holder.img1); Picasso.get().load(mlist.get(position).getThumbnail_pic_s03()).into(holder.img2); holder.tv.setText(mlist.get(position).getTitle()); holder.tv1.setText(mlist.get(position).getAuthor_name()); holder.tv2.setText(mlist.get(position).getCategory()); } holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mListener != null) { mListener.onItemClick(position); } } });position报红
时间: 2024-04-22 22:26:20 浏览: 196
在您的代码中,有几个问题需要解决。
首先,您定义的 `mListener` 应该是 `AdapterView.OnItemClickListener` 类型,而不是 `MyAdapter.OnItemClickListener` 类型。所以请将以下行代码从注释中移动到适当的位置:
```java
public AdapterView.OnItemClickListener mListener;
```
然后,您不需要为点击事件定义一个接口,因为 `AdapterView` 已经提供了 `OnItemClickListener` 接口。只需将点击事件的监听器设置为 `mListener` 即可。
最后,关于 `position` 报红的问题,这是因为在匿名内部类中使用局部变量时,需要将其声明为 `final`。在 Java 8 及以上版本中,可以不用声明为 `final`,但仍然需要保证变量不可变。您可以将 `position` 声明为 `final`,或者使用 `holder.getAdapterPosition()` 方法获取当前项的位置。
下面是修改后的代码片段:
```java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<DataBean> mlist;
private Context mcontext;
public AdapterView.OnItemClickListener mListener;
public MyAdapter(List<DataBean> mlist, Context mcontext) {
this.mlist = mlist;
this.mcontext = mcontext;
}
// ...
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// ...
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onItemClick(holder.getAdapterPosition());
}
}
});
}
}
```
请注意,为了避免引发内存泄漏,确保在不需要时及时清除对 `mListener` 的引用。
阅读全文