尝试在Recyclerview中实现ClickListener时出错

时间:2019-05-26 15:35:33

标签: java android android-recyclerview

我很想尝试在DialogFragment的适配器中实现ClickListener。

我在代码行中收到错误:

  

holder.bind(tzmodel.get(position),监听器);

错误说(无法应用ViewHolder中的bind())。似乎与ViewHolder代码不匹配。

有人可以帮忙吗?

timeZoneAdapter的代码:

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

    public interface OnItemClickListener {

        void onItemClick(TimezoneModel item);
    }

    private Context c;
    private List<TimezoneModel> tzmodel;
    private final OnItemClickListener listener;

    public timeZoneAdapter(Context c, List<TimezoneModel> tzModel, OnItemClickListener listener) {

        this.c = c;
        this.tzmodel = tzModel;
        this.listener = listener;
    }

    //INITIALIE TZ
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.timezonemodel, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        holder.bind(tzmodel.get(position), listener); <-- error
    }

    @Override
    public int getItemCount() {

        return tzmodel.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView timeTxt, currenttimeTxt, regionTxt;

        public ViewHolder(View itemView) {

            super(itemView);
            timeTxt= (TextView) itemView.findViewById(R.id.timeTxt);
            currenttimeTxt= (TextView) itemView.findViewById(R.id.currentTimeTxt);
            regionTxt= (TextView) itemView.findViewById(R.id.regionTxt);
        }

        public void bind(ViewHolder viewHolder, int i, final OnItemClickListener listener) {

            viewHolder.timeTxt.setText(tzmodel.get(i).getTime());
            viewHolder.currenttimeTxt.setText(tzmodel.get(i).getCurrentTime());
            viewHolder.regionTxt.setText(tzmodel.get(i).getRegion());

            itemView.setOnClickListener(new View.OnClickListener() {

                @Override public void onClick(View v) {

                    listener.onItemClick((TimezoneModel) tzmodel);
                }
            });
        }
    }
}

1 个答案:

答案 0 :(得分:0)

问题是您放入定义的bind方法中的参数。

您可以通过以下方式在ViewHolder类中定义公共方法绑定:

public void bind(ViewHolder viewHolder, int i, final OnItemClickListener listener){

   //...

}

您可以解决编译错误,将调用更改为:

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.bind(holder,position, listener); 
}

我还与您分享一些更正,因为如果您在ViewHolder类中创建一个公共方法,则无需配方viewHolder实例,因为可以使用此运算符进行访问:


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

    public interface OnItemClickListener {
        void onItemClick(TimezoneModel item);
    }

    private Context c;
    private List<TimezoneModel> tzmodel;
    private final OnItemClickListener listener;

    public TimeZoneAdapter(Context c, List<TimezoneModel> tzModel, OnItemClickListener listener) {
        this.c = c;
        this.tzmodel = tzModel;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.bind(tzmodel.get(position), listener);
    }

    @Override
    public int getItemCount() {

        return tzmodel.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView timeTxt, currenttimeTxt, regionTxt;

        public ViewHolder(View itemView) {

            super(itemView);
            timeTxt= itemView.findViewById(R.id.timeTxt);
            currenttimeTxt= itemView.findViewById(R.id.currentTimeTxt);
            regionTxt= itemView.findViewById(R.id.regionTxt);
        }

        public void bind(TimezoneModel item , final OnItemClickListener listener) {

            this.timeTxt.setText(item.getTime());
            this.currenttimeTxt.setText(item.getCurrentTime());
            this.regionTxt.setText(item.getRegion());

            itemView.setOnClickListener(new View.OnClickListener() {

                @Override public void onClick(View v) {
                    listener.onItemClick((TimezoneModel) tzmodel);
                }
            });
        }
    }
}