为什么从未使用过onBindViewHolder方法?

时间:2016-03-01 17:39:42

标签: android android-recyclerview android-adapter

在我更改onBindViewHolder的参数后,它变为灰色,而不是被调用,为什么?这是我的适配器:

public class Myadapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
       private Context context;
       private List<String> lists;
       private OnItemClickListener onItemClickListener;


    public Myadapter(Context context, List<String> lists) {
       this.context = context;
       this.lists = lists;
    }

   public interface OnItemClickListener {
       void onItemClick(View view, int postion);
       void onItemLongClick(View view, int postion);
   }

   public void setOnItemClickListener(OnItemClickListener listener) {
       this.onItemClickListener = listener;
   }

   @Override
   public void onBindViewHolder(final myViewHolder holder, int position) {
       holder.textView.setText(lists.get(position));

       if (onItemClickListener != null) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int postion = holder.getLayoutPosition();//得到当前点击item的位置postion
                onItemClickListener.onItemClick(holder.itemView, postion);
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int postion = holder.getLayoutPosition();
                onItemClickListener.onItemLongClick(holder.itemView, postion);
                return true;
            }
        });
    }

}

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

@Override
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
    myViewHolder viewHolder = new myViewHolder(itemView);

    return viewHolder;
}


  public class myViewHolder extends RecyclerView.ViewHolder{
       TextView textView;

       public myViewHolder(View itemView){
           super(itemView);
           textView =(TextView)itemView.findViewById(R.id.text);

       }
   }
}

它说Myadapter必须在&#34; Adapter&#34;

中被声明为实现抽象方法onBindViewHolder(VH,int)的摘要

1 个答案:

答案 0 :(得分:1)

由于您尝试对此适配器使用自己的ViewHolder,因此在扩展RecyclerView.Adapter时需要将其用作类型。

变化:

public class Myadapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

要:

public class Myadapter extends RecyclerView.Adapter<myViewHolder> {