fragment静态动态使用

Fragment的创建

  1. public class ContentFragment extends Fragment  

  2. {  

  3.     @Override  

  4.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  

  5.             Bundle savedInstanceState)  

  6.     {  

  7.         return inflater.inflate(R.layout.fragment_content, container, false);  

  8.     }   

静态使用:

        在布局文件中直接写就可以、很简单。

  1.  <fragment  

  2.         android:layout_below="@id/id_fragment_title"  

  3.         android:id="@+id/id_fragment_content"  

  4.         android:name="com.zhy.zhy_fragments.ContentFragment"  

  5.         android:layout_width="fill_parent"  

  6.         android:layout_height="fill_parent" />  


动态Fragment的使用:

        在Fragment的父所属的Activity中留出一块位置:

        这边暂且用一个FramLayout占一个位置,下面在代码中用Fragment对着块进行赋予Fragment,可以动态的用别的Fragment替换。

  1.  <FrameLayout  

  2.         android:id="@+id/id_content"  

  3.         android:layout_width="fill_parent"  

  4.         android:layout_height="fill_parent"  

  5.         android:layout_above="@id/id_ly_bottombar"  

  6.         android:layout_below="@id/id_fragment_title" />

      Activity中的代码:  

  1. public class MainActivity extends Activity implements OnClickListener  

  2. {  

  3.     private LinearLayout mTabWeixin;  

  4.     private LinearLayout mTabFriend;  

  5.   

  6.     private ContentFragment mWeixin;  

  7.     private FriendFragment mFriend;  

  8.   

  9.     @Override  

  10.     protected void onCreate(Bundle savedInstanceState)  

  11.     {  

  12.         super.onCreate(savedInstanceState);  

  13.         requestWindowFeature(Window.FEATURE_NO_TITLE);  

  14.         setContentView(R.layout.activity_main);  

  15.   

  16.         // 初始化控件和声明事件  

  17.         mTabWeixin = (LinearLayout) findViewById(R.id.tab_bottom_weixin);  

  18.         mTabFriend = (LinearLayout) findViewById(R.id.tab_bottom_friend);  

  19.         mTabWeixin.setOnClickListener(this);  

  20.         mTabFriend.setOnClickListener(this);  

  21.   

  22.         // 设置默认的Fragment  

  23.         setDefaultFragment();  

  24.     }  

  25.   这一块代码是动态使用Fragment的关键代码,只需要记住写法就行:

  26.     private void setDefaultFragment()  

  27.     {  

  28.         FragmentManager fm = getFragmentManager();  

  29.         FragmentTransaction transaction = fm.beginTransaction();  

  30.         mWeixin = new ContentFragment();  

  31.         transaction.replace(R.id.id_content, mWeixin);  

  32.         transaction.commit();  

  33.     }  

  34.   

  35.     @Override  

  36.     public void onClick(View v)  

  37.     {  

  38.         FragmentManager fm = getFragmentManager();  

  39.         // 开启Fragment事务  

  40.         FragmentTransaction transaction = fm.beginTransaction();  

  41.   

  42.         switch (v.getId())  

  43.         {  

  44.         case R.id.tab_bottom_weixin:  

  45.             if (mWeixin == null)  

  46.             {  

  47.                 mWeixin = new ContentFragment();  

  48.             }  

  49.             // 使用当前Fragment的布局替代id_content的控件  

  50.             transaction.replace(R.id.id_content, mWeixin);  

  51.             break;  

  52.         case R.id.tab_bottom_friend:  

  53.             if (mFriend == null)  

  54.             {  

  55.                 mFriend = new FriendFragment();  

  56.             }  

  57.             transaction.replace(R.id.id_content, mFriend);  

  58.             break;  

  59.         }  

  60.         // transaction.addToBackStack();  

  61.         // 事务提交  

  62.         transaction.commit();  

  63.     }  

  64.   

  65. }  

上面activity 中的代码 对刚刚在布局中留给Fragment的位置赋予了各种Fragment,红色的代码就是我们用来占Fragment的Id,也是我们对那一块位置进行赋予Fragment的步骤。上面用的都是replace(),此外Fragment的动态加载还有各种,例如:

transaction.add() 

往Activity中添加一个Fragment

transaction.remove()

从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈(回退栈后面会详细说),这个Fragment实例将会被销毁。

transaction.replace()

使用另一个Fragment替换当前的,实际上就是remove()然后add()的合体~

transaction.hide()

隐藏当前的Fragment,仅仅是设为不可见,并不会销毁

transaction.show()

显示之前隐藏的Fragment

detach()

会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护。

attach()

重建view视图,附加到UI上并显示。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值