0

I'm trying to use the databinding library with a recyclerview with multiple viewholders (with shared elements).

I tried solutions that involve casting and generics. Neither worked.

Is there a way to relate different ViewDataBinding classes that link similar xml layouts such that they have a sibling relationship?

Or, Is there a way to do inheritance in xml? I figure since the ViewDataBinding objects are generated (in part) by the xml, maybe parent and children xml layouts could produce the desired effects?

1 Answers1

0

Make Sure to read my comment below for the solution implemented. I'm marking this as correct because it eventually got me to the correct answer -Joel

Hope I understood your problem.
Consider the snippet below.

parent_xml.xml

<layout>
   <FrameLayout
   ....
   ...
   >

   <!-- including child -->
   <include 
       android:id="@+id/included"
       layout="@layout/child_xml" /> 

   </FrameLayout>
</layout>

child_xml.xml

<layout>
    <View
        android:id="@+id/child_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</layout>

You can access child view using..

ParentXmlBinding mBinding = DataBindingUtil.inflate(... reference of parent_xml);
mBinding.included.childView.(... Access view method here); // Referring child view

Have I answered your question?

Paresh P.
  • 6,677
  • 1
  • 14
  • 26
  • so, I like your idea, but I couldn't find a way to inflate the view (at first). I tried passing in a Integer object with the resource id of the desired layout into the xml, hoping I could create a reference to the layout from the resource id (it didn't work). Then I tried replacing the include tag with a view and then set the view to the View.inflate() output (didn't work). The soln that worked include having multiple include tags that linked to the unique views. I set the visibility of the unique views to View.GONE when I didn't need them. – Joel Robinson-Johnson Jan 25 '18 at 22:10