我是靠谱客的博主 知性太阳,最近开发中收集的这篇文章主要介绍Pages must fill the whole ViewPager2 (use match_parent),觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
从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
}
};
}
如果子view的宽和高不是Match_parent就会抛出异常 ,我们在看下enforceChildFillListener
mRecyclerView.addOnChildAttachStateChangeListener(enforceChildFillListener());
一个注册监听,我们在看下addOnChildAttachStateChangeListener方法
public void addOnChildAttachStateChangeListener(
@NonNull OnChildAttachStateChangeListener listener) {
if (mOnChildAttachStateListeners == null) {
mOnChildAttachStateListeners = new ArrayList<>();
}
mOnChildAttachStateListeners.add(listener);
}
这个方法就是注册一个监听器,当子视图附加到RecyclerView或从RecyclerView分离时,该监听器就会收到通知, 问题的原因找到了,那么问题就好解决了。
归根结底就是子view的宽和高并没有起到效果,虽然在xml文件中设置了,类似这样
<?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" />
或者这样都是不行的
<?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>
解决方案有两种:
第一种就是
View inflate = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_guide_img, parent,false)
// 我们给view设置父布局
第二种:
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 must fill the whole ViewPager2 (use match_parent)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复