Android viewpager 滞后于回收器视图



>我有包含 3 个片段的 viewpager,在这些片段中没有图像视图,只有简单的单元格。当我切换到更长的片段(回收器视图有更多项目(时,查看页是滞后的。

我录制视频是为了更好地理解:https://youtu.be/6IMzItswBGo

还有我的自定义视图页,用于调整页面高度。

public class CustomPager extends ViewPager
{
private int width = 0;
public CustomPager(Context context) {
super(context);
}
public CustomPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int height = 0;
width = widthMeasureSpec;
if (getChildCount() > getCurrentItem()) {
View child = getChildAt(getCurrentItem());
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if(h > height) height = h;
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void onRefresh()
{
try {
int height = 0;
if (getChildCount() > getCurrentItem()) {
View child = getChildAt(getCurrentItem());
child.measure(width, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if(h > height) height = h;
}
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
ViewGroup.LayoutParams layoutParams = this.getLayoutParams();
layoutParams.height = heightMeasureSpec;
this.setLayoutParams(layoutParams);
} catch (Exception e) {
e.printStackTrace();
}
}
}

我正在使用动态高度视图页面

使用此代码在设置 viewpager 适配器时预加载所有 3 个片段。

viewPager.setOffScreenPageLimit(3);

似乎onMeasure()方法需要太多时间。我想知道为什么要将视图页面的高度更改为其子高度?

最新更新