title: Data binding while using fragment manager
body: <p>I recently started to learn data binding in Android, I understand the basics but when I'm trying to do some more complex data binding such as data binding of view pager with viewpager adapter (uses fragment manager) I don't completely understand how to use the fragment manager of the view (which is a fragment in my case, I use childfragment manager).
  I found a few incomplete examples and solutions but nothing seemed to work.</p>
  
  <p>Here is the code that I have so far:</p>
  
  <ul>
  <li><p>HomeFragment</p>
  
  <pre><code>class HomeFragment : Fragment() {
  
  lateinit var binding : FragmentHomeBinding
  lateinit var viewModel : HomeViewModel
  
  lateinit var viewPager : ViewPager
  lateinit var tabs : TabLayout
  
  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
      binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false)
      val view = binding.root
      viewModel = ViewModelProviders.of(this).get(HomeViewModel::class.java)
      setupViews(view)
  
      return view
  }
  
  fun setupViews(view : View){
      tabs = view.findViewById(R.id.tabs)
      viewPager = view.findViewById(R.id.pager)
  
      val childFragmentManager = childFragmentManager
  
      viewPager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabs))
  
  
      tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
          override fun onTabSelected(tab: TabLayout.Tab) {
              viewPager.currentItem = tab.position
          }
  
          override fun onTabUnselected(tab: TabLayout.Tab) {
  
          }
  
          override fun onTabReselected(tab: TabLayout.Tab) {
  
          }
      })
  }
  </code></pre>
  
  <p>}</p></li>
  <li><p>home_fragment.xml</p>
  
  <p>
  </p>
  
  <pre><code>&lt;data&gt;
  
      &lt;variable
          name="fm"
          type="androidx.fragment.app.FragmentManager" /&gt;
  
      &lt;variable
          name="viewModel"
          type=".HomeViewModel" /&gt;
  
      &lt;variable
          name="adapter"
          type=".HomeViewPagerAdapter" /&gt;
  
  &lt;/data&gt;
  
  &lt;androidx.constraintlayout.widget.ConstraintLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
  
      &lt;com.google.android.material.tabs.TabLayout
          android:id="@+id/tabs"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:tabMaxWidth="0dp"
          app:setupWithViewPager="@{pager}"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent" &gt;
  
          &lt;com.google.android.material.tabs.TabItem
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/upcoming_tab"/&gt;
          &lt;com.google.android.material.tabs.TabItem
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/synced_items_tab"/&gt;
  
      &lt;/com.google.android.material.tabs.TabLayout&gt;
  
  
      &lt;androidx.viewpager.widget.ViewPager
          android:id="@+id/pager"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          app:adapter="@{adapter(fm)}"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/tabs" /&gt;
  
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  
  <p></p></li>
  <li><p>HomeViewModel</p>
  
  <p>class HomeViewModel: ViewModel(){</p>
  
  <pre><code>@BindingAdapter("adapter")
  fun setAdapter(pager: ViewPager, fm: FragmentManager) {
      pager.adapter = HomeViewPagerAdapter(pager.context,fm)
  }
  </code></pre>
  
  <p>}</p></li>
  </ul>