Skip to content
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

使用ViewTreeObserver.OnPreDrawListener接口计算totalDistance一些场景有比较大的问题 #5

Closed
momoandy opened this issue May 22, 2017 · 1 comment

Comments

@momoandy
Copy link

momoandy commented May 22, 2017

我的使用场景如下:
ViewPager+Fragment,Fragment中使用LoopViewPagerLayout。
正常使用以及滑动VP没问题;当快速滑动VP时,LoopViewPagerLayout无法释放ViewTreeObserver.OnPreDrawListener接口,导致无限调用一下代码:

public boolean onPreDraw() {
            Rect firstRect = new Rect();
            indicatorLayout.getChildAt(0).getGlobalVisibleRect(firstRect);
            L.e("firstRect = " + firstRect.toShortString());
            Rect lastRect = new Rect();
            indicatorLayout.getChildAt(indicators.length - 1).getGlobalVisibleRect(lastRect);
            L.e("lastRect = " + lastRect.toShortString());
            totalDistance = lastRect.left - firstRect.left;
            L.e("totalDistance = " + totalDistance);
            indicatorLayout.getViewTreeObserver().removeOnPreDrawListener(this);
            return false;
}

我的建议是放弃OnPreDrawListener接口,使用如下代码计算即可:

totalDistance = 2 * size * (indicators.length-1);
@why168
Copy link
Owner

why168 commented Jun 16, 2017

@momoandy good idea!

我这种方法是动态计算总距离。

这是凑巧了!

totalDistance = 2 * size * (indicators.length-1);

indicators的大小,marginRight的大小刚好是size,你说的这个计算总距离刚刚好!
不错,不错

打算使用你的这种方式。

@why168 why168 closed this as completed Jun 16, 2017
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

2 participants