0

我在双窗格模式下有非常简单的列表/详细信息应用程序:左侧 - ListFragment,右侧 - ViewPager。这是布局:

<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:baselineAligned="false" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <fragment
            android:id="@+id/site_list"
            android:name="com.example.fragmentviewpager.ListFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/viewpager"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" >

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

一切正常:当您单击列表项时,详细信息会显示在 ViewPager 中。ViewPager 由选项卡中的两个简单片段组成。ViewPager 使用 FragmentStatePagerAdapter 来创建片段。

片段布局:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Test"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:clickable="true" >
        </TextView>

        <TextView
            android:id="@+id/tvPage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge" >
        </TextView>

    </RelativeLayout>

现在,当我单击第一个片段中的 TextView 时,我想显示新片段而不是第id=title一个片段。我为此 TextView 设置了监听器:

title.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Fragment3 fragment3 = new Fragment3();
            FragmentTransaction transaction = getChildFragmentManager()
                    .beginTransaction();
            transaction.addToBackStack(null);
            transaction.replace(R.id.SOME_ID, fragment3).commit();
        }
    });

但我不知道,我必须改写什么SOME_ID......一般来说,片段可以替换自己吗?如何替换片段???

如果您需要其他代码 - 请告诉我。

4

0 回答 0