Frament

Frament的介绍

    可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的生命周期,单独处理自己的输入,在Activity运行的时候可以加载或者移除Fragment模块。

  可以把Fragment设计成可以在多个Activity中复用的模块。

Frament的生命周期

因为Fragment必须嵌入在Acitivity中使用,所以Fragment的生命周期和它所在的Activity是密切相关的。

  如果Activity是暂停状态,其中所有的Fragment都是暂停状态;如果Activity是stopped状态,这个Activity中所有的Fragment都不能被启动;如果Activity被销毁,那么它其中的所有Fragment都会被销毁。

  但是,当Activity在活动状态,可以独立控制Fragment的状态,比如加上或者移除Fragment。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.example.android_fragment.MainActivity">

   <fragment
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:id="@+id/res_left"
    android:name="com.example.android_fragment.fragmentLeft"
    ></fragment>
    <fragment
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:id="@+id/f_main_right"
        android:name="com.example.android_fragment.fragmentRigth"
        ></fragment>
</LinearLayout>
把这个View一分为二


 android:name="com.example.android_fragment.fragmentLeft"
这句代码是引用了一个View

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.android_fragment.fragmentLeft">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/res_left_phone"
        ></ListView>
</LinearLayout>
 android:name="com.example.android_fragment.fragmentRigth"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.android_fragment.fragmentRigth">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/res_rigth_phone"
    ></TextView>
</LinearLayout>
最终结果是这样的你选择谁就调用谁的界面


代码如下:

       选择一个的用时就会调用一个View视图,我在这里简单的写了四个

package com.example.android_fragment;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class fragmentLeft extends Fragment {

    private ListView res_left_phone;
    private String[] list={"黄1","黄2","黄3","黄4"};
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.activity_fragment_left,null);
        res_left_phone = (ListView) view.findViewById(R.id.res_left_phone);
        ArrayAdapter aa=new ArrayAdapter(getContext(),android.R.layout.simple_list_item_1,list);
        res_left_phone.setAdapter(aa);
        res_left_phone.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //情况:右边是不同的布局
                //获取碎片的管理者
               FragmentManager fm=getFragmentManager();
                //获取事务
                FragmentTransaction ft=fm.beginTransaction();

                switch (position) {
                    case 0:
                        WjFragment wjFragment=new WjFragment();//这是new了自己写的和他相对应的View视图
                        //替换
                        ft.replace(R.id.f_main_right,wjFragment) ;
                        break;
                    case 1:
                        BgFragment bgFragment=new BgFragment();//这是new了自己写的和他相对应的View视图

                        ft.replace(R.id.f_main_right,bgFragment) ;
                        break;
                    case 2:
                        XxFragment xxFragment=new XxFragment();//这是new了自己写的和他相对应的View视图

                        ft.replace(R.id.f_main_right,xxFragment) ;
                        break;
                    case 3:
                        SmallSisterFragment smallSisterFragment=new SmallSisterFragment();//这是new了自己写的和他相对应的View视图

                        ft.replace(R.id.f_main_right,smallSisterFragment) ;
                        break;
                }
                ft.commit();
            }
        });
        return view;
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值