常に3以下)私はこの現象を再現しようとしたが、次のコードで私はcontainer.getChildCountを(参照:
activity_main.xml
MainActivity.java
public class MainActivity extends AppCompatActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private String TAG = SectionsPagerAdapter.class.getSimpleName();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Log.v(TAG, "container.getChildCount:" + container.getChildCount());
return super.instantiateItem(container, position);
}
@Override
public int getCount() {
return 10;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
return String.format(l, "Section %d", position);
}
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ListView listView = (ListView) rootView.findViewById(R.id.listView);
ArrayAdapter<String> listAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1);
listAdapter.add("Fragment Body " + getArguments().getInt(ARG_SECTION_NUMBER));
listAdapter.add("one");
listAdapter.add("two");
listAdapter.add("three");
listView.setAdapter(listAdapter);
return rootView;
}
}
}
を
<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" tools:context=".MainActivity"/>
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment">
<ListView
android:id="@+id/listView"
android:layout_height="match_parent"
android:layout_width="wrap_content" />
「2ページがどちらの側に保持さがある」 - どのようにあなたは、これが真実であることを知っていますか?あなたは何の変数を見ていますか?ありがとう! –
アイドル状態では、container.getChildCount()(PagerAdapterのinstantiateItemメソッドで使用されています)を確認しました。これは5.Thatという意味で、どちらの側にも2ページが保持されています。 –
私は、ListViewを含む単純なフラグメントで、android.support.v4.app.FragmentPagerAdapterとandroid.support.v4.view.ViewPagerを使用してこの動作を再現しようとしましたが、container.getChildCountはテストアプリケーションで常に3以下でした。私は使用しているのと同じViewPagerクラスを使用していますか? –