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--removeOnGlobalLayoutListener()在Android4.2以下设备崩溃的问题及解决方案 #9

Open
gxj0116 opened this issue Feb 18, 2022 · 0 comments

Comments

@gxj0116
Copy link

gxj0116 commented Feb 18, 2022

原来代码:
recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@OverRide
public void onGlobalLayout() {
recyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int recyclerviewHeight = recyclerView.getHeight();
resizeHeight(recyclerviewHeight);
}
}); 以上代码在Android4.2以下Android设备会崩溃,建议修改为以下代码:recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener() { @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { recyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this); } else { recyclerView.getViewTreeObserver().removeGlobalOnLayoutListener(this); } int recyclerviewHeight = recyclerView.getHeight(); resizeHeight(recyclerviewHeight); } });

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

1 participant