FragmentStatePagerAdapter 项目不加载

FragmentStatePagerAdapter items don't load

在我的主 activity 中,我有多个片段,您可以通过底部导航进行导航(替换主 activity XML 中的 FrameLayout 容器)

其中一个片段包含一个 ViewPager,它应该使用 FragmentStatePagerAdapter (MyAdapter) 显示多个片段 (MyFragment)。

我第一次打开该 Fragment 时它工作得很好,但是当我导航到另一个 Fragment 然后返回时,Pager 中的前两个 Fragment 是灰色且未填充的。有人知道问题出在哪里吗?

适配器设置如下: 这在 onFragmentViewCreated

中调用
private void setUpHeaderPager() {
    featuredAdapter = new MyAdapter(getActivity().getSupportFragmentManager());
    myPager.setAdapter(myAdapter);
    pageIndicatorView.setViewPager(myPager);
    pageIndicatorView.setAnimationType(AnimationType.WORM);
    compositeSubscription.add(apiService.getMyPOJOs()
            .subscribeOn(networkScheduler)
            .observeOn(uiScheduler)
            .subscribe(new Action1<List<MyPOJO>>() {
                @Override
                public void call(List<MyPOJO> myPOJOs) {
                    myAdapter.setItems(myPOJOs);
                    pageIndicatorView.setCount(myPOJOs.size());
                    myPager.setCurrentItem(0);
                    pageIndicatorView.setSelection(0);
                }
            }, new Action1<Throwable>() {
                @Override
                public void call(Throwable throwable) {
                    throwable.printStackTrace();
                }
            }));
}

这就是适配器的样子:

private class MyAdapter extends FragmentStatePagerAdapter {

    private List<SomePOJO> items = new ArrayList<>(0);

    public void setItems(@Nonnull List<Collection> items) {
        this.items = items;
        notifyDataSetChanged();
    }

    FeaturedReleasesAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public Fragment getItem(int position) {
        return MyFragment.newInstance(items.get(position));
    }

    @Override
    public int getCount() {
        return items.size();
    }
}

万一MyFragment的相关代码:

public class FeaturedReleaseFragment extends BaseFragment {

private MyPOJO someObject = null;

// Bind some views

public static MyFragment newInstance(MyPOJO someObject) {
    MyFragment fragment = new MyFragment();
    fragment.someObject = someObject;
    return fragment;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_my_fragment, container, false);

    ButterKnife.bind(this, rootView);

    populate();

    return rootView;
}

private void populate() {
    if (MyPOJO != null) {
        // set some text and load images
    }
}

onFragmentViewCreated 被调用就好了,适配器 getItem 不是...

这可能是您在底部导航片段之间导航的方式以及随后它们生命周期中发生的事情。

如果您在 FragmentTransaction 上使用 .replace,请尝试改用 .show.hide

可能会执行以下操作:

public void navigateToFragment( Fragment frag ) {
     FragmentTransaction transaction = getFragmentManager().beginTransaction();

    transaction.hide(currentFragment);

    if ( !fragment.isAdded() ) {
        transaction.add(R.id.container,fragment)
    } else if ( !fragment.isHidden() ) {
        transaction.show(fragment);
    }
    transation.commit();
}