在我的项目中,我有一些片段布局。
在Main.axml中,我使用CoordinatorLayout和NavigationView导航到我的布局。这没问题。但在一个片段中,我想使用TabLayout和ViewPager来显示两个子布局,这也是片段,我有问题。当我切换TabLayout时,ViewPager显示为空白。
简而言之,Fragment和ViewPager中的ViewPager中没有任何内容。
我调试了两天,并搜索了很多方法来解决这个问题,但没用。这是我的部分代码。
提前致谢
片段布局有ViewPager
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/astroTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="@+id/astroViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
其片段代码
class AstroFragment : Fragment
{
ViewPager astroViewPager;
TabLayout astroTabLayout;
public override void OnStart()
{
base.OnStart();
}
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public override void OnViewCreated(View view, Bundle savedInstanceState)
{
base.OnViewCreated(view, savedInstanceState);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.AstroFragment, container, false);
astroViewPager = view.FindViewById<ViewPager>(Resource.Id.astroViewPager);
astroTabLayout = view.FindViewById<TabLayout>(Resource.Id.astroTabLayout);
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(ChildFragmentManager);
astroViewPager.Adapter = adapter;
astroViewPager.SetCurrentItem(0, true);
astroViewPager.OffscreenPageLimit = 2;
astroTabLayout.SetupWithViewPager(astroViewPager);
return view;
}
}
一个子片段布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="30px">
<TextView
android:text="data"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
一个子片段代码
class AstroDataFragment : Android.Support.V4.App.Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override void OnViewCreated(View view, Bundle savedInstanceState)
{
base.OnViewCreated(view, savedInstanceState);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
View view = inflater.Inflate(Resource.Layout.AstroDataFragment, container, false);
return view;
}
}
MyFragmentPagerAdapter代码
class MyFragmentPagerAdapter : FragmentPagerAdapter
{
const int PAGE_COUNT = 2;
private string[] tabTitles = { "视图一", "网页图表" };
private List<Android.Support.V4.App.Fragment> fragments = new List<Android.Support.V4.App.Fragment>();
public MyFragmentPagerAdapter(Android.Support.V4.App.FragmentManager fm) : base(fm)
{
// Sub Fragments
fragments.Add(new AstroDataFragment());
fragments.Add(new AstroImageFragment());
}
public override int Count
{
get
{
return PAGE_COUNT;
}
}
public override void DestroyItem(ViewGroup container, int position, Java.Lang.Object @object)
{
//base.DestroyItem(container, position, @object);
}
public override Android.Support.V4.App.Fragment GetItem(int position)
{
return fragments[position];
}
public override ICharSequence GetPageTitleFormatted(int position)
{
var charSequence = CharSequence.ArrayFromStringArray(tabTitles);
return charSequence[position];
}
}