-
Notifications
You must be signed in to change notification settings - Fork 72
Magellan 1.x Implementing Tabs or other "Screens into a Screen" UI, using ScreenGroup
Ryan Moelter edited this page Jul 18, 2021
·
1 revision
Implementing Tabs, or any other "Screens into a Screen" UI should be pretty easy using ScreenGroup
BaseTabsScreen.java
public class BaseTabsScreen extends ScreenGroup<Screen, TabsView> {
public BaseTabsScreen(List<Screen> screens) {
super(screens);
}
@Override
protected TabsView createView(Context context) {
return new TabsView(context, getScreens());
}
@Override
protected void onShow(Context context) {
super.onShow(context);
for (Screen screen : getScreens()) {
getView().addTabView(screen.getView());
}
}
}
TabsView.java
public class TabsView extends BaseScreenView<BaseTabsScreen> {
@BindView(R.id.tabLayout) TabLayout tabLayout;
@BindView(R.id.pager) ViewPager pager;
public TabsView(Context context) {
super(context);
}
public TabsView(Context context, List<? extends Screen> tabScreens) {
super(context);
inflate(getContext(), R.layout.tabs, this);
bind(this);
initPager(context, tabScreens);
}
@VisibleForTesting
public TabsView(Context context, List<? extends Screen> tabScreens, TabLayout tabLayout, ViewPager pager) {
super(context);
this.tabLayout = tabLayout;
this.pager = pager;
initPager(context, tabScreens);
}
private void initPager(Context context, List<? extends Screen> tabScreens) {
TabsAdapter tabsAdapter = new TabsAdapter(context, tabScreens);
pager.setAdapter(tabsAdapter);
pager.setOffscreenPageLimit(Integer.MAX_VALUE);
pager.addOnPageChangeListener(new TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setTabsFromPagerAdapter(tabsAdapter);
tabLayout.setOnTabSelectedListener(new ViewPagerOnTabSelectedListener(pager));
}
public void addTabView(View view) {
pager.addView(view);
}
}
TabsAdapter.java
public class TabsAdapter extends PagerAdapter {
private final Context context;
private final List<? extends Screen> tabScreens;
public TabsAdapter(Context context, List<? extends Screen> tabScreens) {
this.context = context;
this.tabScreens = tabScreens;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
return tabScreens.get(position).getView();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {}
@Override
public int getCount() {
return tabScreens.size();
}
@Override
public boolean isViewFromObject(View v1, Object v2) {
return v1 == v2;
}
@Override
public CharSequence getPageTitle(int position) {
return tabScreens.get(position).getTitle(context);
}
}
Made with 💚 by the Wealthfront Android Team.