首先我们在主界面设置两个静态Fragment,布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
>
<fragment
android:id="@+id/item_list"
android:name="com.example.myapplication.ItemFragment"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
<fragment
android:id="@+id/item_detail"
android:name="com.example.myapplication.ItemDetaiFragment"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
/>
</LinearLayout>
MainActivity 一启动,这两个Fragment 就会被创建。两个Fragment布局文件如下:
ItemFragment
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ListFragment"
/>
</LinearLayout>
ItemDetaiFragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas