在ActionBar中进行Fragment之间的切换
来源:互联网 发布:软件开发培训机构 编辑:程序博客网 时间:2024/05/17 07:14
在ActionBar中添加标签(Tabs),每个标签对应的是一个Fragment,点击不同的Tab时,就会切换到对应的Fragment。 这里有五个关键步骤: 1. 要实现 ActionBar.TabListener接口,当点击Tab的时候触发这个接口里面的事件,有onTabSelected(), onTabUnselected(), 和 onTabReselected(). 实现ActionBar.TabListener接口时,应当在类内有个Fragment的引用,这样点击这个Tab时就可以调用对应的Fragment. 2. 通过getActionBar() 方法得到Activity中的ActionBar。 3. 设置AcitonBar的操作模式: setNavigationMode(NAVIGATION_MODE_TABS)。 4. 在ActionBar中添加Tabs:一.调用AciontBar的newTab()生成一个ActionBar.Tab. 二.为Tab增加text或者icon .调用setText() , setIcon() 三.为每个 ActionBar.Tab 添加ActionBar.TabListener. 5. 调用addTab()将生成的Tab加入ActionBar中 以下是例子代码,就是为了测试,没有实际的用途. 有两个Fragment:EditFragment和ComputerFragment,对应的Tab是"编辑"和计算。他们的XML和Activity如下: EditFragment:复制代码public class EditFragment extends Fragment{ @Override public void onCreate(Bundle savedInstanceState) { System.out.println("EidtFragment--->onCreate"); super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { System.out.println("EidtFragment--->onCreateView"); return inflater.inflate(R.layout.editfragment, container, false); } @Override public void onStop() { System.out.println("EidtFragment--->onStop"); super.onStop(); }}复制代码 editfragment.xml:复制代码<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:text="请输入你的信息:" android:textSize="20dp" /> <EditText android:layout_width="fill_parent" android:layout_height="40pt" android:layout_margin="5dp" android:background="@android:color/darker_gray" android:textSize="18dp" /></LinearLayout>复制代码ComputerFragment:复制代码public class ComputerFragment extends Fragment{ @Override public void onCreate(Bundle savedInstanceState) { System.out.println("ComputerFragment--->onCreate"); super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { System.out.println("ConputerFragment--->onCreateView"); return inflater.inflate(R.layout.computerfragment, container, false); } @Override public void onStop() { System.out.println("ConputerFragment--->onStop"); super.onStop(); }}复制代码computerfragment.xml:复制代码<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:text="简单加法计算" android:textSize="20dp" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="@android:color/darker_gray" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="@android:color/darker_gray" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="开始计算" /></LinearLayout>复制代码 主要的程序:MainActivity:复制代码public class MainActivity extends Activity{ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { System.out.println("MainActivity--->onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.main); // 得到Activity的ActionBar ActionBar actionBar = getActionBar(); // 设置AcitonBar的操作模型 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // 将Activity的头部去掉 actionBar.setDisplayShowTitleEnabled(false); // 生成Tab Tab edit = actionBar.newTab().setText("编辑"); Tab computer = actionBar.newTab().setText("计算"); // 为每个Tab添加Listener MyTabListener editListener = new MyTabListener(new EditFragment()); edit.setTabListener(editListener); MyTabListener computerListener = new MyTabListener(new ComputerFragment()); computer.setTabListener(computerListener); // 将Tab加入ActionBar中 actionBar.addTab(edit); actionBar.addTab(computer); } @Override protected void onStop() { System.out.println("MainActivity--->onStop"); super.onStop(); } /** * 实现ActionBar.TabListener接口 */ class MyTabListener implements TabListener { // 接收每个Tab对应的Fragment,操作 private Fragment fragment; public MyTabListener(Fragment fragment) { this.fragment = fragment; } public void onTabReselected(Tab tab, FragmentTransaction ft) { } // 当Tab被选中的时候添加对应的Fragment public void onTabSelected(Tab tab, FragmentTransaction ft) { ft.add(R.id.context, fragment, null); } // 当Tab没被选中的时候删除对应的此Tab对应的Fragment public void onTabUnselected(Tab tab, FragmentTransaction ft) { ft.remove(fragment); } }}程序运行结果:<img src="http://pic002.cnblogs.com/images/2012/285363/2012041023230414.png" alt="" /><img src="http://pic002.cnblogs.com/images/2012/285363/2012041023235586.png" alt="" /> 一颗平常心,踏踏实实,平静对待一切
0 0
- 在ActionBar中进行Fragment之间的切换
- 在ActionBar中进行Fragment之间的切换
- 在ActionBar中进行Fragment之间的切换
- 在ActionBar中进行Fragment之间的切换
- 在ActionBar中进行Fragment之间的切换
- 在ActionBar中进行Fragment之间的切换
- 在ActionBar中进行Fragment之间的切换
- Android ActionBar中进行Fragment切换
- ActionBar通过Tab进行不同的Fragment之间的交换
- Fragment之间的切换
- Fragment之间的切换
- Fragment之间的切换
- Fragment之间的切换
- 不同的fragment切换不同的actionbar.
- ActionBar + Fragment 实现两个碎片的切换
- 在activity、fragment中获得actionbar的高度
- 在activity,fragment中获得actionbar的高度
- 在Fragment之间切换,getActivity()为null的处理
- 20. Valid Parentheses
- Scala学习笔记三
- 从linux主目录中的makefile文件分析编译构成。。。
- ONE-CLASS SVM FOR LEARNING IN IMAGE RETRIEVAL
- sql 升序 ,降序
- 在ActionBar中进行Fragment之间的切换
- 实时操作系统与非实时操作系统到底有什么区别?
- iOS try catch
- Android版本和API Level对应关系
- ContentObserver 介绍
- JPush推送 之 RegistrationID 精确对点推送
- 【Android基础篇】TabWidget设置背景和字体
- Cloudfoundry之Service
- Unity里面三角面数