I'm trying to use the Fragment, ViewPager & co. from the Android support library v4. Fragments seem to scroll when I listen to page change events from the pager but the content is not visible. What I basically have is:
A FragmentActivity:
public class MyActivity extends FragmentActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentPagerAdapter fpa = new MyPagerAdapter(this, createFragments());
ViewPager vp = (ViewPager) findViewById(R.id.pager);
vp.setAdapter(fpa);
}
private List<Fragment> createFragments() {
List<Fragment> list = new ArrayList<Fragment>();
list.add(Fragment.instantiate(this, First.class.getName()));
list.add(Fragment.instantiate(this, Second.class.getName()));
list.add(Fragment.instantiate(this, Third.class.getName()));
return list;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
}
A FragmentPagerAdapter:
public class MyPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPagerAdapter(FragmentActivity activity, List<Fragment> fragments) {
super(activity.getSupportFragmentManager());
this.fragments = fragments;
}
@Override
public Fragment getItem(int i) {
return fragments.get(i);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public boolean isViewFromObject(View view, Object o) {
return view.equals(o);
}
}
And three Fragments that look like this:
public class First extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.first, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
And they have layout like these:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:text="First Fragment"
android:id="@+id/firstText"/>
</LinearLayout>
I originally forgot to add the main layout, here it comes:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="fill_parent"/>
</LinearLayout>
I would appreciate if someone can tell me what I am doing wrong.
This is because you have overridden isViewFromObject
in MyPagerAdapter
.
FragmentPagerAdapter
handles isViewFromObject
, so you shouldnt override it.
As with me, I reckon most visitors to this post started with a basic PagerAdapter
, then realised they needed to page fragments and changed their base class without realising the consequences. Unfortunate, but I couldn't criticise the Android team for this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With