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