FragmentStatePagerAdapter

FragmentStatePagerAdapter

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

这些片段之一包含一个ViewPager,该ViewPager应该使用FragmentStatePagerAdapter(MyAdapter)显示多个片段(MyFragment)。

第一次打开该片段时,它工作得很好,但是当我导航到另一个片段时,又回来时,传呼器中的前两个片段是灰色且没有填充。任何人都知道可能是什么问题吗?

适配器设置如下:
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不是...

最佳答案

这可能是您如何在底部导航片段之间导航,以及随后在其生命周期中发生的事情。

如果在.replace上使用FragmentTransaction,请尝试使用.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();
}

关于android - FragmentStatePagerAdapter项目无法加载,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44946085/

10-09 07:43