Fragment界面添加

了解过fragment的生命周期等简单知识,于是去看官方文档来了解更多相关内容,要添加fragment到我们的UI界面中,给出了两种常用的方法,第一个是在activity的布局xml文件中使用<fragment>标签声明,第二个是在java代码中将fragment添加到一个ViewGroup,我比较习惯于使用第一种XML布局的方式,这里只是介绍了第一种, 在主activity中纵向加入两个fragment,使用<fragment>标签声明,每个fragment有各自的layout布局,都作为activity的一部分

<LinearLayout 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=".MainActivity"     android:orientation="vertical"    android:background="#7ecef4"><fragment     android:name="com.example.fragementexam.FragementList"    android:id="@+id/frag_list"    android:layout_width="fill_parent"    android:layout_height="0dp"    android:layout_weight="2"/><fragment     android:name="com.example.fragementexam.FragementDetails"    android:id="@+id/frag_detail"    android:layout_width="fill_parent"    android:layout_height="0dp"    android:layout_weight="1"/></LinearLayout>

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

之后要为每一个fragment添加界面,单独写每个的layout,然后嵌入到activity中,我们必须要在每个Fragment的类中实现onCreateView()方法,并将layout添加进去;如果你的Fragement类本身继承了ListFragment,则不需要必须实现onCreateView(),因为默认已经返回了ListView控件对象

下面是一段Fragment类如何从布局xml文件中生成对象的,主要就是在onCreateView()方法中添加进去布局文件:

public class FragementDetails extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubreturn inflater.inflate(R.layout.frag_detail, container,false);}}

其中
inflater用于向fragment中添加view
container用来存放fragment其layout的ViewGroup对象
savedInstanceState类似于onCreate()中相同的Bundle参数,用于恢复状态

inflater的inflate方法的三个参数分别是:

int resource:fragment的layout资源ID。

ViewGroup root:存放fragment的layout的ViewGroup

boolean attachToRoot:是否在创建fragment的layout时,把layout添加到container上面去

Fragment的管理方式

要管理fragment,我们需要用到FragmentManager,在fragement所在的activity中通过getFragment来获得,FragmentManager常用之处;

1使用方法findFragmentById()findFragmentByTag(),获取activity中已存在的fragment们。

2使用方法popBackStack()activity的后退栈中弹出fragment们(这可以模拟后退键引发的动作)。

3用方法addOnBackStackChangedListerner()注册一个侦听器以监视后退栈的变化。

----------------------------------------------------------------------------------------------------------------------------------------------------

通过FragmentManager我们还可以来执行对fragment的事务操作,即在同一时刻执行一组动作,首先通过begintransaction获取一个事物的实例,然后可以执行一系列的事务操作,如add(),remove(),replace(),最后使用commint()方法提交事务

下面的代码片段展示了一个简单的事务操作

public class FragmentPreferences extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragement()).commit();}public static class PrefsFragement extends PreferenceFragment {@Overridepublic void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);addPreferencesFromResource(R.xml.preferences);}}}


值得注意的是只能在activity处于可保存的状态时提交事务(比如onPause和onStop方法中),否则会引起异常,这是因为fragment的状态会丢失。如果要在可能丢失状态的情况下提交事务,请使用commitAllowingStateLoss()。

更多相关文章

  1. Android(安卓)Hook Activity 的几种姿势
  2. 十分钟学会kotlin实现Android(安卓)MVP模式开发
  3. 8大应用助你个性化定制最Cool的Android手机
  4. 线程方法Android:异步调用详解
  5. Android查看数据库方法及工具
  6. [转]android解决apk编译方法数超过64k的问题
  7. Android(安卓)Handler 作用以及使用
  8. Android(安卓)四大组件 ————Service(生命周期)
  9. Android每周一轮子:OkHttp(1)

随机推荐

  1. Android中图像变换Matrix的原理
  2. Android Studio如何轻松整理字符串到stri
  3. [Android]Unit Test for Android
  4. android中怎样声明操作通话记录的权利
  5. Android开发—利用FrameLayout实现图标中
  6. 横竖屏布局小技巧
  7. Android练习小项目时踩到的坑
  8. Android 淺探
  9. [Android 特效] Android 通过流播放声音(
  10. android edittext的属性