Android RecyclerView adapter class View Binding 的正确方式是什么?

What is the right way of Android View Binding in the RecyclerView adapter class?

这是我在 RecycleView 适配器 class 中使用的代码。我不知道这是不是使用 View Binding 的正确方法。如果您有更好的解决方案,请回答我。谢谢。

@Override
public CategoryAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.common_circle_image, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull CategoryAdapter.MyViewHolder holder, final int position) {
    holder.binding.img.setBackgroundResource(addAdapterData.get(position).getItemUrl());
    holder.binding.txt.setText(addAdapterData.get(position).getItemName());
}

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

public class MyViewHolder extends RecyclerView.ViewHolder {

    CommonCircleImageBinding binding;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        binding = CommonCircleImageBinding.bind(itemView);
        binding.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                commonItemClick.onItemClick(getAdapterPosition(),"");
            }
        });
    }
}

此外,我想知道在同一个class中使用R.layout.layout_nameViewBinding是否正确。

您可以通过 CommonCircleImageBinding.inflate(LayoutInflater.from(parent.getContext()))

onCreateViewHolder 中直接创建 CommonCircleImageBinding

然后传给MyViewHolder

您需要做的是将生成的绑定 class 对象传递给 holder class 构造函数。在您的示例中,您有 common_circle_image XML 文件用于 RecyclerView 项目并且生成的 class 是 CommonCircleImageBinding 所以像这样您使用 onCreateViewHolder将生成的绑定 class 传递给 ViewHolder class

@NonNull
@Override
public CategoryAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    CommonCircleImageBinding itemBinding = CommonCircleImageBinding .inflate(LayoutInflater.from(parent.getContext()), parent, false);
    return new MyViewHolder(itemBinding);
}

并像这样使用持有人 class,这样您就可以在 onBindViewHolder

中使用这些字段
static class MyViewHolder extends RecyclerView.ViewHolder {
    private TextView txt;
    private ImageView img; 

    MyViewHolder(CommonCircleImageBinding itemBinding) {
        super(itemBinding.getRoot());
        img = itemBinding.img ;
        txt = itemBinding.txt ;
    }
}

这里是java中的完整视图绑定回收器视图代码,你可以这样做:

package com.jbws.myviewbindingdemo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.jbws.myviewbindingdemo.databinding.RowXmlViewBinding;
import com.jbws.myviewbindingdemo.pojo.ModelObject;

import java.util.ArrayList;

public class RecyclerViewListAdapter extends RecyclerView.Adapter<RecyclerViewListAdapter.ViewHolder> {
    public ArrayList<ModelObject> modelObjectArrayList;

    public RecyclerViewListAdapter(ArrayList<ModelObject> modelObjectArrayList) {
        this.modelObjectArrayList = modelObjectArrayList;
    }

    @NonNull
    @Override
    public RecyclerViewListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(RowXmlViewBinding.inflate(LayoutInflater.from(parent.getContext()),
                parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewListAdapter.ViewHolder holder, final int position) {
        ModelObject modelObject = modelObjectArrayList.get(position);
        holder.rowXmlViewBinding.txtObjectName.setText(modelObject.getFullName());
        holder.rowXmlViewBinding.btnUpdateName.setOnClickListener(view -> {
         Log.i("LOG_TAG", "Full Name: " + modelObject.getFullName);
        });
    }

    @Override
    public int getItemCount() {
        return modelObjectArrayList == null ? 0 :
                modelObjectArrayList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private RowXmlViewBinding rowXmlViewBinding;

        public ViewHolder(RowXmlViewBinding rowXmlViewBinding) {
            super(rowXmlViewBinding.getRoot());
            this.rowXmlViewBinding = rowXmlViewBinding;
        }
    }
}

对于在 Kotlin 中寻找解决方案的人们,这里是:

这是一个最小的示例,其中适配器获取一个字符串数组,并在名为 itemTextView 的 TextView 中以名为 recyclerview_item 的布局显示每个字符串。

它基于@SomeshKumar 的回答和@Vijay Villiers 关于如何摆脱 private TextView txt;

的问题的回答

编辑:新版本: 我注意到生成的 ...Binding 有一个 .bind() 函数,所以让我们使用它。 (我猜它可能不那么占用资源?)

class SampleAdapter(private val context: Context, private val content: Array<String>) :
        RecyclerView.Adapter<SampleAdapter.CustomViewHolder>()
{
    class CustomViewHolder(view: View) : RecyclerView.ViewHolder(view)

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int) =
            CustomViewHolder(
                    // Alternatively inflate like usual, if you don't need binding
                    RecyclerviewItemBinding
                            .inflate(LayoutInflater.from(context), viewGroup, false)
                            .root
            )

    override fun getItemCount() = content.size

    override fun onBindViewHolder(viewHolder: CustomViewHolder, position: Int)
    {
        RecyclerviewItemBinding.bind(viewHolder.itemView).apply{
            itemTextView.text = content[position]
            
        }
    }
} 

编辑:旧版本:

class SampleAdapter(private val context: Context, private val content: Array<String>) :
        RecyclerView.Adapter<SampleAdapter.CustomViewHolder>()
{
    class CustomViewHolder(var viewBinding: RecyclerviewItemBinding) :
            RecyclerView.ViewHolder(viewBinding.root)

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int) =
            CustomViewHolder(
                    RecyclerviewItemBinding
                            .inflate(LayoutInflater.from(context), viewGroup, false)
            )

    override fun getItemCount() = content.size

    override fun onBindViewHolder(viewHolder: CustomViewHolder, position: Int)
    {
        viewHolder.viewBinding.apply {
            itemTextView.text = content[position]
        }
    }
}

Here is full recycler view adapter class in java :

public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.MyViewHolder> {

    private List<Note> notes;
    private ItemNotesBinding notesBinding;

    public NotesAdapter(List<Note> notes) {
        this.notes = notes;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    
        notesBinding = ItemNotesBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new MyViewHolder(notesBinding);
    }

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

        Note note = notes.get(position);
        notesBinding.tvTitle.setText(note.getNote());
    }

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

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemNotesBinding notesBinding;

        public MyViewHolder(@NonNull ItemNotesBinding binding) {
            super(binding.getRoot());
            notesBinding = binding;
        }
    }
}