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

為{幸葍}努か 提交于 2020-04-06 22:22:13

问题


Here is the code I used in my RecycleView adapter class. I don't know this is the right way or not to use View Binding. If you have a better solution answer me. Thank you.

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

Also, I want to know is it right to use R.layout.layout_name and ViewBinding in the same class.


回答1:


What you need to do is pass the generated binding class object to the holder class constructor. In your example, You have common_circle_image XML file for RecyclerView item and the generated class is CommonCircleImageBinding so like this you use the onCreateViewHolder to pass the generated binding class to the 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);
}

and use the holder class like this so you can use these fields in 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 ;
    }
}



回答2:


You can create CommonCircleImageBinding directly in onCreateViewHolder by CommonCircleImageBinding.inflate(LayoutInflater.from(parent.getContext()))

Then pass it to MyViewHolder




回答3:


You can directly use DataBindingUtil to get the binding from the layout in your onCreateViewHolder as follows.

Then modify your ViewHolder to take the binding as an argument.

@Override
public CategoryAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    CommonCircleImageBinding binding = DataBindingUtil
        .inflate(inflater, R.layout.common_circle_image, parent, false);

    return new MyViewHolder(binding);
}

public class MyViewHolder extends RecyclerView.ViewHolder {

    public MyViewHolder(@NonNull CommonCircleImageBinding binding) {
        super(binding.getRoot());

        binding.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                commonItemClick.onItemClick(getAdapterPosition(),"");
            }
        });
    }
}


来源:https://stackoverflow.com/questions/60664346/what-is-the-right-way-of-android-view-binding-in-the-recyclerview-adapter-class

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