Android ViewPager无法加载第3个片段

时间:2015-10-10 07:53:17

标签: android android-fragments android-viewpager

当我将3个碎片加载到ViewPager时,我鼓励我解决这个问题 每次我启动Activity时,只加载了2个片段(Fragment 1& Fragment 2)。 我不知道为什么片段3没有加载。

以下是我的代码

活动

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_testing);
        // load pager data
        viewPager = (ViewPager) findViewById(R.id.pagerTestingRegister);
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new Fragment1());
        fragments.add(new Fragment2());
        fragments.add(new Fragment3());

        viewPager.setAdapter(new RegisterTestingFragmentPagerAdapter(getSupportFragmentManager(), fragments));

}

活动用户界面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/expense1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".Expense1">

<TabHost
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tabHost">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <HorizontalScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/scrollTabHost"
            android:fillViewport="true"
            android:scrollbars="none">
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="bottom"></TabWidget>
        </HorizontalScrollView>



        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v4.view.ViewPager
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:persistentDrawingCache="all"
                android:id="@+id/pagerTestingRegister">
            </android.support.v4.view.ViewPager>
        </FrameLayout>
    </LinearLayout>
</TabHost>

适配器

public class RegisterTestingFragmentPagerAdapter extends FragmentPagerAdapter {
List<Fragment> fragments;

public RegisterTestingFragmentPagerAdapter(FragmentManager fragmentManager, List<Fragment> fragments){
    super(fragmentManager);
    this.fragments = fragments;
}

public RegisterTestingFragmentPagerAdapter(FragmentManager fragmentManager){
    super(fragmentManager);
    //this.fragments = fragments;
}

@Override
public android.support.v4.app.Fragment getItem(int position) {
    return fragments.get(position);
}

@Override
public int getCount() {
    return fragments.size();
}

}

1 个答案:

答案 0 :(得分:3)

这对我有用:

mViewPager.setOffscreenPageLimit(2);

在此Angad Tiwari

中由comment提及