查看ViewPager2源码可以发现
private RecyclerView.OnChildAttachStateChangeListener enforceChildFillListener() { return new RecyclerView.OnChildAttachStateChangeListener() { @Override public void onChildViewAttachedToWindow(@NonNull View view) { RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) view.getLayoutParams(); if (layoutParams.width != LayoutParams.MATCH_PARENT || layoutParams.height != LayoutParams.MATCH_PARENT) { throw new IllegalStateException( "Pages must fill the whole ViewPager2 (use match_parent)"); } } @Override public void onChildViewDetachedFromWindow(@NonNull View view) { // nothing } };}
当item的宽或高不为match_parent时就会闪退;
而当ViewPager2结合使用BaseRecyclerViewAdapterHelper的加载更多模式时,就会偶尔出现闪退现象;
闪退的最终原因就是:
LoadMoreModule也是item的一种,而它的布局是默认的,观看源码发现它的高度40dp,而非match_parent,
所以需要我们自定义LoadMoreView
将布局的高度修改为match_parent即可。
之所以我发现这个问题的场景是,运用ViewPager2+BaseRecyclerViewAdapterHelper实现抖音短视频功能