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

这是我的代码:

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();

    }
}


}

我继承了内部类,即StaggeredGridViewViews.OnClickListner,并且调试点未达到StaggeredGridView.OnClick方法。 ADB logs中也不例外。

我在这里做错了什么?

最佳答案

您需要做这个简单的事情,它将开始与您合作

itemView.setOnClickListener(this)

07-27 17:37