嗨,我正在制作一个简单的recyclerview。我还想添加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/

10-10 23:32