我有一个包含 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>
这样做的唯一一件事就是冻结我当前的片段,使其无法滑动。有人知道我应该做什么吗?