隣り合った 2 つのフラグメントを含むレイアウトのアクティビティがあります。各 Fragment には、ViewPager があります。両方の Fragment を同じように表示する必要があるため、ViewPager を含む同じレイアウト XML を両方に使用します。
アプリをテストすると、最初の Fragment の ViewPager だけが機能するようです。2 番目の Fragment には PagerTitleStrip が表示され、ページをめくることができますが、ViewPager に Fragment が表示されません。
2 番目の ViewPager にも Fragments が表示されないのはなぜですか? これは何が原因ですか?彼らが同じレイアウトを使用しているという問題はありますか?とにかく別のオブジェクトではありませんか?
アクティビティ内の 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>
どちらの Fragment も同じ方法で 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;
}
...
}
バグの原因を探すヒントはありますか? もちろん、必要に応じてここにさらにコードを含めることもできます。