Fragment 构建程序的框架

通过Api可以发现原来使用用于构建普通应用程序的框架的一些类,比如TabActivity,ActivityGroup都已经不推荐使用了,转而替换成Fragment。今天我们就来看看如何使用Fragment来构建应用程序。

底部的BottomBar结合的是之前发出的一篇博客

常用的底部分段控件BottomBar(带有小图标)

下面是效果图:

 

我们再看下工程的目录:

 

这里对BottomBar就不解释了,主要讲下Fragment相关的。

首先你的界面需要继承自FragmentActivity,在它的布局文件中需要两个控件:

1. 屏幕底部的BottomBar

2. BottomBar上方的RelativeLayout(用来切换显示各个Fragment)。

接下去你就可以针对每个界面分开写布局和代码了。

 

注释代码中都有,下面直接上代码:

MainActivity.java

Java代码   收藏代码
  1. /** 
  2.  * This demo shows how to use FragmentActivity to build the frame of a common application. 
  3.  * To replace the deprecated class such as TabActivity, ActivityGroup,and so on. 
  4.  *  
  5.  * 这个demo展示了如何使用FragmentActivity来构建应用程序的框架 
  6.  * 可以使用这个来替代原来的TabActivity,ActivityGroup等等 
  7.  *  
  8.  * @author MichaelYe 
  9.  * @since 2012-9-6 
  10.  * @see http://developer.android.com/reference/android/app/Fragment.html 
  11.  * @see http://developer.android.com/training/basics/fragments/index.html 
  12.  * @see http://developer.android.com/guide/components/fragments.html 
  13.  * */  
  14. public class MainActivity extends FragmentActivity   
  15. {  
  16.   
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState)   
  19.     {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.         final BottomBar bottomBar = (BottomBar)findViewById(R.id.ll_bottom_bar);  
  23.         bottomBar.setOnItemChangedListener(new OnItemChangedListener()   
  24.         {  
  25.               
  26.             @Override  
  27.             public void onItemChanged(int index)   
  28.             {  
  29.   
  30.                 showDetails(index);  
  31.             }  
  32.         });  
  33.         bottomBar.setSelectedState(0);  
  34.           
  35. //        bottomBar.hideIndicate();//这个代码原来控制红色小图标的可见性  
  36. //        bottomBar.showIndicate(12);  
  37.           
  38.     }  
  39.       
  40.     private void showDetails(int index)  
  41.     {  
  42.         Fragment details = (Fragment)  
  43.                 getSupportFragmentManager().findFragmentById(R.id.details);  
  44.         switch(index)  
  45.         {  
  46.         case 0:  
  47.             details = new FragmentExecute();  
  48.             break;  
  49.         case 1:  
  50.             details = new FragmentLaunch();  
  51.             break;  
  52.         case 2:  
  53.             details = new FragmentTeam();  
  54.             break;  
  55.         case 3:  
  56.             details = new FragmentSearch();  
  57.             break;  
  58.         case 4:  
  59.             details = new FragmentSetting();  
  60.             break;  
  61.         }  
  62.         // Execute a transaction, replacing any existing  
  63.         // fragment with this one inside the frame.  
  64.         FragmentTransaction ft = getSupportFragmentManager().beginTransaction();  
  65.         ft.replace(R.id.details, details);  
  66.         ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);  
  67. //        ft.addToBackStack(null);//这行代码可以返回之前的操作(横屏的情况下,即两边都显示的情况下)  
  68.         ft.commit();  
  69.     }  
  70. }  

 

activity_main.xml

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="@drawable/bg_login"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <RelativeLayout  
  9.         android:id="@+id/details"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="fill_parent"  
  12.         android:layout_above="@+id/ll_bottom_bar" />  
  13.   
  14.     <com.michael.widget.BottomBar  
  15.         android:id="@+id/ll_bottom_bar"  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="60dip"  
  18.         android:layout_alignParentBottom="true" />  
  19.   
  20. </RelativeLayout>  

 

Java代码   收藏代码
  1. /** 
  2.  * 需要使用不带参数的构造器,可以使用getActivity()替换context参数 
  3.  * 否则屏幕在旋转的时候会抛出异常: 
  4.  * Caused by: java.lang.InstantiationException:  
  5.  * can't instantiate class com.michael.fragment.FragmentExecute; no empty constructor 
  6.  *  
  7.  * @see http://stackoverflow.com/questions/7016632/unable-to-instantiate-fragment 
  8.  * */  
  9. public class FragmentExecute extends Fragment  
  10. {  
  11.   
  12.     public FragmentExecute()  
  13.     {  
  14.     }  
  15.       
  16.     @Override  
  17.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  18.             Bundle savedInstanceState)   
  19.     {  
  20.         if (container == null)   
  21.         {  
  22.             // Currently in a layout without a container, so no  
  23.             // reason to create our view.  
  24.             return null;  
  25.         }  
  26.         LayoutInflater myInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
  27.         View layout = myInflater.inflate(R.layout.frag_execute, container, false);   
  28.           
  29.         return layout;  
  30.     }  
  31. }  

 

 frag_execute.xml

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     >  
  6.       
  7.     <RelativeLayout  
  8.         android:id="@+id/rl_title"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="45dip"  
  11.         android:layout_alignParentTop="true"  
  12.         android:layout_centerVertical="true"  
  13.         android:background="@drawable/bg_title_bar"  
  14.         android:gravity="center" >  
  15.   
  16.         <Button  
  17.             android:id="@+id/btn_back"  
  18.             android:layout_width="70dip"  
  19.             android:layout_height="fill_parent"  
  20.             android:layout_alignParentLeft="true"     
  21.             android:layout_marginBottom="5dip"  
  22.             android:layout_marginLeft="8dip"  
  23.             android:layout_marginTop="5dip"  
  24.             android:background="@drawable/title_btn_back_selector"  
  25.             android:text="@string/workbench_home_page"  
  26.             android:textColor="@color/title_button_color_gray" />  
  27.   
  28.         <Button  
  29.             android:id="@+id/btn_add"  
  30.             android:layout_width="70dip"  
  31.             android:layout_height="fill_parent"  
  32.             android:layout_alignParentRight="true"  
  33.             android:layout_marginBottom="5dip"  
  34.             android:layout_marginRight="8dip"  
  35.             android:layout_marginTop="5dip"  
  36.             android:background="@drawable/title_btn_rect_selector"  
  37.             android:text="@string/workbench_add_task"  
  38.             android:textColor="@color/title_button_color_gray" />  
  39.   
  40.         <TextView  
  41.             android:layout_width="wrap_content"  
  42.             android:layout_height="fill_parent"  
  43.             android:layout_centerInParent="true"  
  44.             android:gravity="center"  
  45.             android:text="@string/workbench_title"  
  46.             android:textColor="@color/title_color_white"  
  47.             android:textSize="22sp" />  
  48.     </RelativeLayout>  
  49.   
  50.     <TextView   
  51.         android:layout_width="wrap_content"  
  52.         android:layout_height="wrap_content"  
  53.         android:layout_centerInParent="true"  
  54.         android:textSize="30sp"  
  55.         android:text="@string/num_0"   
  56.         />  
  57.   
  58. </RelativeLayout>  

其它的Fragment也可以这样写。

 

使用Fragment有两个好处:

让手机和平板之间更好的显示,这个你可以参考API;

再就是有回退功能,addToBackStack(null); 加入这个方法,当你按返回键的时候,就可以实现界面的回退了(回到之前的Fragment)。

 

项目下载地址:

https://github.com/michaelye/DemoFragment

猜你喜欢

转载自lishuaishuai.iteye.com/blog/2291256