引用了:http://www.runoob.com/w3cnote/android-tutorial-fragment-base.html
目录
0.前言
1.生命周期
2.android.app.Fragment
2.android.support.v4.app.Fragment
3.实战(底部导航栏)
前言
问题1:fragment干什么用的?
Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑
而使用Fragment 我们可以把屏幕划分成几块,然后进行分组,进行一个模块化的管理!从而可以更加方便的在 运行过程中动态地更新Activity的用户界面
现在很多页面底部导航栏用到fragment
问题2:Fragment & v4.app.Fragment?为什么有两个?
- Android 3.0(api11)之后引入的fragment,之前的无法兼容,产生了v4.app.Fragment.*
使用区别:
- 如果你使用了v4包下的Fragment,那么所在的那个Activity就要继承FragmentActivity
- getFragmentManager( )改成getSupportFragmentManager( )
生命周期
Android.app.fragment
one.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:layout_width="match_parent"
android:id="@+id/fragment"
android:name="com.example.rtyui.androidteach.FragmentApp"
android:layout_height="match_parent"/>
</RelativeLayout>
fragment_three.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="fragment_three"
android:gravity="center"/>
</LinearLayout>
class FragmentApp
public class FragmentApp extends Fragment {
private View root;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_three, container, false);
return root;
}
}
android.support.v4.app.Fragment
xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:id="@+id/fragment"
android:layout_height="match_parent"/>
</RelativeLayout>
Activity
public class One extends FragmentActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
Fragment fragment = new FragmentThree();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment, fragment).commit();
}
}
实战(底部导航栏)
activity xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/btn"
android:text="跳转"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0px"
android:id="@+id/top"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0px"
android:layout_weight="1"
android:id="@+id/btn_one"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:src="@mipmap/ic_launcher"
android:padding="5dp"
android:layout_height="40dp" />
<TextView
android:layout_width="match_parent"
android:text="底部1"
android:gravity="center"
android:layout_height="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0px"
android:layout_weight="1"
android:orientation="vertical"
android:id="@+id/btn_two"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:src="@mipmap/ic_launcher"
android:padding="5dp"
android:layout_height="40dp" />
<TextView
android:layout_width="match_parent"
android:text="底部2"
android:gravity="center"
android:layout_height="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0px"
android:layout_weight="1"
android:id="@+id/btn_three"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:src="@mipmap/ic_launcher"
android:padding="5dp"
android:layout_height="40dp" />
<TextView
android:layout_width="match_parent"
android:text="底部3"
android:gravity="center"
android:layout_height="20dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
结构,代码都是类似的
activity class
package com.example.rtyui.androidteach;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
public class Main extends FragmentActivity {
private Fragment fragmentOne = null;
private Fragment fragmentTwo = null;
private Fragment fragmentThree = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fragmentOne = new FragmentOne();
fragmentTwo = new FragmentTwo();
fragmentThree = new FragmentThree();
getSupportFragmentManager().beginTransaction()
.add(R.id.top, fragmentOne)
.add(R.id.top, fragmentTwo)
.add(R.id.top, fragmentThree).commit();
showFrag(1);
findViewById(R.id.btn_one).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showFrag(1);
}
});
findViewById(R.id.btn_two).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showFrag(2);
}
});
findViewById(R.id.btn_three).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showFrag(3);
}
});
}
private void showFrag(int sign){
switch (sign){
case 1:
getSupportFragmentManager().beginTransaction().hide(fragmentTwo).hide(fragmentThree).show(fragmentOne).commit();
break;
case 2:
getSupportFragmentManager().beginTransaction().hide(fragmentOne).hide(fragmentThree).show(fragmentTwo).commit();
break;
case 3:
getSupportFragmentManager().beginTransaction().hide(fragmentOne).hide(fragmentTwo).show(fragmentThree).commit();
break;
}
}
}