手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android学习之Fragment
Android学习之Fragment
摘要:Fragment是什么碎片(Fragment)是一种可以嵌入在活动(activity)当中的UI片段。一、碎片的简单用法创建两个布局文件:/...

Fragment 是什么

碎片(Fragment)是一种可以嵌入在活动(activity)当中的 UI 片段。

一、碎片的简单用法

创建两个布局文件:

<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:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Button" /> </LinearLayout> //left_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textSize="20sp" android:text="This is right fragment" /> </LinearLayout> //right_fragment.xml

所有的自定义 Fragment 都需要继承 Fragment 类:

public class LeftFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.left_fragment, container, false); return view; } } public class RightFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.right_fragment, container, false); return view; } }

最后定义 activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <fragment android:id="@+id/left_fragment" android:name="com.example.fragmenttest.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/right_fragment" android:name="com.example.fragmenttest.RightFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout>

二、动态添加碎片

可以在代码当中动态添加碎片:

@Override public void onClick(View v) { switch (v.getId()) { case R.id.button: AnotherRightFragment fragment = new AnotherRightFragment(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager. beginTransaction(); transaction.replace(R.id.right_layout, fragment); transaction.commit(); break; default: break; } }

动态添加碎片主要分为 5 步。

1、创建待添加的碎片实例。

2、获取到 FragmentManager,在活动中可以直接调用 getFragmentManager()方法得到。

3、开启一个事务,通过调用 beginTransaction()方法开启。

4、向容器内加入碎片,一般使用 replace()方法实现,需要传入容器的 id 和待添加的碎片实例。

5、提交事务,调用 commit()方法来完成。

三、在碎片中模拟返回栈

通过点击按钮添加了一个碎片之后,这时按下 Back 键程序就会直接退出。

public class MainActivity extends Activity implements OnClickListener { ...... @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: AnotherRightFragment fragment = new AnotherRightFragment(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager. beginTransaction(); transaction.replace(R.id.right_layout, fragment); transaction.addToBackStack(null); transaction.commit(); break; default: break; } } }

四、碎片和活动之间进行通信

为了方便碎片和活动之间进行通信,FragmentManager 提供了一个类似于 findViewById() 的方法,专门用于从布局文件中获取碎片的实例,代码如下所示:

RightFragment rightFragment = (RightFragment) getFragmentManager() .findFragmentById(R.id.right_fragment);

在每个碎片中都可以通过调用getActivity() 方法来得到和当前碎片相关联 的活动实例,代码如下所示:

MainActivity activity = (MainActivity) getActivity();

五、碎片的生命周期

运行状态:当一个碎片是可见的,并且它所关联的活动正处于运行状态时,该碎片也处于运行状态。

暂停状态:当一个活动进入暂停状态时(由于另一个未占满屏幕的活动被添加到了栈顶),与它相关联的可见碎片就会进入到暂停状态。

停止状态:当一个活动进入停止状态时,与它相关联的碎片就会进入到停止状态。

销毁状态:碎片总是依附于活动而存在的,因此当活动被销毁时,与它相关联的碎片就会进入 到销毁状态。

下面是碎片的一些回调方法:

onAttach() 当碎片和活动建立关联的时候调用。

onCreateView() 为碎片创建视图(加载布局)时调用。

onActivityCreated() 确保与碎片相关联的活动一定已经创建完毕的时候调用。

onDestroyView() 当与碎片关联的视图被移除的时候调用。

onDetach() 当碎片和活动解除关联的时候调用。

以上就是关于Android中碎片Fragment的全部内容,希望对大家的学习有所帮助。

【Android学习之Fragment】相关文章:

Android自定义控件之自定义属性

Android Mms之:深入理解Compose

Android 动画之RotateAnimation应用详解

Android开发之ContentProvider的使用详解

Android:多线程之AsyncTask

Android 布局控件之LinearLayout详细介绍

Android开发之OpenGL ES 颜色

Android之PreferenceActivity应用详解

Android控件系列之Toast使用介绍

Android 动画之ScaleAnimation应用详解

精品推荐
分类导航