我有一个活动,其中包含2个片段相邻的布局。在每个片段中,我有一个ViewPager。由于我需要两个片段看起来相同的方式,我使用相同的布局XML,其中包含一个ViewPager。Android:2片段共享相同ViewPager XML布局
现在,当我测试应用程序,只有第一个片段的ViewPager似乎工作。第二个片段显示一个PagerTitleStrip,我可以翻阅它,但它不显示ViewPager中的片段。
为什么不是第二个ViewPager也显示碎片?这是什么造成的? 他们使用相同布局的问题是什么?无论如何,它不是一个不同的对象吗?
了2个片段在活动分享看起来像这样的布局:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.PagerTitleStrip
android:id="@+id/search_mapping_pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
两个片段实例化ViewPager以同样的方式:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search_mapping,
container, false);
mSectionsPagerAdapter = new SectionsPagerAdapter(mActivity
.getSupportFragmentManager());
mViewPager = (ViewPager) view.findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
return view;
}
编辑:更多的代码显示我的SectionsPagerAdapter
:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new SuperclassFragment();
Bundle args = new Bundle();
args.putInt(SuperclassFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
...
}
任何提示,我应该寻找错误的原因?如有必要,我当然可以在这里包含更多的代码。
您应该将Viw = ewPage放在线性/相对布局下。此外,你可以请把SectionsPagerAdapter代码,因为它是有趣的看到getItem函数 – Amit
随着线性/相对布局你是对的,我想。我已经把SectionsPagerAdapter的代码。可能问题是他们使用相同的'FragmentManager'实例? – Terry
我觉得,line Fragment fragment = new SuperclassFragment();是问题。 View Paget将调用getItem方法,此时,您将返回SuperclassFragment。它是一个父类,因此View Pager不知道要渲染哪个片段,它将使用它在classpath中找到的第一个片段,但我不确定 – Amit