Android ViewPager和片段替换。阿盖恩

Android ViewPager和片段替换。阿盖恩,android,android-viewpager,fragment,Android,Android Viewpager,Fragment,在双窗格模式下,我有一个非常简单的列表/详细信息应用程序:左侧-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:layou

在双窗格模式下,我有一个非常简单的列表/详细信息应用程序:左侧-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>
但是我不知道,我必须写些什么来代替
一些ID
。。。一般来说,碎片能代替自己吗?如何替换碎片


如果您需要额外的代码,请告诉我。

您到底想更改哪个片段?带有列表或viewpager或viewpager中的片段?viewpager中的片段。想象一下情况:左边是国家列表,右边是空白。您单击了列表中的国家名称。现在右侧是带有两个选项卡的ViewPager。第一个片段(第一个标签)有文本视图,它告诉我们这个国家的首都。你可以点击这个文本视图,大写名称,这个片段,在ViewPager中,必须被另一个包含大写信息的片段替换。所以你正在寻找一种方法来替换ViewPager中的片段。这可能会有帮助:我已经读过了……看看答案。他也使用replace()函数。。。但是在我的例子中我必须使用哪个ID???使用第一个片段(您要替换的片段)布局的根ID。因此,例如,如果它只是一个全屏
LinearLayout
,其中包含
TextView
,则使用此
LinearLayout
的id。
    <?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>
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();
        }
    });