2013年8月27日星期二

ActionBar tab example

之前用的是TabActivity,现在用4.2,没有找到相关功能。发现还有一个ActionBar,找了一下午,发现这方面的资料好少,最基本的tab实例都没有特别清晰的解释。发现国外一个网站上有个例子很不错,网址见原文链接,翻不了的将就着看吧
共7个文件,分别是4个class(两个Fragment ,一个MainActivity,一个Listener),3个布局文件(一个main,两个fragment)
1.MainActivity.java
01public class MainActivity extends Activity {
02 
03    public static Context appContext;
04     
05    @Override
06    protected void onCreate(Bundle savedInstanceState) {
07        super.onCreate(savedInstanceState);
08        setContentView(R.layout.main);
09      //ActionBar gets initiated
10        ActionBar actionbar = getActionBar();
11      //Tell the ActionBar we want to use Tabs.
12        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
13      //initiating both tabs and set text to it.
14        ActionBar.Tab PlayerTab = actionbar.newTab().setText("Fragment A");
15        ActionBar.Tab StationsTab = actionbar.newTab().setText("Fragment B");
16      //create the two fragments we want to use for display content
17        Fragment PlayerFragment = new AFragment();
18        Fragment StationsFragment = new BFragment();
19      //set the Tab listener. Now we can listen for clicks.
20        PlayerTab.setTabListener(new MyTabsListener(PlayerFragment));
21        StationsTab.setTabListener(new MyTabsListener(StationsFragment));
22      //add the two tabs to the actionbar
23        actionbar.addTab(PlayerTab);
24        actionbar.addTab(StationsTab);
25    }
2.AFragment.java
1public class AFragment extends Fragment {
2 
3    @Override
4    public View onCreateView(LayoutInflater inflater, ViewGroup container,
5            Bundle savedInstanceState) {
6        return inflater.inflate(R.layout.afragment, container, false);
7    }
8}
3.BFragment.java
1public class BFragment extends Fragment{
2 
3    @Override
4    public View onCreateView(LayoutInflater inflater, ViewGroup container,
5            Bundle savedInstanceState) {
6        return inflater.inflate(R.layout.bfragment, container, false);
7    }
8}
4.MyTabsListener.java
01public class MyTabsListener implements TabListener {
02 
03    public Fragment fragment;
04     
05    public MyTabsListener(Fragment fragment) {
06        this.fragment = fragment;
07        }
08     
09    @Override
10    public void onTabReselected(Tab tab, FragmentTransaction ft) {
11        Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
12    }
13 
14    @Override
15    public void onTabSelected(Tab tab, FragmentTransaction ft) {
16        ft.replace(R.id.fragment_container, fragment);
17    }
18 
19    @Override
20    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
21        ft.remove(fragment);
22    }
23 
24}
5.main.xml
01<span class="goog_qs-tidbit goog_qs-tidbit-0"><LinearLayout
02xmlns:android="http://schemas.android.com/apk/res/android"</span>
03android:orientation="vertical"
04android:layout_width="fill_parent"
05android:layout_height="fill_parent" android:layout_gravity="center">
06     
07    <LinearLayout
08        android:id="@+id/fragment_container"
09        android:layout_width="match_parent"
10        android:layout_height="match_parent" >
11    </LinearLayout>
12</LinearLayout>
6.afragment.xml
01<?xml version="1.0" encoding="utf-8"?>
02<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03    android:layout_width="match_parent"
04    android:layout_height="match_parent"
05    android:orientation="vertical" >
06     
07    <TextView
08        android:layout_width="match_parent"
09        android:layout_height="wrap_content"
10        android:text="1"
11        />
12</LinearLayout>
7.bfragment.xml


01<?xml version="1.0" encoding="utf-8"?>
02<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03    android:layout_width="match_parent"
04    android:layout_height="match_parent"
05    android:orientation="vertical" >
06     
07    <TextView
08        android:layout_width="match_parent"
09        android:layout_height="wrap_content"
10        android:text="2"
11        />
12</LinearLayout>