RecyclerView宽度显示不全问题:RecyclerView和itemview的layout_width属性都是match_parent仍然不行解决方案

本文解决了RecyclerView宽度显示不全的问题。原因在于onCreateViewHolder函数内的LayoutInflater参数设置不当。通过调整参数为(parent, false),成功修复了宽度显示不全的异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:本以为RecyclerView已经用的很熟练了,与scrollview嵌套显示,滑动冲突,RecyclerView嵌套RecyclerView出现滑动卡顿等异常都有遇到过,但没想到今天还是遇到了一个很奇怪的问题:RecyclerView宽度显示不全。

相关问题经验回顾:之前也遇到过一回这样的情况,不过那次很简单排除起来,那次是无意中把把RecyclerView的layout_width属性写成了wrap_content,改写成match_parent就可以了,但这次遇到了相同的问题,再按照上次的解决思路查找问题排除,但没有上次那样的代码疏忽,一时间还真是不知道到底是哪里出现的问题了,后来Google搜索了一番,找到了问题的解决方案,一个真的是平时被我疏忽的一个知识点。

问题出现的原因是在RecyclerView的adapter的onCreateViewHolder函数里面
出现问题之前的代码:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

            var itemView = LayoutInflater.from(actiity).inflate(R.layout.homefra_reylerview_itemview,null)
            return HomeFraItemHolder(itemView)

        }

正确解决问题的代码:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

            var itemView = LayoutInflater.from(actiity).inflate(R.layout.homefra_reylerview_itemview, parent, false)
            return HomeFraItemHolder(itemView)

        }

然后再运行起来就没有那个奇怪的异常了!遇到的坑都是经验,记录下来,以防下次再遇到时蒙头!如果有写的不到之处欢迎留言指出,谢谢!

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_background"> <!-- 标题栏 --> <RelativeLayout android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize" android:background="#44bd32"> <Button android:id="@+id/fanHui" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:background="@android:color/transparent" android:foreground="?android:attr/selectableItemBackground" android:text="返回" android:textColor="@android:color/white" android:textSize="16sp" /> </RelativeLayout> <!-- 底部按钮 --> <LinearLayout android:id="@+id/footer_buttons" android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:background="@android:color/white" android:orientation="horizontal"> <!-- <Button--> <!-- android:id="@+id/device_location"--> <!-- android:layout_width="match_parent"--> <!-- android:layout_height="match_parent"--> <!-- android:gravity="center"--> <!-- android:text="设备定位"--> <!-- android:textSize="19sp"--> <!-- android:background="#44bd32"--> <!-- android:textColor="@android:color/white" />--> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="380dp" android:layout_above="@id/footer_buttons" android:layout_below="@id/toolbar" android:layout_marginBottom="298dp" android:background="@android:color/black" android:padding="0dp"> <!-- 视频播放器 --> <VideoView android:id="@+id/video_player" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" /> <!-- 播放控制条 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#99000000" android:orientation="horizontal" android:padding="8dp"> <ImageButton android:id="@+id/btn_play_pause" android:layout_width="40dp" android:layout_height="40dp" android:background="?attr/selectableItemBackgroundBorderless" android:src="@drawable/ic_play_sel" /> <SeekBar android:id="@+id/video_seekbar" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="00:00/00:00" android:textColor="@android:color/white" android:textSize="14sp" /> </LinearLayout> </RelativeLayout> </RelativeLayout> 在视频播放器模块下面屏幕剩余位置添加一个放置缩略图的模块<ImageView android:layout_width="0dp" android:layout_height="wrap_content" app:layout_columnWeight="1" android:layout_margin="4dp" android:adjustViewBounds="true" android:contentDescription="萤石缩略图" android:src="@drawable/default_thumbnail" />
最新发布
06-27
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值