背景
(这个例子来源于疯狂安卓讲义)用Fragment实现一个展示书的详细信息的app,app layout 主要分为左右两部分,左边每行显示了书名,右边每部分显示了书名及其描述。如下图所示,
实现的代码
1. res 里面的两个 layout
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 定义一个水平排列的LinearLayout,并指定使用中等分隔条 -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle">
<!-- 添加一个Fragment -->
<fragment
android:name="com.example.foranan3.BookListFragment"
android:id="@+id/book_list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<!-- 添加一个FrameLayout容器 -->
<FrameLayout
android:id="@+id/book_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
从上面的布局文件中,我们知道fragment需要首先添加到Activity中去。 fragment_book_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 定义一个TextView来显示图书标题 -->
<TextView
style="?android:attr/textAppearanceLarge"
android:id="@+id/book_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"/>
<!-- 定义一个TextView来显示图书描述 -->
<TextView
style="?android:attr/textAppearanceMedium"
android:id="@+id/book_desc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"/>
</LinearLayout>
2. MainActivity
public class MainActivity extends AppCompatActivity implements
BookListFragment.Callbacks
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// 加载/res/layout目录下的activity_book_twopane.xml布局文件
setContentView(R.layout.activity_main);
}
// 实现Callbacks接口必须实现的方法
@Override
public void onItemSelected(Integer id)
{
// 创建Bundle,准备向Fragment传入参数
Bundle arguments = new Bundle();
arguments.putInt(BookDetailFragment.ITEM_ID, id);
// 创建BookDetailFragment对象
BookDetailFragment fragment = new BookDetailFragment();
// 向Fragment传入参数
fragment.setArguments(arguments);
// 使用fragment替换book_detail_container容器当前显示的Fragment,刚开始初始化的时候book_detail_container显示为空白
getFragmentManager().beginTransaction()
.replace(R.id.book_detail_container, fragment)
.commit(); // ①
}
}
可以看到整个流程是,刚开始初始化的时候book_detail_container(即右边部分)显示为空白。当我们在左边选择了一本书之后->触发了onItemSelected方法->设置了arguments->fragment.setArguments(arguments)->replace方法,这样就实现了替换更新。
从这里的代码可以学到,
- Activity和Fragment通信(传递参数)的方法。在Activity中,我们需要新建bundle,加入参数,并传给fragment。
- Activity中可以通过getFragmentManager方法对Fragment们进行管理,管理操作有增加、删除以及替换等等;
- 以上这些对Fragment的管理操作统称为transaction,代码中我们选择了替换replace操作,并提交事务;
此外,FragmentManager还有以下几个功能,
- 通过findFragmentById()或findFragmentByTag()方法来获取指定fragment;
- 通过popBackStacke()将fragment从后台栈中弹出(模拟用户按下back键);
- 通过addOnBackStackChangeListener()注册一个监听器,监听后台栈的变化;
3. BookDetailFragment
public class BookDetailFragment extends Fragment
{
public static final String ITEM_ID = "item_id";
// 保存该Fragment显示的Book对象
BookContent.Book book;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// 如果启动该Fragment时包含了ITEM_ID参数
if (getArguments().containsKey(ITEM_ID))
{
book = BookContent.ITEM_MAP.get(getArguments()
.getInt(ITEM_ID)); // ①
}
}
// 重写该方法,该方法返回的View将作为Fragment显示的组件
@Override
public View onCreateView(LayoutInflater inflater
, ViewGroup container, Bundle savedInstanceState)
{
// 加载/res/layout/目录下的fragment_book_detail.xml布局文件
View rootView = inflater.inflate(R.layout.fragment_book_detail,
container, false);
if (book != null)
{
// 让book_title文本框显示book对象的title属性
((TextView) rootView.findViewById(R.id.book_title))
.setText(book.title);
// 让book_desc文本框显示book对象的desc属性
((TextView) rootView.findViewById(R.id.book_desc))
.setText(book.desc);
}
return rootView;
}
}
在 MainActivity 中,我们每次点击左边某一行,就会新建一个 BookDetailFragment。点击的时候通过获取传入的item_id,就可以获得对应id下的Book对象,获得这个对象之后,我们将该对象的title和desc属性展示在对应的组件上就可以了。
4. BookContent
public class BookContent
{
// 定义一个内部类,作为系统的业务对象
public static class Book
{
public Integer id;
public String title;
public String desc;
public Book(Integer id, String title, String desc)
{
this.id = id;
this.title = title;
this.desc = desc;
}
@Override
public String toString()
{
return title;
}
}
// 使用List集合记录系统所包含的Book对象
public static List<Book> ITEMS = new ArrayList<Book>();
// 使用Map集合记录系统所包含的Book对象
public static Map<Integer, Book> ITEM_MAP
= new HashMap<Integer, Book>();
static
{
// 使用静态初始化代码,将Book对象添加到List集合、Map集合中
addItem(new Book(1, "疯狂Java讲义"
, "一本全面、深入的Java学习图书,已被多家高校选做教材。"));
addItem(new Book(2, "疯狂Android讲义"
, "Android学习者的首选图书,常年占据京东、当当、 "
+ "亚马逊3大网站Android销量排行榜的榜首"));
addItem(new Book(3, "轻量级Java EE企业应用实战"
, "全面介绍Java EE开发的Struts 2、Spring 3、Hibernate 4框架"));
}
private static void addItem(Book book)
{
ITEMS.add(book);
ITEM_MAP.put(book.id, book);
}
}
BookContent主要定义了一个类用来描述对象。
5. BookListFragment
public class BookListFragment extends ListFragment
{
private Callbacks mCallbacks;
// 定义一个回调接口,该Fragment所在Activity需要实现该接口
// 该Fragment将通过该接口与它所在的Activity交互
public interface Callbacks
{
void onItemSelected(Integer id);
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// 为该ListFragment设置Adapter
setListAdapter(new ArrayAdapter<BookContent.Book>(getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1, BookContent.ITEMS)); //①
}
// 当该Fragment被添加、显示到Activity时,回调该方法
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
// 如果Activity没有实现Callbacks接口,抛出异常
if (!(activity instanceof Callbacks))
{
throw new IllegalStateException(
"BookListFragment所在的Activity必须实现Callbacks接口!");
}
// 把该Activity当成Callbacks对象
mCallbacks = (Callbacks)activity;
}
// 当该Fragment从它所属的Activity中被删除时回调该方法
@Override
public void onDetach()
{
super.onDetach();
// 将mCallbacks赋为null。
mCallbacks = null;
}
// 当用户单击某列表项时激发该回调方法
@Override
public void onListItemClick(ListView listView
, View view, int position, long id)
{
super.onListItemClick(listView, view, position, id);
// 激发mCallbacks的onItemSelected方法
mCallbacks.onItemSelected(BookContent
.ITEMS.get(position).id);
}
public void setActivateOnItemClick(boolean activateOnItemClick)
{
getListView().setChoiceMode(
activateOnItemClick ? ListView.CHOICE_MODE_SINGLE
: ListView.CHOICE_MODE_NONE);
}
}
BookListFragment主要实现了左边部分的功能,它继承自ListFragment,而MainActivity又实现了该接口。当Fragment被添加、显示到Activity时,接口会被回调,执行Activity中的onItemSelected方法。
onAttach -> mCallbacks = (Callbacks) Activity -> onItemSelected
Fragment 的生命周期