【Android应用开发技术:应用组件】Fragment使用方法
来源:互联网 发布:12寸windows平板 编辑:程序博客网 时间:2024/05/17 16:54
作者:郭孝星
微博:郭孝星的新浪微博
邮箱:allenwells@163.com
博客:http://blog.csdn.net/allenwells
Github:https://github.com/AllenWells
【Android应用开发技术:应用组件】章节列表
一 Fragment管理与事务
Activity通过FragmentManager管理Fragment,FragmentManager可以完成以下功能:
- 调用findFragmentById()或findFragmentByTag()方法来获取指定的Fragment。在XML文件中定义Fragment时可以通过android:id或android:tag来标识Fragment。
- 调用popBackStack()方法将Fragment从后台栈中弹出,模拟用户的back键。
- 调用addOnBackStackChangeListener()注册一个监听器,用来监听后台栈的变化。
在学习如何创建Fragment之前,我们先来看一下Fragment的继承体系,如下图所示:
Fragment继承体系类图Visio源文件下载
创建一个Fragment只需要继承Fragment类,然后在相应的生命周期方法中写入业务逻辑。我们在定义一个Fragment时,最常重写的方法就是onCreateView(),如下所示:
import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.ViewGroup;public class ArticleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.article_view, container, false); }}
1.1 添加Fragment
1.1.1 静态添加
所谓静态添加就是在布局文件总使用元素,元素的android:name属性指定了实现Fragment的类。这种使用XML布局文件将Fragment静态添加到Activity的方法,Fragment是不能被动态移除的。如果想实现Fragment的动态切入和切出,那么就需要在Activity启动后,再把Fragment添加到Activity。
举例
定义一个res/layout-large/news_articles.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <fragment android:name="com.example.android.fragments.HeadlinesFragment" android:id="@+id/headlines_fragment" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="com.example.android.fragments.ArticleFragment" android:id="@+id/article_fragment" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /></LinearLayout>
将布局文件添加到Activity中
import android.os.Bundle;import android.support.v4.app.FragmentActivity;public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.news_articles); }}
1.1.2 动态添加
在Java代码中通过FragmentTransaction对象的add()方法来添加Fragment。Activity的getFragmentManager()方法可返回FragmentManager对象,FragmentManager对象的beginTransaction()方法即可开启并返回FragmentTransaction对象。
举例
定义一个res/layout_articles.xml文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" />
1.2 替换Fragment
替换Fragment的过程与添加的过程非常相似,是需要把add()方法替换为replace()方法即可。
注意:当我们执行Fragment事务的时候,比如替换,我们需要适当的让用户可以向后导航和撤销这次操作,为了能够让用户向后导航Fragment事务,我们需要在FragmentTransaction提交前调用addToBackStack()方法。addToBackStack()方法提供了一个可选的String参数为事务指定了一个唯一的名字。这个名字不是必须的,除非 我们打算用FragmentManager.BackStackEntry APIs来进行一些高级的Fragment操作。
当我们移除或者替换一个Fragment并把它放入返回栈中时,被移除的fragment的生命周期是stopped,而不是destoryed。当用户返回重新恢复这个Fragment,它的生命周期是restarts。如果我们没把Fragment放入返回栈中,那么当它被移除或者替换时,它的生命周期是destoryed。
举例
// Create fragment and give it an argument specifying the article it should showArticleFragment newFragment = new ArticleFragment();Bundle args = new Bundle();args.putInt(ArticleFragment.ARG_POSITION, position);newFragment.setArguments(args);FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();// Replace whatever is in the fragment_container view with this fragment,// and add the transaction to the back stack so the user can navigate backtransaction.replace(R.id.fragment_container, newFragment);transaction.addToBackStack(null);// Commit the transactiontransaction.commit();
二 Fragment与Activity通信
首先说一下两个Fragment之间交互的问题。
为了重用Fragment UI组件,我们应该把每一个Fragment都构建成完全的自包含的、模块化的组件,定义它们自己的布局与行为。当我们定义好这些模块化的Fragment,我们就可以让他们关联Activity,使他们与App的逻辑结合起来,实现全局的复合的UI。
通常,我们想要Fragment之间能相互交互,比如基于用户事件改变Fragment的内容。所有Fragment之间的交互需要通过它们关联的activity,两个Fragment之间不应该直接交互。
2.1 Activity向Fragment传递数据
在Activity中创建Bundle数据包,并调用Fragment的setArguments(Bundle bundle)方法即可将Bundle数据包传递给Fragment。
举例
public static class MainActivity extends Activity implements HeadlinesFragment.OnHeadlineSelectedListener{ ... public void onArticleSelected(int position) { // The user selected the headline of an article from the HeadlinesFragment // Do something here to display that article ArticleFragment articleFrag = (ArticleFragment) getSupportFragmentManager().findFragmentById(R.id.article_fragment); if (articleFrag != null) { // If article frag is available, we're in two-pane layout... // Call a method in the ArticleFragment to update its content articleFrag.updateArticleView(position); } else { // Otherwise, we're in the one-pane layout and must swap frags... // Create fragment and give it an argument for the selected article ArticleFragment newFragment = new ArticleFragment(); Bundle args = new Bundle(); args.putInt(ArticleFragment.ARG_POSITION, position); newFragment.setArguments(args); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack so the user can navigate back transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit(); } }}
2.2 Fragment向Activity传递数据
在Fragment中定义一个内部回调接口,再让包含该Fragment的Activity实现该回调接口,这样Fragment即可调用该回调方法传递数据给Activity。
举例
在Fragment定义了一个接口
public class HeadlinesFragment extends ListFragment { OnHeadlineSelectedListener mCallback; // Container Activity must implement this interface public interface OnHeadlineSelectedListener { public void onArticleSelected(int position); } @Override public void onAttach(Activity activity) { super.onAttach(activity); // This makes sure that the container activity has implemented // the callback interface. If not, it throws an exception try { mCallback = (OnHeadlineSelectedListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener"); } } //Fragment通过回调接口给所属Activity发送信息 @Override public void onListItemClick(ListView l, View v, int position, long id) { // Send the event to the host activity mCallback.onArticleSelected(position); }}
Fragment所属的Activity实现该接口
public static class MainActivity extends Activity implements HeadlinesFragment.OnHeadlineSelectedListener{ ... public void onArticleSelected(int position) { // The user selected the headline of an article from the HeadlinesFragment // Do something here to display that article }}
- 【Android应用开发技术:应用组件】Fragment使用方法
- 【Android应用开发技术:应用组件】Fragment基本原理
- 【Android应用开发技术:应用组件】Intent使用方法
- 【Android应用开发技术:应用组件】应用组件基本原理
- 【Android应用开发技术:应用组件】Handler组件通信机制
- 【Android应用开发技术:应用组件】Android事件处理机制
- Android应用开发:Fragment
- 【Android应用开发技术:应用组件】:Activity基本原理
- 【Android应用开发技术:应用组件】Intent基本原理
- 【Android应用开发技术:应用组件】事件处理机制
- 【Android应用开发技术:应用组件】章节列表
- 【Android应用开发技术:网络通信】网络服务可发现使用方法
- 【Android 应用开发】 Fragment 详解
- 【Android应用开发技术:用户界面】其他杂项组件
- 【Android应用开发技术:用户界面】视图组件View
- 【Android开发】 Android Fragment应用实战(转)
- 【Android应用开发技术:应用调试】ADB
- 【Android应用开发技术:应用调试】GDB
- Java 内部类实现原理简单分析
- HDU 2829 Lawrence (斜率优化DP)
- linux shell 判断文件是否存在等符号
- MIPS 指令
- UVALive 6800 The Mountain of Gold (bellman_ford判负环)
- 【Android应用开发技术:应用组件】Fragment使用方法
- URI规范的百分比编码
- CodeForce 424C Magic Formulas
- oracle 怎么查看表空间,求高手指点,可以用PL/SQL查看
- 【asp】on error resume next用法
- yii2 rbac 详解
- 菜鸟遇到的第一个servlet问题(404,弹出对话框)
- 从工程到代码的自我分裂
- Object对象详解(一)之toString