Fragment详解(一)
初识Fragment
- Fragment有自己的生命周期
- Fragment依赖于Activity
- Fragment通过getActivity获取所在的Activity;Activity通过FragmentManager的findFragmentById()或findFragmentByTag()获取Fragment
- Fragment和Activity是多对多的关系
Fragment显示在Activity的设计思路
- 创建显示的Activity类
- 创建Fragment类
- 在Activity的布局文件布局FrameLayout(用以存放fragment)
- 在Fragment的布局文件布局Fragment要显示的内容
- 连接Fragment到Activity
示例代码
Fragment的java文件
package com.example.administrator.exercise.fragment;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
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.TextView;
import com.example.administrator.exercise.R;
import org.w3c.dom.Text;
public class FragmentA extends android.app.Fragment {
private TextView tv;
//返回一个视图文件,相当于setContextView一样
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a,container,false); //参数:fragment的布局文件,container,false
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tv = view.findViewById(R.id.fm_tv);
}
}
Fragment的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_fragment_a"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
tools:context="com.example.administrator.exercise.fragment.FragmentA">
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="跳转Fragment"
android:textSize="16sp"
android:id="@+id/container_bt"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是Afragment"
android:textSize="16sp"
android:gravity="center"
android:id="@+id/fm_tv"/>
</RelativeLayout>
activity的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.exercise.fragment.ContainerActivity">
<FrameLayout
android:id="@+id/container_fl"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
</RelativeLayout>
activity的java文件
package com.example.administrator.exercise.fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import com.example.administrator.exercise.R;
public class ContainerActivity extends AppCompatActivity {
Button bt_jump;
private FragmentA Afragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
bt_jump = (Button) findViewById(R.id.container_bt);
//实例化Afragment
Afragment = new FragmentA();
//把Afragment添加到Activity中,记得调用commit
getFragmentManager().beginTransaction().add(R.id.container_fl,Afragment).commitAllowingStateLoss();
}
}