问题
It appears that TabActivity is now deprecated, we need to use Fragments.
I tried using a standard Activity but couldn't call getTabHost.
So it appears i have to use Fragments. But i am a little confused how this would work.
I understand that fragments are not activities so they are not in the manifest file?
SO i presume i can't do startActivity on a fragment?
Does anyone know of a good example explaining the tabHost and Fragments, all the examples and tutorials i have found are only using the tabactivity.
Thanks in advance
回答1:
Maybe you could use TabLayout instead.
Tabs are now best implemented by leveraging the ViewPager with a custom "tab indicator" on top. Google's new TabLayout included in the support design library release for Android "M".
Visit the full tutorial of using TabLayout in Google Play Style Tabs using TabLayoutEdit PagePage History
回答2:
Today Android got a nice tutorials for those. Start at Creating Swipe Views with Tabs
Here is a small snapshot of how to create tabs
@Override
public void onCreate(Bundle savedInstanceState) {
final ActionBar actionBar = getActionBar();
...
// Specify that tabs should be displayed in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create a tab listener that is called when the user changes tabs.
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// show the given tab
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// hide the given tab
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// probably ignore this event
}
};
// Add 3 tabs, specifying the tab's text and TabListener
for (int i = 0; i < 3; i++) {
actionBar.addTab(
actionBar.newTab()
.setText("Tab " + (i + 1))
.setTabListener(tabListener));
}
}
回答3:
First - you can startActivity from your fragment by something like context.startActivity(....);
Second to understand the tabs using fragment just create one sample project on your Android Studio and when it asks you to choose the existing template then choose the one which is having tabs. This way you will see the standard code.
回答4:
Lets face it. Fragments are the future. And we are being guided towards using them.
I found the following tutorials to be quite informative and hope they would answer all your 'Tabs with Fragments questions':
- JavaCodeGeeks - Android Fragment Tabs Example
- Truiton - Android Tabs Example – With Fragments and ViewPager
回答5:
I think your problem with using selected realization. Android SDK contains a lot of variants of View, such as ActivityList, TabActivity, ActionBarActivity, etc. And they all deprecated or will deprecated. You should ask why?
- Look at the different example with Tabs and List in new Material. Application contains main header, like ToolbarLayout, and in this contains different state of Toolbar, you may add TabLayout, and other cool things.
So answer on your question - It's better to use customize and powerful view elements, then several implementations of main action view (Activity). This reason still more visible, when you look to action specification of elements. Activity don't need to take place in view initialization.
来源:https://stackoverflow.com/questions/12382389/android-tabactivity-deprecated-use-fragments