ViewPager和片段替换。再次

时间:2013-05-15 09:30:13

标签: android android-viewpager fragment

我在双窗格模式下有非常简单的List / Detail应用程序:左侧 - 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由tab中的两个简单片段组成.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>

现在,当我在第一个片段中单击带有id=title的TextView时,我想显示新片段而不是第一个片段。我有这个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 ...而且一般来说,片段可以替换自己吗?如何替换片段???

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

0 个答案:

没有答案
相关问题