【android】FragMent浅学

【android】FragMent浅学


FragMent

FragMent作为Activity中的一部分,嵌套在Activity中,实现分为静态加载和动态加载。我的理解是类似于HTML中的Frame,这个模块有自己的布局,有自己的生命周期,单独处理自己的输入,在Activity运行的时候可以加载或者移除Fragment模块。

可以把Fragment设计成可以在多个Activity中复用的模块。在开发的应用的过程中,同时适用于平板与手机的开发,如图:


静态加载的实现:

在Activity中的布局文件中,添加啊

<!--<span style="color:#ff0000;">静态加载fragment要写唯一标识 android:id="@+id/fragment" 否则报错</span>-->
    <fragment
                   <span style="color:#ff0000;">android:id="@+id/fragment"</span>
                   <span style="color:#ff0000;">android:name="com.example.FrageMent.FrameMent"</span>
                   android:layout_height="wrap_content"
                   android:layout_width="wrap_content"></fragment>

添加FragMent的布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">

    <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/text1"
            ></TextView>
    <Button android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/staticbutton"/>
</LinearLayout>

FrameMent的JAVA类
public class FrameMent extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.staticfragment, container, false);
        TextView textView = (TextView) view.findViewById(R.id.text1);
        textView.setText("静态更新");
        return view;
    }
}

动态加载

1.调用动态加载

//主类,调用FrageMent
public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId){
                    case R.id.first :
                        Intent intent = new Intent(MyActivity.this,Activity2.class);
                        startActivity(intent);
                        break;
                    case R.id.second:
                        <span style="color:#ff0000;">FragMent2 fragMent2 = new FragMent2();
                        FragmentManager fragmentManager = getFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        fragmentTransaction.add(R.id.frag,fragMent2);</span>
<span style="color:#ff0000;"><span style="white-space: pre;">			</span>fragmentTransaction.addToBackStack(null);//<span style="font-family: Arial; font-size: 14px; line-height: 26px;">Fragment加入到回退栈</span>
                        fragmentTransaction.commit();</span>
                         break;                }
            }
        });
    }
}

2.动态加载Activity布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <fragment
            android:id="@+id/frag"
            android:name="com.example.FrageMent.FrameMent"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            ></fragment>
    <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:orientation="horizontal"
            >
        <RadioButton
                android:id="@+id/first"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/ic_launcher"
                android:gravity="center_horizontal"
                android:text="静态加载"
                /><span style="color:#993399;">
 </span>       <RadioButton
                android:id="@+id/second"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/ic_launcher"
                android:gravity="center_horizontal"
                android:text="动态加载"
                />
    </RadioGroup>
</LinearLayout><span style="color:#993399;">
</span>

3.FragMent布局文件和类都没有变


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值