当前位置:首页 > 嵌入式 > 嵌入式软件
[导读] 利用actionbar同样也可以轻松的实现tab导航的效果,配合使用fragment实现切换不同view的功能。若想使用这个功能,1)设置actionBar.setNavigationMode(ActionBar.NAVIGATIO

 利用actionbar同样也可以轻松的实现tab导航的效果,配合使用fragment实现切换不同view的功能。

若想使用这个功能,1)设置actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS),使actionbar使用tab导航功能。2)调用actionbar的addTab()方法,添加多个tab标签,并为每个tab标签添加时间监听器。

MyFragment.java

01 package com.app.main;
02  
03 import android.annotation.SuppressLint;
04 import android.app.Fragment;
05 import android.content.Context;
06 import android.os.Bundle;
07 import android.view.LayoutInflater;
08 import android.view.View;
09 import android.view.ViewGroup;
10 import android.view.ViewGroup.LayoutParams;
11 import android.widget.TextView;
12  
13 @SuppressLint("NewApi")
14 public class MyFragment extends Fragment {
15  
16     @Override
17     public View onCreateView(LayoutInflater inflater, ViewGroup container,
18             Bundle savedInstanceState) {
19  
20         Context context = this.getActivity();
21  
22         TextView tv = new TextView(context);
23  
24         Bundle arc = this.getArguments();
25  
26         int tabs=arc.getInt("key");
27          
28         tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
29                 LayoutParams.WRAP_CONTENT));
30  
31         tv.setText("hello actionbar "+tabs);
32  
33         return tv;
34  
35     }
36  
37 }

main.xml

01 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
02     xmlns:tools="http://schemas.android.com/tools"
03     android:layout_width="match_parent"
04     android:layout_height="match_parent"
05     tools:context=".Main" >
06  
07     <LinearLayout
08         android:id="@+id/container"
09         android:layout_width="match_parent"
10         android:layout_height="match_parent"
11         android:orientation="vertical" />
12  
13 </RelativeLayout>

Main.java

01 package com.app.main;
02  
03 import android.app.ActionBar;
04 import android.app.ActionBar.Tab;
05 import android.app.Activity;
06 import android.app.FragmentTransaction;
07 import android.os.Bundle;
08  
09 public class Main extends Activity implements ActionBar.TabListener {
10  
11     ActionBar actionBar = null;
12  
13     @Override
14     protected void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.main);
17  
18         actionBar = this.getActionBar();
19  
20         actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
21  
22         actionBar.addTab(actionBar.newTab().setText("tab1")
23                 .setTabListener(this));
24  
25         actionBar.addTab(actionBar.newTab().setText("tab2")
26                 .setTabListener(this));
27  
28         actionBar.addTab(actionBar.newTab().setText("tab3")
29                 .setTabListener(this));
30  
31     }
32  
33     @Override
34     public void onTabReselected(Tab tab, FragmentTransaction ft) {
35  
36     }
37  
38     @Override
39     public void onTabSelected(Tab tab, FragmentTransaction ft) {
40  
41         MyFragment frag = new MyFragment();
42  
43         int index = tab.getPosition() + 1;
44  
45         Bundle bundle = new Bundle();
46  
47         bundle.putInt("key", index);
48  
49         frag.setArguments(bundle);
50  
51         FragmentTransaction action = Main.this.getFragmentManager()
52                 .beginTransaction();
53  
54         action.replace(R.id.container, frag);
55  
56         action.commit();
57  
58     }
59  
60     @Override
61     public void onTabUnselected(Tab tab, FragmentTransaction ft) {
62  
63     }
64  
65 }

实现效果:

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭