RecyclerView高度随Item自适应
来源:互联网 发布:跟着龚琳娜学唱歌 淘宝 编辑:程序博客网 时间:2024/06/05 07:17
转载请注明出处:http://write.blog.csdn.net/postedit/40425231
编写RecyclerView.ItemDecoration时,在onDraw方法中,Drawable的高度等于RecyclerView的高度减去RecyclerView的上下padding。
@Overridepublic void onDraw(Canvas c, RecyclerView parent, State state) {int top = parent.getPaddingTop();int bottom = parent.getHeight() - parent.getPaddingBottom();int childCount = parent.getChildCount();for(int i=0;i < childCount;i++){View child = parent.getChildAt(i);RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams)child.getLayoutParams();int left = child.getRight() + layoutParams.rightMargin;int right = left + mDivider.getIntrinsicWidth();mDivider.setBounds(left, top, right, bottom);mDivider.draw(c);}}但运行后的显示效果却和我的预期相差很大
可以看到,ItemDecoration高度竟然全屏了,然后检查xml布局文件:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.xmy.recylerviewdemo.MainActivity" > <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:padding="10.0dip" android:orientation="vertical" > <ImageView android:id="@+id/item_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="center" android:src="@drawable/img" android:adjustViewBounds="true"/> <TextView android:id="@+id/item_tv" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>xml布局文件中RecyclerView和Item的高度都设定的是wrap_content,那说好的自适应于item高度呢?查看Android文档,没发现有关RecyclerView高度相关说明,看来只能自己动手丰衣足食了。
根据Android-RecylerView初识里提到的,RecyclerView并不负责Item的显示工作,而Adapter负责的是数据仓库和Item的视图,所以最终把目标锁定到RecyclerView.LayoutManager上。于是尝试继承LinearLayoutManager,发现果然有onMeasure方法:
public void onMeasure(Recycler recycler, State state, int widthSpec,int heightSpec)在onMeasure中可以获得RecyclerView.Recycler。Recycler负责管理Item视图的重复利用,所以我们可以通过Recycler获取一个Item视图的实例,然后像复写其他ViewGroup一样,使用measureChild获取子视图的高度后使用setMeasuredDimension设置RecyclerView同样的高度即可。
public class MyLayoutManager extends LinearLayoutManager {public MyLayoutManager(Context context) {super(context);// TODO Auto-generated constructor stub}@Overridepublic void onMeasure(Recycler recycler, State state, int widthSpec,int heightSpec) {View view = recycler.getViewForPosition(0);if(view != null){measureChild(view, widthSpec, heightSpec);int measuredWidth = MeasureSpec.getSize(widthSpec);int measuredHeight = view.getMeasuredHeight();setMeasuredDimension(measuredWidth, measuredHeight);}}}修改完之后的运行效果图:
最后奉上示例程序Github链接。
6 0
- RecyclerView高度随Item自适应
- RecyclerView高度随Item自适应
- RecyclerView高度随Item自适应
- RecyclerView高度随Item自适应
- RecyclerView高度随Item自适应 GridLayoutManager和LinearLayoutManager都适用
- 【Android 界面效果49】RecyclerView高度随Item自适应
- recyclerView高度不能自适应(item满屏)
- ScrollView 嵌套 RecyclerView 高度自适应,item显示不全 问题
- Item高度自适应内容
- RecyclerView的自适应高度
- RecyclerView自适应高度
- RecyclerView自适应高度
- 解决 RecyclerView 自适应高度
- RecyclerView 自适应高度
- 简述RecyclerView实现瀑布流并且随机自适应item的高度
- Android GridView自适应Item高度
- RecyclerView自适应高度的LayoutManager
- RecyclerView自适应高度的LinearLayoutManager
- Linux C Sqllite DataBase Options
- Android获取进程是否在后台注意事项
- SQL 错误: ORA-12910
- 通过位运算的异或运算符加密解密
- android的Message机制
- RecyclerView高度随Item自适应
- LeetCode: Maximum Subarray
- 论证是一门学问 如何让你的观点有说服力-13 规则13 列出信息来源
- 基于公里格网的地震灾情快速评估系统
- 链表有关的常见面试题
- 归档模式和非归档模式
- linux 如何查找命令的路径
- 久别,归来
- c++stack容器介绍