gpt4 book ai didi

java - 如何在Android回收器 View 项目上添加onClick事件?

转载 作者:行者123 更新时间:2023-12-01 11:02:30 24 4
gpt4 key购买 nike

Android Recycler View OnClick 事件在代码中不起作用。我可以看到 Recycler View 网格,但是当我单击一项时,什么也没有发生。

这是我的代码:

public class StaggeredGridAdapter extends RecyclerView.Adapter<StaggeredGridAdapter.StaggeredGridView> {

private Context context;
private List<Warehouse> warehouses = new ArrayList<Warehouse>();
int size;
public StaggeredGridAdapter(Context context) {
this.context = context;
}

public void addItems(List<Warehouse> response) {
size = response.size();
warehouses = response;
}

@Override
public StaggeredGridView onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
StaggeredGridView staggeredGridView = new StaggeredGridView(layoutView);
return staggeredGridView;
}

@Override
public void onBindViewHolder(StaggeredGridView holder, int position) {
holder.textView.setText(warehouses.get(position).getFace());

}

@Override
public int getItemCount() {
return size;
}

class StaggeredGridView extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView textView;

public StaggeredGridView(View itemView) {
super(itemView);

textView = (TextView) itemView.findViewById(R.id.img_name);

}

@Override
public void onClick(View v) {
Toast.makeText(context, "clicked", Toast.LENGTH_SHORT).show();

}
}

}

我继承了内部类,即带有 Views.OnClickListnerStaggeredGridView ,并且调试点未到达 StaggeredGridView.OnClick 方法。 ADB日志中没有异常。

我在这里做错了什么?

最佳答案

你需要做这个简单的事情,它就会开始与你合作

itemView.setOnClickListener(this)

关于java - 如何在Android回收器 View 项目上添加onClick事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33228673/

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