例如,新闻应用可以使用一个片段在左侧显示文章列表,使用另一个片段在右侧显示文章 — 两个片段并排显示在一个 Activity 中,每个片段都具有自己的一套生命周期回调方法,并各自处理自己的用户输入事件。 因此,用户不需要使用一个 Activity 来选择文章,然后使用另一个 Activity 来阅读文章,而是可以在同一个 Activity 内选择文章并进行阅读,如下图中的平板电脑布局所示(图片来源android官方文档)。
Fragment的生命周期
Fragment的生命周期直接受Activity的生命周期影响
创建Fragment要创建一个类继承Fragment就可以了,通过重写onCreateView()方法可以加载Fragment需要加载的布局
Fragment的静态使用:
MainActivity.java
package com.example.createfragment_01;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
/**
* 静态使用Fragment
* 步骤:
* 1.首先创建一个Fragment类继承于Fragment,重写onCreateView()方法,设置Fragment的布局
* 2.再到Activity中声明Fragment,使用方式与view相同
*
* 例子介绍:2个Fragment构成一个Activity的布局,一个是标题fragment,一个是内容Fragment
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 去除原有的标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
}
TitleFragment.java
package com.example.createfragment_01.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.example.createfragment_01.R;
/**
* 创建和使用Fragment的步骤:
* 1.创建子类继承Fragment
* 2.重写onCreateView()方法,该方法主要定义Fragment的布局,以view对象的形式返回fragment的视图
* 3.将Fragment引入到Activity中
*/
public class TitleFragment extends Fragment {
/**
* onCreateView()表示Fragment第一次创建绘制用户界面时系统回调的方法
* 返回值view:表示当前加载的fragment视图,如果fragment不提供视图可以返回null
* 参数:
* LayoutInflater inflater:表示布局加载器或者填充器,将xml文件转化成view对象
* ViewGroup container:表示当前fragment插入Activity的布局视图对象
* Bundle savedInstanceState:表示储存上一个fragment的信息
*/
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
// 表示将指定资源的xml文件转换成具体的view对象,inflate(表示加载xml文件的资源id,null)
View view = inflater.inflate(R.layout.fragment_title,null);
RelativeLayout relativeLayout = view.findViewById(R.id.rl_layout);
relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "我是标题栏", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
fragment_title.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#000000">
<ImageView
android:id="@+id/iv_back"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_centerVertical="true"
android:paddingLeft="15dp"
android:scaleType="centerCrop"
android:src="@drawable/ic_chevron_left_white_24dp" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/title"
android:textColor="#fcfbfb"
android:textSize="18sp" />
<ImageView
android:id="@+id/iv_menu"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:paddingRight="15dp"
android:scaleType="centerCrop"
android:src="@drawable/ic_more_vert_white_24dp" />
</RelativeLayout>
ContentFragment.java
package com.example.createfragment_01.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.createfragment_01.R;
public class ContentFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_content,null);
return view;
}
}
fragment_content.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">
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content"
android:textSize="30sp"
android:gravity="center"
android:textStyle="bold"/>
</LinearLayout>
activity_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.createfragment_01.MainActivity">
<fragment
android:id="@+id/fragment_title"
android:name="com.example.createfragment_01.fragment.TitleFragment"
android:layout_width="match_parent"
android:layout_height="50dp" />
<fragment
android:id="@+id/fragment_content"
android:name="com.example.createfragment_01.fragment.ContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/fragment_title" />
</RelativeLayout>
效果图
Fragment的动态使用及动态切换:
MainActivity.java
package com.example.fragmentchange;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import com.example.fragmentchange.fragment.ChatFragment;
import com.example.fragmentchange.fragment.ContactsFragment;
import com.example.fragmentchange.fragment.DiscoverFragment;
import com.example.fragmentchange.fragment.MeFragment;
/**
* 动态添加fragment
* 具体4个步骤
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FragmentManager manager;
private FragmentTransaction transaction;
private RadioButton rb_chat,rb_contacts,rb_discover,rb_me;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
// 1.创建Fragment的管理器对象
manager = getFragmentManager();
// 2.获取Fragment的事务对象并且开启事务
transaction = manager.beginTransaction();
// 3.调用事务中相应的动态操作Fragment的方法执行 add(表示动态添加位置的资源id,表示添加的fragment对象)
// 设置默认为微信页面
transaction.add(R.id.ll_content,new ChatFragment()); // 将ChatFragment动态添加到ll_content的位置
// transaction.remove(new ChatFragment()); // remove(需要移除的fragment对象)
// transaction.replace(arg0,arg1); // replace(表示被替换fragment的资源id,表示替换者fragment对象)
// 4.提交事务
transaction.commit();
}
// 初始化视图
public void initView(){
rb_chat = findViewById(R.id.rb_chat);
rb_contacts = findViewById(R.id.rb_contacts);
rb_discover = findViewById(R.id.rb_discover);
rb_me = findViewById(R.id.rb_me);
rb_chat.setOnClickListener(this);
rb_contacts.setOnClickListener(this);
rb_discover.setOnClickListener(this);
rb_me.setOnClickListener(this);
}
/**
* 点击RadioButton触发的事件
*/
@Override
public void onClick(View v) {
// 这里的事务创建必不可少(否则程序点击就会崩溃)
transaction = manager.beginTransaction();
switch (v.getId()){
case R.id.rb_chat:
transaction.replace(R.id.ll_content,new ChatFragment());
break;
case R.id.rb_contacts:
transaction.replace(R.id.ll_content,new ContactsFragment());
break;
case R.id.rb_discover:
transaction.replace(R.id.ll_content,new DiscoverFragment());
break;
case R.id.rb_me:
transaction.replace(R.id.ll_content,new MeFragment());
break;
}
transaction.commit();
}
}
activity_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.fragmentchange.MainActivity">
<!--展示内容界面-->
<LinearLayout
android:id="@+id/ll_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<!--展示切换标签-->
<LinearLayout
android:id="@+id/ll_bottom"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<RadioGroup
android:id="@+id/rg_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f7e3e3"
android:orientation="horizontal">
<!--button的@null是去除默认的圆圈(@不能忘)-->
<RadioButton
android:id="@+id/rb_chat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:textSize="15sp"
android:button="@null"
android:drawableTop="@drawable/ic_chat_bubble_outline_green_a700_24dp"
android:gravity="center"
android:text="@string/chat" />
<RadioButton
android:id="@+id/rb_contacts"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:textSize="15sp"
android:button="@null"
android:gravity="center"
android:drawableTop="@drawable/ic_contacts_green_a700_24dp"
android:text="@string/contacts" />
<RadioButton
android:id="@+id/rb_discover"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:button="@null"
android:textSize="15sp"
android:drawableTop="@drawable/ic_discover_green_a700_24dp"
android:gravity="center"
android:text="@string/discover" />
<RadioButton
android:id="@+id/rb_me"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:button="@null"
android:textSize="15sp"
android:drawableTop="@drawable/ic_me_green_a700_24dp"
android:gravity="center"
android:text="@string/me" />
</RadioGroup>
</LinearLayout>
</RelativeLayout>
CharFragment.java
package com.example.fragmentchange.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.fragmentchange.R;
public class ChatFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_chat,null);
}
}
fragment_chat.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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tv_chat"
android:text="@string/chat_page"
android:gravity="center"
android:textSize="20sp"/>
</RelativeLayout>
剩下的contacts,discover,me跟chat的内容基本都一样
上效果图:
v4包下的Fragment使用:
Fragment是android3.0之后引入的UI组件,为了兼容sdk3.0之前的版本,就出现了supportV4包。
1.导包的时候记得都必须导入v4包下的fragment
2.动态引入fragment时Activity要继承FragmentActivity
3.获取Fragment管理器对象就要是getSupportFragmentManager()