我正在使用FragmentPagerAdapter来调整viewPager中的片段。我编写代码并且它工作正常,这是我的代码
public class SPStoryDetailsAdapter extends FragmentPagerAdapter {
Context context;
public ArrayList<SPStoryContents> storyContents;
public SPStoryDetailsAdapter(ArrayList<SPStoryContents> storyContents, Context ctx, FragmentManager fm) {
super(fm);
context = ctx;
this.storyContents = storyContents;
}
@Override
public Fragment getItem(int position) {
return SPStoryDetailsFragment.newInstance(storyContents.get(position));
}
@Override
public int getCount() {
return 2; // I just test it with two elements
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
我这样称这个适配器。
pagerAdapter = new SPStoryDetailsAdapter(storyContents, this, getSupportFragmentManager());
pager = findViewById(R.id.viewPager);
pager.setAdapter(pagerAdapter);
pager.setPageTransformer(true, new SPSimpleTransformation());
pager.post(() -> pager.setCurrentItem(currentPagerIndex,false));
pageIndicatorView = findViewById(R.id.pageIndicatorView);
pageIndicatorView.attachToPager(pager);
pageIndicatorView.setVisibleDotCount(5);
pageIndicatorView.setVisibility(storyContents.size() > 1 ? View.VISIBLE : View.GONE);
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
这是我的孩子片段代码
public class SPStoryDetailsFragment extends Fragment {
private View view;
public static SPStoryDetailsFragment newInstance(SPStoryContents storyContents) {
SPStoryDetailsFragment f = new SPStoryDetailsFragment();
Bundle b = new Bundle();
b.putSerializable("storyContent", storyContents);
f.setArguments(b);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.sp_story_details_fragment, container, false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SPLogger.logMassage("viewVisibility", "mediaType");
}
@Override
public void onStop() {
super.onStop();
SPLogger.logMassage("liveEvent", "onStop");
if (youTubePlayerView != null) {
youTubePlayerView.pauseYoutubeVideo();
}
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (!isVisibleToUser && isResumed()) {
SPLogger.logMassage("viewVisibility",
youTubePlayerView == null ? "isNull" : "notNull2");
SPLogger.logMassage("viewVisibility", "Call code when Fragment not visible");
// Call code when Fragment not visible
} else if (isVisibleToUser && isResumed()) {
// Call code when Fragment becomes visible.
SPLogger.logMassage("viewVisibility",
youTubePlayerView == null ? "isNull" : "notNull");
SPLogger.logMassage("viewVisibility", "Call code when Fragment becomes visible");
}
if (youTubePlayerView != null) {
youTubePlayerView.pauseYoutubeVideo();
}
if (initializedYouTubePlayer != null)
initializedYouTubePlayer.pause();
}
我试着解释我的问题。正如我所说viewpager工作正常,但是当viewPager启动时,两个子片段都已启动(在日志中,我可以显示viewVisibility两次)。当页面是当前页面时,是否以任何方式打印一次?
ViewPager
is为滑动功能做好准备。因此,他需要至少1个关闭屏幕页面才能为此做好准备。因此,解决方案是执行pager.setOffscreenPageLimit(0)
,但这是不可能的,最低页面限制大小为1.如果您需要在显示第二页之前在第一页上执行某些操作,请尝试其他方法。例如,从滑动锁定页面直到某些条件得到验证。