Android 在 Android 3.0(API 11 级)中引入了Fragment,主要是为了给大屏幕(如平板电脑)上更加动态和灵活的 UI 设计提供支持。由于平板电脑的屏幕比手机屏幕大得多,因此可用于组合和交换 UI 组件的空间更大。利用片段实现此类设计时,您无需管理对视图层次结构的复杂更改。 通过将 Activity 布局分成片段,您可以在运行时修改 Activity 的外观,并在由 Activity 管理的返回栈中保留这些更改。
当然了我们普通手机开发也会加入这个Fragment, 我们可以把它看成一个小型的Activity,又称Activity片段!
例如:新闻应用可以使用一个片段在左侧显示文章列表,使用另一个片段在右侧显示文章—两个片段并排显示在一个 Activity 中,每个片段都具有自己的一套生命周期回调方法,并各自处理自己的用户输入事件。 因此,用户不需要使用一个 Activity 来选择文章,然后使用另一个 Activity 来阅读文章,而是可以在同一个 Activity 内选择文章并进行阅读,如下图中的左侧平板电脑布局所示。
我们应该将每个片段都设计为可重复使用的模块化 Activity 组件。也就是说,由于每个片段都会通过各自的生命周期回调来定义其自己的布局和行为,您可以将一个片段加入多个 Activity,因此,您应该采用可复用式设计,避免直接从某个片段直接操纵另一个片段。 这特别重要,因为模块化片段让您可以通过更改片段的组合方式来适应不同的屏幕尺寸。 在设计可同时支持平板电脑和手机的应用时,您可以在不同的布局配置中重复使用您的片段,以根据可用的屏幕空间优化用户体验。 例如,在手机上,如果不能在同一 Activity 内储存多个片段,可能必须利用单独片段来实现单窗格 UI。
下图是文档中给出的一个Fragment分别对应手机与平板间不同情况的处理图:
例如:仍然以新闻应用为例—在平板电脑尺寸的设备上运行时,该应用可以在Activity A 中嵌入两个片段。不过,在手机尺寸的屏幕上,没有足以储存两个片段的空间,因此Activity A 只包括用于显示文章列表的片段,当用户选择文章时,它会启动Activity B,其中包括用于阅读文章的第二个片段。因此,应用可通过重复使用不同组合的片段来同时支持平板电脑和手机,如上图右侧。
如需了解有关通过利用不同片段组合来适应不同屏幕配置这种方法设计应用的详细信息,请参阅支持平板电脑和手机指南。
很多时候我们都是直接重写Fragment,inflate加载布局完成相应业务了,子类用的不多,等需要的 时候在深入研究!
问题概述: 再引入Fragment声明时,
我们到底是使用android.app下的Fragment还是用的android.support.v4.app包下 的Fragment呢?
其实都可以,前面说过Fragment是Android 3.0(API 11)后引入的,那么如果开发的app需要 在3.0以下的版本运行呢?比如还有一点点市场份额的2.3!于是乎,v4包就这样应运而生了, 而最低可以兼容到1.6版本!至于使用哪个包看你的需求了,现在3.0下手机市场份额其实已经不多了,随街都是4.0以上的,7.0都出了,你说呢…所以这个时候,你可以直接使用app包下的Fragment 然后调用相关的方法,通常都是不会有什么问题的;如果你Fragment用了app包的, FragmentManager和FragmentTransaction都需要是app包的!要么用全部用app,要么全部用v4, 不然可是会报错的哦!当然如果你要自己的app对于低版本的手机也兼容的话,那么就可以选择用v4包!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是静态加载的Fragment"/>
LinearLayout>
package com.turing.base.activity.fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.turing.base.R;
public class FragmentOne extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_static_load,container,false);
return view;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/fragmentStaticLoad"
android:name="com.turing.base.activity.fragment.FragmentOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
RelativeLayout>
package com.turing.base.activity.fragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.turing.base.R;
/**
* 静态加载Fragment的Activity
*/
public class FragmentStaticLoadAct extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_static_load);
//静态加载时可以直接获取到 Fragment中的UI控件
TextView tv = (TextView) findViewById(R.id.textview);
tv.setText("我在Act中获取到了Fragment中的UI控件");
}
}
实现动态加载,我们需要先了解Fragment事务。
Fragment事务:对Fragment进行添加、移除、替换或执行其它动作,提交给Activity的每一个变化。
Fragment是UI模块,自然在一个Activity中可以不只有一个模块,所以Android提供了FragmentManage类来管理Fragment,FragmentTransaction类来管理事务。
我们对Fragment的动态加载就是先将添加、移除等操作提交到事务,然后通过FragmentManage完成的。
通过FragmentManager.beginTransaction()
我们可以开始一个事务。在事务中,我们可以对Fragment进行的操作以及对应的方法如下:
如果允许用户通过back键退回到前一个Fragment状态,调用commit()之前可以加入addToBackStack()方法
我们需要注意的是,Fragment以ID或Tag作为唯一标识,所以remove和replace的参数是Fragment,这个Fragment目标Fragment一致
注意:Activity动态的添加Fragment必需有一个容器View来容纳Fragment的layout布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/fragmentDynamicLoad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
RelativeLayout>
package com.turing.base.activity.fragment.dynamicload;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.widget.TextView;
import com.turing.base.R;
public class FragmentDynamicLoadAct extends AppCompatActivity {
// 屏幕宽高
int width, height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_dynamic_load);
// 获取屏幕的宽高
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
width = displayMetrics.widthPixels;
height = displayMetrics.heightPixels;
// 根据宽高,加载不同的Fragment
if (width < height) {
FragementFirst fragementFirst = new FragementFirst();
// 使用V4的包就使用getSupportFragmentManager ,使用app下的,就使用getFragmentManager
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragmentDynamicLoad, fragementFirst)
.commit();
} else {
FragmentSecond fragmentSecond = new FragmentSecond();
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragmentDynamicLoad, fragmentSecond)
.commit();
}
//当fragment被提交之后,【fragmentTransaction.commit()提交fragment是异步处理的,所以获取fragment时要注意】
// 可通过以下两种方法获取fragment:findFragmentByTag()、findFragmentById()
}
/**
* 重写onStart()方法,
* 因为从fragment的生命周期可以知道当Activity的onCreate(Bundle savedInstanceState)中
* 还无法获取fragment的布局的组件
*/
@Override
protected void onStart() {
super.onStart();
/**
* 可以直接通过findViewById()获取fragment的组件,
* 因为fragment本身就是Activity的一部分(“碎片”/“片段”);
* 因为Activity和fragment要从fragment的onActivityCreate()生命周期方法之后
* 才能相互获取对方布局中的组件,
* 所以在fragment中获取Activity的组件最早只能在onActivityCreate()中获取,
* 而Activity最早只能在onStart()中获取;
*/
// 获取Fragment中的UI组件
if (width < height) {
TextView textView = (TextView) findViewById(R.id.fragmentFirst);
textView.setText("~~~~~First");
} else {
TextView textView = (TextView) findViewById(R.id.fragmentSecond);
textView.setText("~~~~~Second");
}
}
}
package com.turing.base.activity.fragment.dynamicload;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.turing.base.R;
/**
* A simple {@link Fragment} subclass.
*/
public class FragementFirst extends Fragment {
public FragementFirst() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragement_first, container, false);
}
}
<FrameLayout 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.turing.base.activity.fragment.dynamicload.FragementFirst">
<TextView
android:id="@+id/fragmentFirst"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="fragment first" />
FrameLayout>
package com.turing.base.activity.fragment.dynamicload;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.turing.base.R;
/**
* A simple {@link Fragment} subclass.
*/
public class FragmentSecond extends Fragment {
public FragmentSecond() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_second, container, false);
}
}
<FrameLayout 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.turing.base.activity.fragment.dynamicload.FragmentSecond">
<TextView
android:id="@+id/fragmentSecond"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="fragment second" />
FrameLayout>
当fragment被提交之后,【fragmentTransaction.commit()提交fragment是异步处理的,所以获取fragment时要注意】 可通过以下两种方法获取fragment:findFragmentByTag()、findFragmentById()
/**
* 重写onStart()方法,
* 因为从fragment的生命周期可以知道当Activity的onCreate(Bundle savedInstanceState)中
* 还无法获取fragment的布局的组件
*/
@Override
protected void onStart() {
super.onStart();
/**
* 可以直接通过findViewById()获取fragment的组件,
* 因为fragment本身就是Activity的一部分(“碎片”/“片段”);
* 因为Activity和fragment要从fragment的onActivityCreate()生命周期方法之后
* 才能相互获取对方布局中的组件,
* 所以在fragment中获取Activity的组件最早只能在onActivityCreate()中获取,
* 而Activity最早只能在onStart()中获取;
*/
// 获取Fragment中的UI组件
if (width < height) {
TextView textView = (TextView) findViewById(R.id.fragmentFirst);
textView.setText("~~~~~First");
} else {
TextView textView = (TextView) findViewById(R.id.fragmentSecond);
textView.setText("~~~~~Second");
}
}
public class FragementFirst extends Fragment {
public FragementFirst() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragement_first, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 在Fragment中获取Activity的组件
TextView textView = (TextView) getActivity().findViewById(R.id.id_tv_actUI);
textView.setText("FFFF");
}
}
在Activity中创建Bundle数据包,调用Fragment实例的setArguments(bundle) 从而将Bundle数据包传给Fragment,然后Fragment中调用getArguments获得 Bundle对象,然后进行解析就可以了
fragementFirst = new FragementFirst();
// 使用V4的包就使用getSupportFragmentManager ,如果使用app下的Fragment,就使用getFragmentManager
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragmentDynamicLoad, fragementFirst)
.commit();
// Activity传递数据给Fragment
Bundle bundle = new Bundle();
bundle.putString("key", "这是Activity传递给Fragment的数据");
// setArguments
fragementFirst.setArguments(bundle);
在Fragment中接收解析数据
// 接收Activity传递过来的数据
Bundle bundle = getArguments();
Toast.makeText(getActivity(), bundle.getString("key"), Toast.LENGTH_SHORT).show();
在Fragment中定义一个内部回调接口,再让包含该Fragment的Activity实现该回调接口, Fragment就可以通过回调接口传数据了。
FragementFirst.java
/**
* 定义一个回调接口:(Fragment中)
*/
public interface FragmentCallBack {
//定义一个接口方法
void getResult(String result);
}
FragementFirst.java getData改方法持有接口对象
/**
* 接口回调(Fragment中)
*/
public void getData(FragmentCallBack callBack){
// 模拟获取的数据
String msg = "小工匠";
// 接口回调
callBack.getResult(msg);
}
//使用接口回调方法读数据(Activity中)
fragementFirst.getData(new FragementFirst.FragmentCallBack() {
@Override
public void getResult(String result) {
Toast.makeText(FragmentDynamicLoadAct.this, result, Toast.LENGTH_SHORT).show();
}
});
->在Fragment定义一个接口,接口中定义抽象方法,你要传什么类型的数据参数就设置为什么类型; ->接着还有写一个调用接口中的抽象方法,把要传递的数据传过去 ->再接着就是Activity了,调用Fragment提供的那个方法,然后重写抽象方法的时候进行数据 的读取就可以了~
找到要接受数据的fragment对象,直接调用setArguments传数据进去就可以了 通常的话是replace时,即fragment跳转的时候传数据的,那么只需要在初始化要跳转的Fragment 后调用他的setArguments方法传入数据即可! 如果是两个Fragment需要即时传数据,而非跳转的话,就需要先在Activity获得f1传过来的数据, 再传到f2了,就是以Activity为媒介~
FragmentManager fManager = getSupportFragmentManager( );
FragmentTransaction fTransaction = fManager.beginTransaction();
Fragmentthree t1 = new Fragmentthree();
Fragmenttwo t2 = new Fragmenttwo();
Bundle bundle = new Bundle();
bundle.putString("key",id);
t2.setArguments(bundle);
fTransaction.add(R.id.fragmentRoot, t2, "~~~");
fTransaction.addToBackStack(t1);
fTransaction.commit();