2012-02-02 70 views
1

我对ViewPager有问题。它不会显示我的任何预定义片段,它只是显示一个黑屏。 onCreateView方法从片段中调用。ViewPager片段,未连接?

public class TestViewPager extends FragmentActivity { 
    @Override 
    public void onCreate(Bundle bundle) { 
     super.onCreate(bundle); 
     setContentView(R.layout.test_view_pager); 
    } 
} 

public class TestFragment extends Fragment { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View fragmentView = inflater.inflate(R.layout.entry_layout, container, false); 
     return fragmentView; 
    } 
} 

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <fragment 
     class="com.test.TestFragment" 
     android:id="@+id/embedded1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <fragment 
     class="com.test.AnotherTestfragment" 
     android:id="@+id/embedded2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
</android.support.v4.view.ViewPager> 

回答

3

使用ViewPager,您必须实现PagerAdapter并从那里提供片段。您无法定义要在XML中显示在ViewPager中的视图/片段,因为它不会绘制它们。

编辑:由于您要显示片段,您必须使用FragmentPagerAdapter。