0

我有一个包含 ViewPager 的活动,设置为模板 Swipe ViewPager。我已通过适配器将两个片段附加到该 ViewPager。我现在正在尝试打开一个新片段,但我很难让它工作。我不完全理解使用 FragmentTransaction 更改片段的想法(我尝试阅读 Android 文档但无济于事)所以我将 FragmentTransaction 代码从网络上的地方拼凑在一起。

这是我的 activity_main.xml

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"/>

这是我用来尝试打开片段的代码

private void openVolumeSettingsFragment() {
    Activity activity = getActivity();
    FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.pager, new VolumeSettingsFragment());
    transaction.addToBackStack(null);
    transaction.commit();
}

这是我的新片段 fragment_volume_settings.xml 的空白 xml 文件

<FrameLayout 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"
             tools:context="com.velixo.bitchtalkandroid.fragments.VolumeSettingsFragment">

    <ListView
        android:id="@+id/volume_settings_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

这样做的唯一一件事就是冻结我当前的片段,使其无法滑动。有人知道我应该做什么吗?

4

1 回答 1

0

在 reddit 上发布了相同的问题,x-posting jeffinmadison 的答案,以防阅读此问题的任何人都需要它:

如果你想用你的设置片段替换寻呼机片段,你应该让activity_main成为一个带有@+id/content的FrameLayout,并在FrameLayout中放置带有@+id/pager的ViewPager,然后在R.id上调用replaceFragment 。内容

<FrameLayout
    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:id="@+id/content">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"/>    

</FrameLayout>
于 2015-02-20T18:07:20.237 回答