I want to include a layout with databinding.
I want to pass the id from java to my layout, using an enum
, but I cant seem to find out the right syntax.
Here is my Fragment class with the enum
and the onCreateView()
:
private enum TYPE{
A(R.layout.fragment_item_A),
B(R.layout.fragment_item_B),
C(R.layout.fragment_item_C);
public final int id;
TYPE(int id) {
this.id = id;
}
private int getId(){
return id;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//...
this.type = TYPE.A;
FragmentItemBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_item, container, false);
tmpView = binding.getRoot();
binding.setType(type);
//...
return tmpView;
}
I want to inflate R.layout.fragment_item
and make it include R.layout.fragment_item_A
(or another Type).
Here is my unsuccessful attempt in my R.layout.fragment_item XML file:
<data>
<variable name="type" type="com.dan.myapp.fragments.MyFragmentClass.TYPE"/>
...
</data>
<include layout="@{type.id}" <!-- layout="@layout/fragment_item_A"-->
android:id="@+id/included_item"/>
How should I write the binding in the include ?
PS : maybe the answer is in this post or in this one, but I didn't find it...