Android 4学习(8):用户界面

参考《Professional Android 4 Development》

Fragment简介

创建Fragment

package test.fragments;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class MySkeletonFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {  // Create, or inflate the Fragment’s UI, and return it.  // If this Fragment has no UI then return null.  return inflater.inflate(R.layout.my_fragment, container, false);  }}Fragment生命周期

Fragment

Fragment特有的生命周期事件获取FragmentManager

FragmentManagerfragmentManager=getFragmentManager();

在Activity中添加Fragment的最简单方法是使用layout配置文件,,例如:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=""  android:orientation="horizontal"  android:layout_width="match_parent"  android:layout_height="match_parent">  <fragment android:name="com.paad.weatherstation.MyListFragment"  android:id="@+id/my_list_fragment"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:layout_weight="1"  />  <fragment android:name="com.paad.weatherstation.DetailsFragment"  android:id="@+id/details_fragment"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:layout_weight="3"  /></LinearLayout>

调用inflate方法生成Fragment的界面后,Fragment实际上是一个类似ViewGroup的角色,在Activity中管理自己的UI。

上面那种将Fragment添加到Activity的方法缺乏灵活性,不能实现动态地添加和删除,更好的方式是使用FragmentTranaction和类似下面这样的配置文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=""  android:orientation="horizontal"  android:layout_width="match_parent"  android:layout_height="match_parent">  <FrameLayout  android:id="@+id/ui_container"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:layout_weight="1"  />  <FrameLayout  android:id="@+id/details_container"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:layout_weight="3"  /></LinearLayout>使用FragmentTransaction

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();// Add, remove, and/or replace Fragments.// Specify animations.// Add to back stack if required.fragmentTransaction.commit();添加,删除和替换Fragment

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();fragmentTransaction.add(R.id.ui_container, new MyListFragment());fragmentTransaction.commit();

删除Fragment需要FragmentTransaction的remove()方法,参数为Fragment对象,Fragment对象可以通过FragmentManager的findFragmentById()方法获得。

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();Fragment fragment = fragmentManager.findFragmentById(R.id.details_fragment);fragmentTransaction.remove(fragment);fragmentTransaction.commit();

替换Fragment使用的是FragmentTransaction的replace()方法,参数分别为所要替代Fragment所在容器的ID和新的Fragment:

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();fragmentTransaction.replace(R.id.details_fragment, new DetailFragment(selected_index));fragmentTransaction.commit();获取指定的Fragment

MyFragment myFragment = (MyFragment)fragmentManager.findFragmentById(R.id.MyFragment);

也可以通过创建Fragment时添加的tag获取特定的Fragment:

MyFragment myFragment = (MyFragment)fragmentManager.findFragmentByTag(MY_FRAGMENT_TAG);删除Fragment容器

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=""  android:orientation="horizontal"  android:layout_width="match_parent"  android:layout_height="match_parent">  <FrameLayout  android:id="@+id/ui_container"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:layout_weight="1"  />  <FrameLayout  android:id="@+id/details_container"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:layout_weight="3"  android:visibility="gone"  /></LinearLayout>Fragment和BackStack人生最大的错误是不断担心会犯错

Android 4学习(8):用户界面

相关文章:

你感兴趣的文章:

标签云: