23

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

@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(),"");
            }
        });
    }
}

另外,我想知道在同一个班级中使用是否R.layout.layout_name正确ViewBinding

4

5 回答 5

32

您需要做的是将生成的绑定类对象传递给持有者类构造函数。在您的示例中,您有项目的common_circle_imageXML 文件,RecyclerView并且生成的类CommonCircleImageBinding就像这样,您使用将onCreateViewHolder生成的绑定类传递给ViewHolder

@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);
}

并像这样使用持有者类,这样您就可以在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 ;
    }
}
于 2020-03-13T11:50:17.500 回答
21

这是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;
        }
    }
}
于 2020-06-08T05:34:05.297 回答
6

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

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

它基于@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]
        }
    }
}
于 2020-07-24T10:52:21.393 回答
1

这是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;
        }
    }
}
于 2020-08-04T12:30:04.117 回答
0

您可以通过以下CommonCircleImageBinding方式直接创建onCreateViewHolderCommonCircleImageBinding.inflate(LayoutInflater.from(parent.getContext()))

然后将其传递给 MyViewHolder

于 2020-03-13T04:15:59.183 回答