0

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.

anta40
  • 6,511
  • 7
  • 46
  • 73
  • REF: https://stackoverflow.com/questions/39483094/data-binding-class-not-generated Is the root tag of `plan_other_item_layout.xml` == `layout`? When you enable data binding in the `gradle`, the Binding class shall be auto generated if the layout file contains a root tag `layout` – Android Newbie A Jan 27 '23 at 07:50
  • Nope, and so does `plan_journey_item_layout.xml`. Turned out there were a few errors on another part, and Android Studio didn't generate the viewbinding until all errors were solved. Then I typed `PlanOtherItemLayoutBinding` and Android Studio happily imported it. Silly me – anta40 Jan 27 '23 at 10:40

2 Answers2

0
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();
    }
}

Update :- Don't forget to add below code in xml layout which you want to use

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        
    </data>
    <androidx.constraintlayout.widget.ConstraintLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent">
        .. 
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
jayesh gurudayalani
  • 1,368
  • 1
  • 9
  • 14
0

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