Skip to content

Instantly share code, notes, and snippets.

@communikein
Last active January 16, 2018 14:26
Show Gist options
  • Save communikein/c843b6330887069a6f046b7993c9de3f to your computer and use it in GitHub Desktop.
Save communikein/c843b6330887069a6f046b7993c9de3f to your computer and use it in GitHub Desktop.
public class MainActivity extends AppCompatActivity {
public static final ArrayList<String> TABS_TITLE = new ArrayList<>();
private static final String FRAGMENT_MAP_TITLE = "Map";
private static final String FRAGMENT_LIST_TITLE = "List";
/* */
private FragmentPoisBinding mBinding;
/* */
private PoisViewModel mViewModel;
public PoisFragment() {
TABS_TITLE.add(FRAGMENT_MAP_TITLE);
TABS_TITLE.add(FRAGMENT_LIST_TITLE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
AndroidInjection.inject(this);
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
mViewModel = ViewModelProviders.of(this).get(PoisViewModel.class);
initViewPager();
}
public PoisViewModel getViewModel() {
return mViewModel;
}
private void initViewPager() {
initViewPager(mBinding.viewpager);
mBinding.tabs.setupWithViewPager(mBinding.viewpager);
}
private void initViewPager(ViewPager viewPager) {
Adapter adapter = new Adapter(getSupportFragmentManager());
PoisMapFragment poisMapFragment = new PoisMapFragment();
poisMapFragment.setViewModel(mViewModel);
PoisListFragment poisListFragment = new PoisListFragment();
poisListFragment.setViewModel(mViewModel);
adapter.addFragment(poisMapFragment, FRAGMENT_MAP_TITLE);
adapter.addFragment(poisListFragment, FRAGMENT_LIST_TITLE);
viewPager.setAdapter(adapter);
}
static class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public Adapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment