Fragement (一)
Fragment 有自己的生命周期
Fragment依赖于Activity
Fragment通过 getActivity() 可以获取所在的Activity;
Activity通过FragmentManager的 findFragmentById()或findFragmentByTag 获取Fragment
Fragment和Activity是多对多的关系
1. 创建装在Fragment的ContainerActivity
java 代码:ContainerActivity.class
public class ContainerActivity extends AppCompatActivity {
private AFragment aFragment;
private BFragment bFragment;
private Button btn_change;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
//实现点击按钮由A碎片转换到B碎片
btn_change = (Button) findViewById(R.id.btn_change);
btn_change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(bFragment == null){
bFragment=new BFragment();
}
getSupportFragmentManager().beginTransaction().replace(R.id.fl_container,bFragment).commitAllowingStateLoss();
}
});
//把AFragment添加到Activity中,记得调用commit
//R.id.fl_container:装在fragment的容器
//aFragment:要加载的fragment
aFragment=new AFragment();
getSupportFragmentManager().beginTransaction().add(R.id.fl_container,aFragment).commitAllowingStateLoss();
}
}
关键代码
1. 先实例化单个Fragment
2. 调用getSupportFragment方法里的beginTransaction()方法去向指定容器添加aFragament
3. 最后调用commitAllowingStateLoss()方法
//R.id.fl_container:装在fragment的容器
//aFragment:要加载的fragment
aFragment=new AFragment();
getSupportFragmentManager().beginTransaction().add(R.id.fl_container,aFragment).commitAllowingStateLoss();
xml 代码:activity_container.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_change"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="更换Fragment"/>
<FrameLayout
android:id="@+id/fl_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/btn_change"/>
</RelativeLayout>
2.创建Fragment(AFragment和BFragment完全相同)
java 代码: AFragment.class
public class BFragment extends Fragment {
private TextView mTvTitle;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//相当于activity的onCreate里的setcontentView,给它一个布局文件返回一个视图文件
View view=inflater.inflate(R.layout.fragment_b,container,false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
//当View创建完成后,这里有一个回调方法
super.onViewCreated(view, savedInstanceState);
mTvTitle=(TextView)view.findViewById(R.id.tv_title);
}
}
xml 代码:fragment_b.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="20sp"
android:text="我是BFragment"
android:gravity="center"/>
</LinearLayout>