I would like to have simple lightweight tabs - no Fragments as the tabs do not have separate lifecycle functionality and can already be reused as ViewGroups. Swiping is not required and I do not want a ViewPager unless I absolutely must.
I have an example working with ViewPager and my simple views converted into Fragments, but it forces me to change the type of my base Activity class and draws in several support libraries, doubling the size of my app, which was supposed to be very small.
Basically something like this example, but with actual xml-defined layouts shown within the tabs rather than a toast message when clicking the tab titles.
This is what I tried:
public class LightweightTabs extends Activity {
@Override
protected void onCreate(Bundle bundy) {
super.onCreate(bundy);
setContentView(R.layout.main);
LayoutInflater linflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TabLayout tabs = findViewById(R.id.tabs);
tabs.addView(new WiFiOptions().view(linflater, this, null));
tabs.addView(new BluetoothOptions().view(linflater, this, null));
tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
tab.select();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {}
@Override
public void onTabReselected(TabLayout.Tab tab) {}
});
}
...
}
The Views for the tab classes look like this:
public class WiFiOptions {
public View view(LayoutInflater linflater, final Context con, ViewGroup parentView) {
final View view = linflater.inflate(R.layout.wifi, parentView);
...
return view;
}
}
As noted in the comments on that page, it doesn't work because
you can only add an instance of TabItem via addView
– same as addTab.
Do I need to write my own PagerAdapter to do this?
(As an aside, I don't get why it is not set up so we could just specify a layout in xml for each TabItem under a TabLayout and easily do all of this in xml..)