如何在 RecyclerView Adapter 中用视图绑定替换 Butter Knife

How can I replace ButterKnife with ViewBinding on RecyclerView Adapter

我想在 RecyclerView Adapter class?

中使用新的 View Binding 而不是 ButterKnife

这是我的 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);
    }
}

我尝试 但我遇到了这个问题。

The_issue

事实上,我想删除所有带有@BindView的代码行,使代码更易读。如果我有错误,请原谅我的英语。

您的 ViewHolder 期望 View 实例通过。所以,只需将 binding.getRoot() 而不是 binding 传递给构造函数,它就会起作用。

像这样:

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

这可能对您有所帮助-

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

    OnItemClickListener onItemClickListener;
    private CardviewMain binding; //binding class object

    ViewHolder(@NonNull CardviewMain binding, OnItemClickListener onItemClickListener) {
        super(binding.getRoot());
        this.binding = binding;
        this.onItemClickListener = onItemClickListener;

        this.binding.ivCardviewDeleteBtn.setOnClickListener(this);
    } 
}

并在 onCreateViewHolder 中:

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

    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    CardviewMain binding = CardviewMain.inflate(layoutInflater, parent, false);
    return new ViewHolder(binding, onItemClickListener);
}

对于那些想要完整解决方案的人:

class YourAdapter() : ListAdapter<YourCategory, YourAdapter.YourViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): YourViewHolder {
    val binding = YourItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
    return YourViewHolder(binding)
}

override fun onBindViewHolder(holder: YourViewHolder, position: Int) {
    // your stuff
}

class YourViewHolder(private val binding: YourItemBinding) : RecyclerView.ViewHolder(binding.root) {

        binding.apply {

            // your stuff
        }
    }
}