Reputation: 7338
I have an activity with two ViewPagers both working with a FragmentStatePagerAdapter. But when the ViewPagers have no id, or both have the same id, they will not work correctly.
Activity layout (activity.xml
):
<LinearLayout 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:orientation="vertical"
tools:context="com.example.app.MainActivity">
<FrameLayout
android:id="@+id/frame1"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
<include layout="@layout/merge_pager" />
</FrameLayout>
<FrameLayout
android:id="@+id/frame2"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
<include layout="@layout/merge_pager" />
</FrameLayout>
</LinearLayout>
Merge layout (merge_pager.xml
):
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</merge>
Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
FrameLayout frame1 = (FrameLayout) findViewById(R.id.frame1);
FrameLayout frame2 = (FrameLayout) findViewById(R.id.frame2);
ViewPager vp1 = (ViewPager) frame1.getChildAt(0);
ViewPager vp2 = (ViewPager) frame2.getChildAt(0);
if (vp1 == null | vp2 == null) {
throw new NullPointerException();
}
vp1.setAdapter(new ColorAdapter(getSupportFragmentManager()));
vp2.setAdapter(new ColorAdapter(getSupportFragmentManager()));
}
private class ColorAdapter extends FragmentStatePagerAdapter {
public ColorAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int i) {
return new ColorFragment();
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
}
public static class ColorFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView tv = new TextView(container.getContext());
Drawable d = new ColorDrawable(randomColor());
tv.setBackgroundDrawable(d);
return tv;
}
public static int randomColor() {
return Color.rgb(random255(), random255(), random255());
}
public static int random255() {
return
(int) (Math.random() * (double) 255);
}
Using this code, the second ViewPager won't work. I suspected it might be due to clashing ids (R.id.view_pager
), so I removed the id from the merge file. However, this results in a fatal exception:
FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: Unable to find resource ID #0xffffffff
What is up with this?
Upvotes: 1
Views: 246
Reputation: 7338
It says in the docs:
When using FragmentPagerAdapter the host ViewPager must have a valid ID set.
So at least it's documented that the adapter won't work without an id for its pager. I suppose you can then take 'valid' to mean 'unique in the activity view hierarchy'.
Upvotes: 1