今晚在做GreenDao数据库操作需要列表展示读取数据是用到Recycleview列表展示数据,但是奇怪的是Item子布局无法横向铺满父布局。
此BUG出现主要两种错误写法,第一种是Adapter中加载inflater时候parent传入为null。
错误代码:
return new CreateTODOViewHolder(
LayoutInflater.from(context).inflate(R.layout.item_date,null,false));
正确代码:
return new CreateTODOViewHolder(
LayoutInflater.from(context).inflate(R.layout.item_date,parent,false));我犯的是第二种错误,在写Recycleview布局时候未明确宽高,用权重代替。

<com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/recycleview" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"> </com.jcodecraeer.xrecyclerview.XRecyclerView>
解决办法是在RecycleVeiw的最外层添加一个RelativeLayout 布局就可以解决该问题
<RelativeLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/recycleview" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent"> </com.jcodecraeer.xrecyclerview.XRecyclerView> </RelativeLayout>
问题在于Recycleview 的Item子布局在填充的时候会获取父布局的宽高 ,但是原来错误写法没有给明确的高度而是用权重代替高度导致子布局在获取父布局的宽高出现问题 只要在Recycleview外层添加一个RelativeLayout就可以解决。
大家加油!!!

本文解决了一个关于使用RecyclerView展示数据时,Item子布局无法横向铺满父布局的问题。问题出现的原因有两种:一是Adapter加载inflater时parent参数传入null;二是RecyclerView布局中未明确宽高,用权重代替。
2276

被折叠的 条评论
为什么被折叠?



