zoukankan      html  css  js  c++  java
  • Android_Fragment

    Fragment:

       简单来说,Fragment其实可以理解为一个具有自己生命周期的控件,只不过这个控件又有点特殊,它有自己的处理输入事件的能力,有自己的生命周期,又必须依赖于Activity,能互相通信和托管。

    fragment VS activity
      1.Fragment是android 3.0后在出项的
      2.一个activity可以运行多个Fragment
      3.Fragment不能脱离activity独立存在
      4.activiy是屏幕的主体,Fragment只是activity的组成元素

    fragment和activity一样也具有生命周期,

      Fragment的生命周期是依赖于activity的,不能脱离activity而独立存在;

    1.fragment的静态加载

    直接在xml布局文件中进行加载

      <fragment
            android:id="@+id/listFragment"
            android:name="com.example.fragment_test.ListFragment"
            android:layout_width="204dp"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.151"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0"
            />

      android:name  属性是Fragment类所在的全路径

    2.Fragment的动态加载

      使用Java代码进行加载获取FragmentManager(获取管理器)----->beginTranaction(开启事物)---->add(添加Fragment)---->commit(提交)  不提交前面就白费了

      @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //动态加载fragment
    //      1.在MainActivity的布局文件中给Fragment提供一个布局容器
    //      2.创建Fragment对象
    //      3.将Fragment与容器关联
            ListFragment listFragment = new ListFragment();
            //添加
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.linear1,listFragment)
                    .commit();
         /*   getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.linear2,new ListFragment())//同一个Fragment 只能和一个容器相互关联,所里这里需要new一个
                    .commit();*/
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.linear2,ListFragment.instance("功夫足球"))
                    .commit();
            listFragment.setTextChange(this);
        }
    
            //移除remove 替换replace等方法
          /*  getSupportFragmentManager()
                    .beginTransaction()
                    .remove(listFragment)
                    .commit();*/

    3.Fragment的传值问题

      activity向fragment传递数据:使用setArgument()方法,将数据写入一个Bundle中进行传递

     public static ListFragment instance(String text){
            ListFragment listFragment = new ListFragment();
            Bundle bundle = new Bundle();
            bundle.putString("name",text);
            listFragment.setArguments(bundle);
            return  listFragment;
        }
      @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if(getArguments() != null){
                str = getArguments().getString("name");
            }
    
        }

    4.Fragment向activity传值

      主要是采用接口回调

    在Fragment类中创建一个借口,声明一个接口类型的成员属性,生成set()方法

      //创建set方法
        public void setTextChange(TextChange textChange) {
            this.textChange = textChange;
        }
        //定义变量
        TextChange textChange;
        //创建接口
        public interface TextChange{
            void change(String str);
        }

    将所要传递的数据利用参数进行传递,我这里传递的一个String类型的,

      if(textChange != null){
                        textChange.change("我是英俊的标题");
                    }

    最后让activity实现这个接口,并重写方法,这时候传进来的参数(str)就是传递的数据

       @Override
        public void change(String str) {
            setTitle(str);
        }
  • 相关阅读:
    poj 1067||hdu 1527 取石子游戏(博弈论,Wythoff Game)
    CGAffineTransform属性
    自定义UITabBarController
    UISwitch用法:
    UIButton常见属性和方法
    xcode添加背景音乐/音效
    使用cocoaPods加载框架的具体步骤:
    UILabel
    NSString -- UILabel中字体有多种颜色,字符串自动计算高度/换行
    xcode 修改类名 变量名
  • 原文地址:https://www.cnblogs.com/conglingkaishi/p/9346382.html
Copyright © 2011-2022 走看看