使用多个Fragment可以在一个单独的Activity中建立多个UI面板,也可以在多个Activity中重用Fragment
Fragment 生命周期
onAttach() 在Fragment 和 Activity 建立关联是调用(Activity 传递到此方法内)
onCreateView() 当Fragment 创建视图时调用
onActivityCreated() 在相关联的 Activity 的 onCreate() 方法已返回时调用。
onDestroyView() 当Fragment中的视图被移除时调用
onDetach() 当Fragment 和 Activity 取消关联时调用。
创建Fragment
要创建一个Fragment,必须创建一个Fragment的子类,或者继承另一个已经存在Fragment的子类,例如创建一个NewFragment 的Fragment 并重写OnCreateView()方法
public class NewsFragment extents Fragment{
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
// 从布局文件new.xml 加载一个布局文件
View v = inflater.inflate(R.layout.news,container,false);
return v;
}
}
在Activity 中添加Fragment
向Activity中添加Fragment有两种方法:一种是直接在布局文件中添加,将Fragment作为Ativity整个布局的一部分,另一种是当Activity运行时,将Fragment放入Activity布局中,
1.直接在布局文件中添加Fragment,使用<fragment></fragment> 标记实现,例如,要在一个布局文件中添加两个Fragment
<?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">
<fragment
android:name="com.example.testapplication.TestFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
></fragment>
<fragment
android:name="com.example.testapplication.ListFragment"
android:id="@+id/list2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
></fragment>
</LinearLayout>
注意:在<fragment></fragment> android:name 属性用于指定要添加的Fragment
2.当Activity运行时添加Fragment
实现方法时:首先获取一个FragmentTransaction的实列:然后使用add() 方法添加一个Framgent,其中add方法的第一个参数是Fragment要放入的ViewGroup(由ResourceID)指定
,第二个参数是需要添加的Fragment;最后为了是改变,还必须调用commit() 方法提交事务,
DetailFramgent details = new DetailFragment(); // 获取实例化对象
FragmentTransaction ft = getFragmentManager().beginTransaction(); // 获取一个实列
ft.add(android.R.id.content,details); // 添加一个显示详细内容的Fragment
ft.commit(); // 提交事务
案例 底部导航栏
package com.example.testapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
public class FragmentDemo extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragdemo);
// 获取布局文件的第一个导航图片
ImageView imageView1 = (ImageView) findViewById(R.id.image1);
// 获取布局文件的第二个导航图片
ImageView imageView2 = (ImageView) findViewById(R.id.image2);
// 获取布局文件的第3个导航图片
ImageView imageView3 = (ImageView) findViewById(R.id.image3);
// 获取布局文件的第4个导航图片
ImageView imageView4 = (ImageView) findViewById(R.id.image4);
imageView1.setOnClickListener(l); // 为第一个导航图片添加单机事件
imageView2.setOnClickListener(l); // 为第一个导航图片添加单机事件
imageView3.setOnClickListener(l); // 为第一个导航图片添加单机事件
imageView4.setOnClickListener(l); // 为第一个导航图片添加单机事件
}
View.OnClickListener l = new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm = getSupportFragmentManager(); // 获取Fragment
FragmentTransaction ft = fm.beginTransaction(); // 开启一个事务
Fragment f = null; // 为Fragment初始化
switch (view.getId()){ // 通过获取点击的id判断点击了那个图片
case R.id.image1:
f = new WeChat_Fragment(); // 创建第一个Fragment
break;
case R.id.image2:
f = new Message_Fragment(); //创建第一个Fragment
break;
case R.id.image3:
f = new Find_Fragment();
break;
case R.id.image4:
f = new Me_Fragment();
break;
}
ft.replace(R.id.fragment,f); // 替换Fragment
ft.commit(); // 提交事务
}
};
}
<?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-->
<fragment
android:id="@+id/fragment"
android:name="com.example.testapplication.WeChat_Fragment"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
>
<!--微信图标-->
<ImageView
android:id="@+id/image1"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/bottom_1"
/>
<ImageView
android:id="@+id/image2"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/bottom_2"
/>
<ImageView
android:id="@+id/image3"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/bottom_3"
/>
<ImageView
android:id="@+id/image4"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/bottom_4"
/>
</LinearLayout>
</RelativeLayout>
Fragment
package com.example.testapplication;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class Message_Fragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.message_fragment,null);
return view;
}
}
<?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">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/faxian"
android:scaleType="fitXY"/>
</RelativeLayout>