Android Fragment的学习

Fragment

一,静态使用Fragment

  1,继承Fragment,重写oncreateview方法。

  2,在mainacitivity.xml中声明此fragment。

   


public class Fragment1 extends Fragment {


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1,container,false);

        return view;


    }
}
 
 
   在main.xml中:
 
  <fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="com.baikuzi.lb4_5fragment2.Fragment1"
    android:id="@+id/fragment"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
 
 
二,动态使用Fragment。
    Fragment的删除和替换。
 
              <pre name="code" class="java" style="color: rgb(85, 85, 85); font-size: 15px; line-height: 35px; white-space: pre-wrap; word-wrap: break-word; background-color: rgb(255, 255, 255);">               // 开启Fragment事务
                FragmentManager fm = getFragmentManager();
		FragmentTransaction transaction = fm.beginTransaction();
                <span style="font-family: 宋体;">transaction.</span><span style="font-family: 宋体; font-size: 12pt;">replace(R.id.</span><span style="font-family: 宋体; font-size: 12pt; color: rgb(102, 14, 122);  "><strong><em>framelayout1</em></strong></span><span style="font-family: 宋体; font-size: 12pt;">,</span><span style="font-family: 宋体; font-size: 12pt; color: rgb(0, 0, 128); "><strong>new </strong></span><span style="font-family: 宋体; font-size: 12pt;">Fragment2());</span>
<span style="font-family: 宋体; font-size: 12pt;">               </span><span style="font-family: 宋体;">transaction.commit();</span>
<span style="font-weight: bold; font-family: 'microsoft yahei';"> transaction.add() </span>
<span style="font-family: 宋体;"></span><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;"><span style="margin: 0px; padding: 0px; white-space: pre;"></span><span style="margin: 0px; padding: 0px; white-space: pre;"></span>往Activity中添加一个Fragment</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;"><strong>transaction.remove()<span style="margin: 0px; padding: 0px; white-space: pre;"> </span></strong></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;"><span style="margin: 0px; padding: 0px; white-space: pre;"></span><span style="margin: 0px; padding: 0px; white-space: pre;"></span>从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈(回退栈后面会详细说),这个Fragment实例将会被销毁。
</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;"><strong>transaction.replace()</strong></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;"><span style="margin: 0px; padding: 0px; white-space: pre;"></span><span style="margin: 0px; padding: 0px; white-space: pre;"></span>使用另一个Fragment替换当前的,实际上就是remove()然后add()的合体~</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;"><strong>transaction.hide()</strong></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;"><span style="margin: 0px; padding: 0px; white-space: pre;"></span><span style="margin: 0px; padding: 0px; white-space: pre;"></span>隐藏当前的Fragment,仅仅是设为不可见,并不会销毁</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;"><strong>transaction.show()</strong></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;"><span style="margin: 0px; padding: 0px; white-space: pre;"></span><span style="margin: 0px; padding: 0px; white-space: pre;"></span>显示之前隐藏的Fragment</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">
</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">
</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">以上,学习自:</p>
 
http://blog.csdn.net/lmj623565791/article/details/37970961
 
 
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值