已创建ViewPager片段,但未在Android上显示
我的布局如下:已创建ViewPager片段,但未在Android上显示,android,android-viewpager,fragment,xamarin.android,android-4.2-jelly-bean,Android,Android Viewpager,Fragment,Xamarin.android,Android 4.2 Jelly Bean,我的布局如下: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.widget.Dra
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/main_container_1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/main_container_2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<FrameLayout
android:id="@+id/leftDrawer"
android:layout_width="@dimen/some_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:paddingTop="?android:attr/actionBarSize" />
</android.support.v4.widget.DrawerLayout>
</FrameLayout>
当然,稍后,我会通过FragmentStatePagerAdapter将数据绑定到viewPager
结果:第一个片段中包含的子片段显示正确
我对main_container_2中不同类型的第二个片段执行相同的操作,该片段具有以下onCreateView
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
_myCustomContainer = (MyCustomLayout)inflater.Inflate(Resource.Layout.MyCustomLayout, container, false);
_viewPager = new ViewPager(Activity);
_myCustomContainer.AddView(_viewPager);
//It is required that view pager have an ID othe than -1
_viewPager.Id = 0x00000001;
return _myCustomContainer;
}
我使用与第一个片段的第一个viewPager相同类型的FragmentStatePagerAdapter
结果:在低于或等于JellybeanAPI 16的Android版本上,创建了第二个片段的子片段,但未显示
下面是我实现FragmentStatePagerAdapter的GetItem方法
public override Fragment GetItem(int position)
{
if (!ActiveFragments.ContainsKey(position))
{
var fragment = PagedCustomFragment.NewPagedCustomFragment(position);
ActiveFragments[position] = fragment;
}
return ActiveFragments[position];
}
问题:为什么没有显示第二个片段的子片段?您的两个ViewPager似乎属于同一个父级,并且具有相同的ID,这可能是问题的根本原因。您是否尝试为每个ViewPager使用不同的ID?请为您正在使用的任何编程环境标记此ID,因为这显然不是Java。关于您的问题,使用API级别17之前的本机片段不支持嵌套片段,例如,包含ViewPager的片段包含片段。您必须使用Android支持包中的fragments backport。我已经用monodroid标记了它,因为它是用C编写的。此外,我理解,我应该期望不支持的功能根本不可用,但正如我所提到的,第一个包含view pager的fragments工作得很好,并且已经工作了近一年。就我而言,期待第二个片段也能起作用是不合理的吗?它起作用了!更改第二视图寻呼机的ID修复了我的问题!非常感谢你!!!
public override Fragment GetItem(int position)
{
if (!ActiveFragments.ContainsKey(position))
{
var fragment = PagedCustomFragment.NewPagedCustomFragment(position);
ActiveFragments[position] = fragment;
}
return ActiveFragments[position];
}