Skip to content

viewpager 中使用的问题 #624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
KinaZhen opened this issue Nov 3, 2017 · 7 comments
Closed

viewpager 中使用的问题 #624

KinaZhen opened this issue Nov 3, 2017 · 7 comments

Comments

@KinaZhen
Copy link

KinaZhen commented Nov 3, 2017

在viewpager 中使用fragmentStatePagerAdapter ,共有6个卡页,在调用viewPager.setAdapter 后,继续调用viewPager.setCurrentItem(4) ,(默认显示第5个卡页的意思), 这时 这个卡页下fragment显示正常,然后通过点击第一个卡页tab , 回到第一个卡页下的fragment时, 这个fragment不走onLazyInitView; 请问为什么?

@beilo
Copy link
Contributor

beilo commented Nov 3, 2017

原因是viewPage转载的第一个fragment会先被实例化,所以其实你在mViewPager.setAdapter的时候下一步就已经执行到A的onLazyInitView,这个时候你mViewPager.setCurrentItem(4),其实相当于已经实例化完第1个和第5个fragment了

@YoKeyword YoKeyword reopened this Nov 3, 2017
@YoKeyword
Copy link
Owner

@KinaZhen
viewPager.setAdapter在哪里调用的,onActivityCreated()? onLazyInitView()?

代码贴下

@KinaZhen
Copy link
Author

KinaZhen commented Nov 3, 2017

代码如下

@OverRide
public void onEnterAnimationEnd(Bundle savedInstanceState) {
super.onEnterAnimationEnd(savedInstanceState);
initView();
}

private void initView(){
    ProductCatPagerAdapter productCatPagerAdapter = new     ProductCatPagerAdapter(getChildFragmentManager());
    productCatPagerAdapter.setProductTypeCategories(productTypeCategories);
    productCatPagerAdapter.setiProductCatListener(this);
    viewPager.setAdapter(productCatPagerAdapter);
    viewPager.setCurrentItem(4,false);
}

@YoKeyword
Copy link
Owner

onEnterAnimationEnd()中模拟了下, 在切回第一个Fragment时,可以正常回调onLazyInitView()

@KinaZhen
Copy link
Author

KinaZhen commented Nov 3, 2017

不是在 onEnterAnimationEnd() 中切回第一个,而是 手动点击 tab 切回第一个

@YoKeyword
Copy link
Owner

@KinaZhen 是手动切回, 在onEnterAnimationEnd()中init, 然后手动切回第一个Fragment, 第一个Fragment的onLazyInitView()正常回调

但是确实存在BUG,在这种场景下FragmentStatePagerAdapter 正常,但是FragmentPagerAdapter 第一次不回调, 你再确认下是FragmentStatePagerAdapter还是FragmentPagerAdapter

@YoKeyword
Copy link
Owner

@KinaZhen 排查了下,是v1.1.7这个commit 8b1d7b4 引起的,已修复:commit 6017d60

请更新至v1.1.8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants