Home > Software design >  How to generate ViewBinding for RecyclerView's ViewHolder?
How to generate ViewBinding for RecyclerView's ViewHolder?

Time:01-27

I've seen few parts like this in the project I'm working on:

   public class ViewHolder extends RecyclerView.ViewHolder {
        public PlanJourneyItemLayoutBinding binding;


        public ViewHolder(View view) {
            super(view);
            binding = PlanJourneyItemLayoutBinding.bind(view);
        }
    }

I'm on MacOS, and if binding is highlighted and Shift Cmd B is pressed, Android Studio will show you the content of plan_journey_item_layout.xml, which is the one inflated in onCreateViewHolder(). No problem so far.

Now what if I want to use another XML layout for the viewholder, say plan_other_item_layout.xml? I tried cleaning and rebuilding the project, and couldn't import PlanOtherItemLayoutBinding, for example.

CodePudding user response:

dataBinding {
        enabled = true
    }

To enable the new data binding compiler, add the following option to your gradle.properties file:

android.databinding.enableV2=true

Update:

To enable databinding please use,

android {
    ...
    buildFeatures {
        dataBinding true
    }
}

Please follow this link: https://developer.android.com/jetpack/androidx/releases/databinding

Sample:

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> implements CustomClickListener {

private List<DataModel> dataModelList;
private Context context;

public MyRecyclerViewAdapter(List<DataModel> dataModelList, Context ctx) {
    this.dataModelList = dataModelList;
    context = ctx;
}

@Override
public MyRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                           int viewType) {
    ItemRowBinding binding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.getContext()),
            R.layout.item_row, parent, false);

    return new ViewHolder(binding);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    DataModel dataModel = dataModelList.get(position);
    holder.bind(dataModel);
    holder.itemRowBinding.setItemClickListener(this);
}


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

public class ViewHolder extends RecyclerView.ViewHolder {
    public ItemRowBinding itemRowBinding;

    public ViewHolder(ItemRowBinding itemRowBinding) {
        super(itemRowBinding.getRoot());
        this.itemRowBinding = itemRowBinding;
    }

    public void bind(Object obj) {
        itemRowBinding.setVariable(BR.model, obj);
        itemRowBinding.executePendingBindings();
    }
}

public void cardClicked(DataModel f) {
    Toast.makeText(context, "You clicked "   f.androidName,
            Toast.LENGTH_LONG).show();
    }
}

CodePudding user response:

Step 1: Rename the file to something else. Like "plan_other_item.xml"

Step 2: Invalidate Cache & Restart on android studio from "File" -> "Invalidate Caches" -> " Invalidate Cache & Restart. "

Step 3: Rebuild the project. and try to import.

import <YOUR_PACKAGE_NAME>.databinding.PlanOtherItemBinding

  • Related