How can I replace ButterKnife with ViewBinding on RecyclerView Adapter

吃可爱长大的小学妹 提交于 2020-05-14 12:12:06

问题


I would like to use the new View Binding instead of ButterKnife in my RecyclerView Adapter class?

This is my class :

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.gallosalocin.mareu.R;
import com.gallosalocin.mareu.model.Meeting;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

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

private List<Meeting> meetingsList;
private OnItemClickListener onItemClickListener;

MeetingRecyclerViewAdapter(List<Meeting> meetings, OnItemClickListener onItemClickListener) {
    this.meetingsList = meetings;
    this.onItemClickListener = onItemClickListener;
}

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

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Meeting meeting = meetingsList.get(position);
    String descriptionItems = String.format("%1$s - %2$s - %3$s", meeting.getTopic(), meeting.getTime(), meeting.getRoom());

    holder.roomColor.setImageResource(meeting.getRoomColor());
    holder.description.setText(descriptionItems);
    holder.email.setText(meeting.getEmail());

}

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


public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    @BindView(R.id.iv_cardview_image)
    ImageView roomColor;
    @BindView(R.id.tv_cardview_description)
    TextView description;
    @BindView(R.id.tv_cardview_email)
    TextView email;
    @BindView(R.id.iv_cardview_delete_btn)
    ImageView deleteImage;
    @BindView(R.id.cardview_meeting)
    CardView cardView;

    OnItemClickListener onItemClickListener;

    ViewHolder(@NonNull View itemView, OnItemClickListener onItemClickListener) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        this.onItemClickListener = onItemClickListener;

        deleteImage.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.iv_cardview_delete_btn) {
            onItemClickListener.onDeleteClick(getAdapterPosition());
        }
    }
}

    public interface OnItemClickListener {
        void onDeleteClick(int position);
    }
}

I try this but I have this issue.

The_issue

In fact, I would like to remove all the code lines with @BindView to make the code more readable. Sorry for my english if I make mistakes.


回答1:


Your ViewHolder expects View instance to be passed. So, just pass binding.getRoot() instead of binding into the constructor, and it will work.

Like this:

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    // get your binding here: itemBinding = ...
    // and then:
    return new ViewHolder(itemBinding.getRoot(), onItemClickListener);
}


来源:https://stackoverflow.com/questions/60756997/how-can-i-replace-butterknife-with-viewbinding-on-recyclerview-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!