Android Fragment基本应用

Fragment用于在一个activity中描述一些行为或界面,可以在多个activity中重用fragment

Fragment生命周期
受到所在的activity的生命周期影响

运行过程
添加Fragment->onAttach()->onCreate()->^onCreateView->onActivityCreated->onStart->onResume->运行状态->
1、用户返回或Fragment被移除或替换->onPause->暂停状态->onStop->停止状态->onDestoryView->onDestory->onDetach->销毁状态
2、Fragment被添加到返回栈然后被移除/替换->onPause->暂停状态->onStop->停止状态->onDestoryView->^

创建Fragment
1、创建Fragment的子类,也可以让其继承自一个已经存在的Fragment子类
2、重写onCreateView方法,用于指定要显示的界面

在这里插入图片描述

在这里插入图片描述
3、在Activity中添加Fragment
a)直接在布局文件中添加Fragment

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<fragment
    android:id="@+id/fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.example.fragment1.ListFragment"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imgbnt1"
            android:layout_width="0dp"
            android:layout_height="150dp"
            android:layout_weight="1"
            android:src="@mipmap/img1"/>
        <ImageView
            android:id="@+id/imgbnt2"
            android:layout_width="0dp"
            android:layout_height="150dp"
            android:layout_weight="1"
            android:src="@mipmap/img2"/>
    </LinearLayout>
</RelativeLayout>

b)当Activity运行时添加Fragment

4、实现fragment的切换

public class MainActivity extends AppCompatActivity {

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

        ImageView imageView1 = (ImageView) findViewById(R.id.imgbnt1);
        ImageView imageView2 = (ImageView) findViewById(R.id.imgbnt2);

        imageView1.setOnClickListener(l);//添加监听器
        imageView2.setOnClickListener(l);

    }
        View.OnClickListener l = new View.OnClickListener() {//单击事件监听器
            @Override
            public void onClick(View v) {
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();//获取Fragment的Transaction实例
                Fragment f = null;
                switch (v.getId()){
                    case R.id.imgbnt1:
                        f = new ListFragment();//如果单击1,就创建对应的fragment
                        break;
                    case R.id.imgbnt2:
                        f = new DetailFragment();
                        break;
                        default:
                            break;
                }
                ft.replace(R.id.fragment,f);//替换每次所显示的fragment
                ft.commit();
            }
    };
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值