Android开发(五)--- Fragment

这两天在学Fragment,刚开始感觉还行,但是渐渐深入起来,感觉Fragment学起来挺难的。Fragment必须要和Activity一起使用,所以他们的生命周期都息息相关。

生命周期:

 这张图最能形像的说明Activity和Fragment方法之间的先后执行顺序。

每当创建一个Fragment时,首先执行的是Attach,onCreate,onCreateView这三个方法:

主要方法的作用是:

  • Attach:Fragment和Activity建立关联的时候调用(获得activity的传递的值)
  • onCreateView方法:为Fragment创建视图(加载布局)时调用(给当前的fragment绘制UI布局,可以使用线程更新UI)
  • onActivityCreated方法:当Activity中的onCreate方法执行完后调用(表示activity执行oncreate方法完成了的时候会调用此方法)
  • onDestroyView方法:Fragment中的布局被移除时调用(表示fragment销毁相关联的UI布局)
  • onDetach方法:Fragment和Activity解除关联的时候调用(脱离activity)

如何使用:

Fragment的使用方式有两种,一种是静态的使用方法,一种是动态的使用方法。

静态的使用方法

如下面代码所示:

 <fragment
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/content1"
        android:tag="content1"
        android:name="com.example.yzg.fragmentpassvalue.AFragment"
        >
    </fragment>

注解:

name:是指引用的Fragment这个类文件

tag:这是Fragment的一个标志,Activity中的FragmentManager可以通过Tag找到相应的Fragment

动态的方法;

如下面的代码所示:
 

FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.content1,new AFragment());

注释:

在代码中利用布局管理器添加Fragment,add中的参数代表要添加到哪个组件中的ID和要添加的Fragment,但是一个最最最最重要的注意点,第一个参数的组件一定不能是fragment,这样的话,编辑器一定会报错!!!!!,我找了这个错误半天,累死我了!!!

第一种方式虽然简单但灵活性不够。添加Fragment到Activity的布局文件当中,就等同于将Fragment及其视图与activity的视图绑定在一起,且在activity的生命周期过程中,无法切换fragment视图。

第二种方式比较复杂,但也是唯一一种可以在运行时控制fragment的方式(加载、移除、替换)。

这两种方法介绍完之后,下面开始一个具体的实例:

需求如下:

定义一个界面,两个上下的Fragment,第一Fragment中有一个居中的按钮,第二个Fragment中有一个居中平的TextView,每点击第一个的Button,第二个中的字会相应的发生改变。

解答:

这个问题看着很简单,实则是比较困难的,因为他牵扯到,传值之间的问题,首先Activity要当一个中间人,负责传递两个Fragment之间的信息。

具体的代码如下:

总布局如下:

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

    <fragment
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/content1"
        android:tag="content1"
        android:name="com.example.yzg.fragmentpassvalue.AFragment"
        >
    </fragment>

    <fragment
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/content2"
        android:tag="content2"
        android:name="com.example.yzg.fragmentpassvalue.BFragment"
        >

    </fragment>


</LinearLayout>

MainActivity.java:

package com.example.yzg.fragmentpassvalue;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

    BFragment content2;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获得Fragment的管理器
        FragmentManager fragmentManager = getFragmentManager();
        //利用tag找到相应的Fragment  但是有一个注意点  就是要进行相应类型的转换
        AFragment content1 = (AFragment) fragmentManager.findFragmentByTag("content1");


        content1.setActivity(this);
        content2 = (BFragment) fragmentManager.findFragmentByTag("content2");

    }

    public void setText(int index){

        content2.setTextView(index);

    }
}

 

AFragment布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF9"
    tools:context="com.example.yzg.fragmentpassvalue.AFragment">

    <!-- TODO: Update blank fragment layout -->
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击按钮"
        android:layout_gravity="center"
        />

</FrameLayout>

BFragment布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F99"
    tools:context="com.example.yzg.fragmentpassvalue.BFragment">

    <!-- TODO: Update blank fragment layout -->

        <TextView
            android:id="@+id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="hhhhhhhhhh"
            android:textSize="22sp"
            android:gravity="center"
            />
</FrameLayout>

AFragment.java

package com.example.yzg.fragmentpassvalue;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


public class AFragment extends Fragment {

    Button button;
    MainActivity mainActivity;
    int index = 0;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_a, null);
        button = inflate.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index++;
                mainActivity.setText(index);

            }
        });
        return inflate;
    }

    public void setActivity(MainActivity mainActivity){
        this.mainActivity  = mainActivity;
    }


}

 

BFragment.java

package com.example.yzg.fragmentpassvalue;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class BFragment extends Fragment  {

    TextView textView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_b, null);
        textView  = inflate.findViewById(R.id.text1);
        return inflate;
    }

    public void setTextView(int index){
        textView.setText("点击了第"+index+"次数");
    }
}

总结:

对于每一个问题都需要抱着一颗学习的心态,不能马马虎虎的去学习,这样的话,对于个知识点学的不精,不是自己想要的。

要理解Fragment生命周期中主要方法的作用,学会他们之间是如何传值的。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值