Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何用片段替换ViewPager_Android_Android Fragments_Android Viewpager_Fragmenttransaction - Fatal编程技术网

Android 如何用片段替换ViewPager

Android 如何用片段替换ViewPager,android,android-fragments,android-viewpager,fragmenttransaction,Android,Android Fragments,Android Viewpager,Fragmenttransaction,我有下面的布局 <RelativeLayout 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:paddingBottom="@dimen/activity_verti

我有下面的布局

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >


<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>

这不是重新绘制ViewPager我该怎么办?

您可以创建一个包含ViewPager的片段,然后替换该片段

public class ViewPagerContainerFragment extends Fragment {

    private ViewPager mViewPager;

    public ViewPagerContainerFragment() {   }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.view_pager_container_fragment, container, false);          

        // Set up the ViewPager, attaching the adapter and ...
        mViewPager = (ViewPager) root.findViewById(R.id.viewPager);

        // do other stuff...

        return root;
    }
}
查看\u pager\u container\u fragment.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rlMain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"   
        />
</RelativeLayout>
代码后面的某个地方:(如果您想-用另一个片段替换ViewPagerFragment)


请确保没有造成任何内存泄漏,并正确设置适配器。

文档说明-
替换添加到容器中的现有片段。这本质上与为所有当前添加的片段调用remove(Fragment)相同,这些片段是使用相同的containerWebID添加的,然后再添加(int、Fragment、String)使用此处给出的相同参数。
因此我假设您只能替换
片段,而不能替换其他视图。如果尝试此解决方案,请注意导航返回行为。退房
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rlMain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"   
        />
</RelativeLayout>
    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">       
        </FrameLayout>
getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame, new ViewPagerContainerFragment())                 
                .commit();
getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame, new CustomFragment())                 
                .commit();