Android Fragment使用之实例演示使用之实例演示
Fragment是Android honeycomb 3.0新增的概念,在如何使用Android Fragment中做了关于Fragment的详细介绍。本文
则主要是通过实例的方式让大家更直观的了解Fragment的使用方法。
首先贴上实例的运行效果截图:
效果图的左边是一个列表,右边是列表item的详情。
先看一下布局文件(layout):
XML/HTML代码
<?xml version=“1.0″ encoding=“utf-8″?>
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:orientation=“horizontal” android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<fragment
class=“com.fragment.main.TitlesFragment”
android:id=“@+id/titles” android:layout_weight=“1″
android:layout_width=“0px” android:layout_height=“match_parent” />
<FrameLayout android:id=“@+id/details” android:layout_weight=“1″
android:layout_width=“0px” android:layout_height=“match_parent”
android:background=“?android:attr/detailsElementBackground” />
</LinearLayout>
布局文件中使用了fragment标签和FrameLayout标签。如何使用Android Fragment中介绍了两中嵌入Fragment的方法,这
个实例中都用到,从布局文件看到有了fragment标签,这是一种使用方法,FrameLayout标签将会成为第二种加载fragment的
载体view。
看一下程序实现(com.fragment.main.TitlesFragment):
Java代码
public class TitlesFragment extends ListFragment {
int mCurCheckPosition = 0;
int mShownCheckPosition = -1;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_activated_1,
Shakespeare.TITLES)); //使用静态数组填充列表
if (savedInstanceState != null) {
评论0