RecyclerView在ScrollView中使用wrap_content
来源:互联网 发布:fm 迭戈科斯塔数据 编辑:程序博客网 时间:2024/06/08 03:33
RecyclerView如果在ScrollView中使用wrap_content,那么有可能会出现高度为0的情况,为了解决这个问题,我们要手动测量高度并且设置高度,RecyclerView测量工作都是交个LayoutManager的,所以我们要从LayoutManager下手。
以LinearLayoutManager为例子,重写onMeasure方法:
public class AutoSizeLayoutManager extends LinearLayoutManager{ public AutoSizeLayoutManager(Context context) { super(context); } public AutoSizeLayoutManager(Context context, int orientation, boolean reverseLayout) { super(context, orientation, reverseLayout); } public AutoSizeLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) { int itemCount = getItemCount(); if (itemCount == 0) { super.onMeasure(recycler, state, widthSpec, heightSpec); return; } //测量每个item,高度相加 int measuredWidth = 0; int measuredHeight = 0; for (int i = 0; i < itemCount; i++) { View view = recycler.getViewForPosition(i); if (view != null) { measureChild(view, widthSpec, heightSpec); measuredWidth += view.getMeasuredWidth(); measuredHeight += view.getMeasuredHeight(); } } //if (!isAutoWidth){//使用原来的宽度 // measuredWidth = View.MeasureSpec.getSize(widthSpec); //} //if (!isAutoHeight){//使用原来的高度 // measuredHeight = View.MeasureSpec.getSize(heightSpec); //} setMeasuredDimension(measuredWidth, measuredHeight); }}
声明
原创文章,欢迎转载,请保留出处。有任何错误、疑问或者建议,欢迎指出。我的邮箱:Maxwell_nc@163.com
0 0
- RecyclerView在ScrollView中使用wrap_content
- RecyclerView wrap_content
- Android 6.0+ RecyclerView嵌套在ScrollView中显示不全
- 在ScrollView中使用ListView
- 在kotlin中使用RecyclerView
- ScrollView中嵌套RecyclerView问题
- Scrollview中嵌套Recyclerview问题
- RecyclerView和ScrollView嵌套使用
- 【Android】RecyclerView嵌套ScrollVIew使用
- RecyclerView和ScrollView嵌套使用
- RecyclerView和ScrollView嵌套使用
- RecyclerView和ScrollView嵌套使用
- RecyclerView和ScrollView嵌套使用
- SwipeRefreshLayout、ScrollView、RecyclerView嵌套使用
- Android中Recyclerview使用10----Recyclerview外面嵌套ScrollView滑动事件冲突
- GetClipBox 在scrollview中使用注意事项
- 怎么在ScrollView中使用ListView
- 在ScrollView中使用GridView和ListView
- poj2985The k-th Largest Group
- 第一次写博客
- 弹出窗重新加载页面错误问题
- Java中Class类浅谈
- linux kernel的中断子系统之(八):softirq
- RecyclerView在ScrollView中使用wrap_content
- CDN 技术研究——Cache集群通信
- XStream解析xml,并分享
- Java中的抽象类和接口
- 叠加图片切片工具:可以处理大图了
- 《Python核心编程》读书笔记—正则表达式
- jQuery异步框架探究2:jQuery.Deferred方法
- Android 添加自定义的.ttf字库文件到系统
- js中设置显示和隐藏