gpt4 book ai didi

android - OnItemClickListener 在 recyclerview 中不起作用?

转载 作者:行者123 更新时间:2023-11-29 15:08:48 27 4
gpt4 key购买 nike

您好,我正在制作一个简单的回收 View 。我还想添加 OnItemClickListener。所以我在 ViewHolderClass 中实现了 Adapter.Onitemclicklistener。但它不起作用。

我想我以错误的方式添加了 OnItemClicklistener,但有人可以给我解决方案吗。

这是我的适配器

package com.example.niyamat.golfscoreboard;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
* Created by Niyamat on 12/3/2015.
*/
public class ScoreBoardAdapter extends RecyclerView.Adapter<ScoreBoardAdapter.ScoreBoardViewHolder> {
private Context mContext;
private Hole[] mHoles;

public ScoreBoardAdapter(Context context, Hole[] holes) {
mContext = context;
mHoles = holes;
}

@Override
public ScoreBoardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.scoreboard_layout, parent, false);
return new ScoreBoardViewHolder(view);
}

@Override
public void onBindViewHolder(ScoreBoardViewHolder holder, int position) {
holder.mHoleName.setText(mHoles[position].getHoleName());


}

@Override
public int getItemCount() {
return mHoles.length;
}

public class ScoreBoardViewHolder extends RecyclerView.ViewHolder implements AdapterView.OnItemClickListener{
public TextView mHoleName;
public TextView mResultLabel;
public ScoreBoardViewHolder(View itemView) {
super(itemView);
mHoleName = (TextView) itemView.findViewById(R.id.holeName);
mResultLabel = (TextView) itemView.findViewById(R.id.resultNumber);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 3) {
Toast.makeText(mContext, "Yey we got it",Toast.LENGTH_LONG).show();
}
}
}
}

最佳答案

myViewHolder.viewID.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("NIC", "Clicked on View::");
}
});

最简单的方法

关于android - OnItemClickListener 在 recyclerview 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34102333/

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