manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.robert.sample20"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name=".MainActivity"
android:label="TabBarNavigation"
android:uiOptions="splitActionBarWhenNarrow">
<intent-filter >
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
menu
<?xml version="1.0"
encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="@+id/settings"
android:title="settings"
android:showAsAction="always"/>
<item
android:id="@+id/about"
android:title="about"
android:showAsAction="always"/>
</menu>
MainActivity
package com.robert.sample20;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar
actionBar=getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab
tab=actionBar
.newTab()
.setText("First
Tab")
.setTabListener(new
AllTabListener<FragmentA>(this,"fragmentA",FragmentA.class));
actionBar.addTab(tab);
tab=actionBar.newTab()
.setText("Second
Tab")
.setTabListener(new
AllTabListener<FragmentB>(this,"fragmentB",FragmentB.class));
actionBar.addTab(tab);
tab=actionBar.newTab()
.setText("Third
Tab")
.setTabListener(new
AllTabListener<FragmentC>(this,"fragmentC",FragmentC.class));
actionBar.addTab(tab);
}
@Override
public boolean
onCreateOptionsMenu(Menu menu) {
MenuInflater
inflater=getMenuInflater();
inflater.inflate(R.menu.main_menu,
menu);
return true;
}
@Override
public boolean
onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()){
case R.id.settings:
handleSettings();
break;
case R.id.about:
handleAbout();
break;
}
return true;
}
private void
handleSettings(){
Toast.makeText(this,
"clickSetting", Toast.LENGTH_SHORT).show();
}
private void handleAbout(){
Toast.makeText(this,
"clickABout", Toast.LENGTH_SHORT).show();
}
public static class
AllTabListener<T extends Fragment> implements ActionBar.TabListener{
private final
Class<T> mClass;
private final
Activity mActivity;
private final
String mTag;
private T mFragment;
public AllTabListener(Activity
activity,String tag,Class<T> clz){
mActivity=activity;
mClass=clz;
mTag=tag;
}
@Override
public void
onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void
onTabSelected(Tab tab, FragmentTransaction ft) {
if(mFragment==null){
mFragment=(T)T.instantiate(mActivity,mClass.getName());
ft.add(R.id.viewContainer,
mFragment,mTag);
}else{
ft.attach(mFragment);
}
}
@Override
public void
onTabUnselected(Tab tab, FragmentTransaction ft) {
if(mFragment!=null){
ft.detach(mFragment);
}
}
}
}
沒有留言:
張貼留言