fabioCollini / mv2m

Android MVVM lightweight library based on Android Data Binding
Apache License 2.0
183 stars 21 forks source link

viewPager #6

Closed Hanspagh closed 7 years ago

Hanspagh commented 8 years ago

Do you have any examples on how to use this with a viewPager?

osiloke commented 8 years ago

view pagers use fragments so you can just have your view pager fragments extend it.cosenonjaviste.mv2m.ViewModelFragment

Hanspagh commented 8 years ago

That makes sense, how would you then parse each of the items in the adapter into the fragment/Viewmodel. Though constructors?

osiloke commented 8 years ago

well what i did in my project was to use a regular pager adapter in my activity and then just handle each page fragment as a ViewModelFragment. For example in my container activity, MainFeedFragment extends ViewModelFragment.

@Override
public void onCreate(Bundle savedInstanceState ) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.id.activity_main); 
        pagerAdapter = new PagerAdapter(getSupportFragmentManager());
        //get bound +id/viewPager in layout and attach a pagerAdapter
        binding.viewPager.setAdapter(pagerAdapter);
}
private class PagerAdapter extends FragmentStatePagerAdapter {
        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0){
                return new MainFeedFragment();
            }
            return null;
        }

        @Override
        public int getCount() {
            return 1;
        }

    }