我正在尝试使用Fragment,ViewPager和co。来自Android支持库v4。当我从寻呼机听页面更改事件时,片段似乎在滚动,但是内容不可见。我基本上拥有的是:

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) {

    }

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);
    }

}

还有三个看起来像这样的片段:
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);
    }
}

它们具有如下布局:
<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>

我最初忘了添加主要布局,它来了:
<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>

如果有人可以告诉我我在做什么错,我将不胜感激。

最佳答案

这是因为您已在isViewFromObject中覆盖了MyPagerAdapter
FragmentPagerAdapter处理isViewFromObject,因此您不应覆盖它。

和我一样,我认为这篇文章的大多数访问者都是从基本的PagerAdapter开始的,然后意识到他们需要分页片段并更改基类,而没有意识到后果。不幸的是,但是我不能为此批评Android团队。

关于android - 带有FragmentPagerAdapter的ViewPager不显示内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9567741/

10-12 03:56