Use Tab layout in MVVM architecture with the data binding library

后端 未结 3 494
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-31 21:48

I am developing an app that has a tab layout as the image.

I’d like to use MVVM architecture with data binding library but I am new with this framework.

3条回答
  •  轮回少年
    2021-01-31 22:13

    I'm not sure if this is newly introduced recently but with Android Support version 27.1.1, you don't even even need a custom data binding adapter, you can simply use:

    
    
    
            
    

    Take note that the viewPager variable in app:setupWithViewPager="@{some_fragment_viewpager}" points to android:id="@+id/some_fragment_viewpager". That's how the reference to the ViewPager is done (like magic I know)!

    ViewModel

    public class SomeViewModel {
      public ViewPager.OnPageChangeListener pageChangeListener;
      public SomeFragmentPagerAdapter pagerAdapter;
      // ...
    }
    

    FragmentPagerAdapter

    public classs SomeFragmentPagerAdapter extends FragmentPagerAdapter {
      public Boolean currentPosition;
    }
    

提交回复
热议问题