我是靠谱客的博主 知性太阳,这篇文章主要介绍Pages must fill the whole ViewPager2 (use match_parent),现在分享给大家,希望可以做个参考。

从Viewpager2源码中找出抛出异常的代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 } }; }

如果子view的宽和高不是Match_parent就会抛出异常 ,我们在看下enforceChildFillListener

复制代码
1
2
mRecyclerView.addOnChildAttachStateChangeListener(enforceChildFillListener());

一个注册监听,我们在看下addOnChildAttachStateChangeListener方法

复制代码
1
2
3
4
5
6
7
8
public void addOnChildAttachStateChangeListener( @NonNull OnChildAttachStateChangeListener listener) { if (mOnChildAttachStateListeners == null) { mOnChildAttachStateListeners = new ArrayList<>(); } mOnChildAttachStateListeners.add(listener); }

这个方法就是注册一个监听器,当子视图附加到RecyclerView或从RecyclerView分离时,该监听器就会收到通知, 问题的原因找到了,那么问题就好解决了。
归根结底就是子view的宽和高并没有起到效果,虽然在xml文件中设置了,类似这样

复制代码
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?> <androidx.appcompat.widget.AppCompatImageView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/item_guide_img" android:layout_width="match_parent" android:layout_height="match_parent" app:srcCompat="@mipmap/audio_stop" tools:ignore="MissingDefaultResource" />

或者这样都是不行的

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.appcompat.widget.AppCompatImageView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/item_guide_img" android:layout_width="match_parent" android:layout_height="match_parent" app:srcCompat="@mipmap/audio_stop" tools:ignore="MissingDefaultResource" /> </LinearLayout>

解决方案有两种:
第一种就是

复制代码
1
2
3
4
5
View inflate = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_guide_img, parent,false) // 我们给view设置父布局

第二种:

复制代码
1
2
3
4
5
6
7
8
View inflate = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_guide_img, null); ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); inflate.setLayoutParams(layoutParams); //动态设置宽和高

最后

以上就是知性太阳最近收集整理的关于Pages must fill the whole ViewPager2 (use match_parent)的全部内容,更多相关Pages内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部