在新版本中需求变更导致布局需要变化,RecyclerView外需要一层ScrollView来处理滑动。发布前夕发现在API 23 & 24上RecyclerView显示不完整。

  光速冷静下来,马上去stackoverflow翻了一下,有人说ScrollView加上 android:fillViewport="true" ,但是我加上并没有解决问题。后来在RecyclerView外面加了一层RelativeLayout,问题解决。如果你在API 23 & 24上也遇到这个问题,可以参考一下。

<android.support.v4.widget.NestedScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"><!-- DEV NOTE: Outer wrapper relative layout is added intentionally to address issuethat only happens on Marshmallow & Nougat devices (API 23 & 24).On marshmallow API 23, the "RecyclerView" `layout_height="wrap_content"` does NOToccupy the height of all the elements added to it via adapter. The result is cut outitems that is outside of device viewport when it loads initially.Wrapping "RecyclerView" with "RelativeLayout" fixes the issue on Marshmallow devices.--><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><com.jcodecraeer.xrecyclerview.XRecyclerViewandroid:id="@+id/rv_fragment_find_tips_list"android:layout_width="match_parent"android:layout_height="wrap_content" /></RelativeLayout></android.support.v4.widget.NestedScrollView>

  1. import android.content.Context;
  2. import android.support.v7.widget.LinearLayoutManager;
  3. import android.support.v7.widget.RecyclerView;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. /**
  8. * Created by k on 2016/8/13.
  9. */
  10. public class FullyLinearLayoutManager extends LinearLayoutManager {
  11. private static final String TAG = FullyLinearLayoutManager.class.getSimpleName();
  12. public FullyLinearLayoutManager(Context context) {
  13. super(context);
  14. }
  15. public FullyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
  16. super(context, orientation, reverseLayout);
  17. }
  18. private int[] mMeasuredDimension = new int[2];
  19. @Override
  20. public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
  21. int widthSpec, int heightSpec) {
  22. final int widthMode = View.MeasureSpec.getMode(widthSpec);
  23. final int heightMode = View.MeasureSpec.getMode(heightSpec);
  24. final int widthSize = View.MeasureSpec.getSize(widthSpec);
  25. final int heightSize = View.MeasureSpec.getSize(heightSpec);
  26. Log.i(TAG, "onMeasure called. \nwidthMode " + widthMode
  27. + " \nheightMode " + heightSpec
  28. + " \nwidthSize " + widthSize
  29. + " \nheightSize " + heightSize
  30. + " \ngetItemCount() " + getItemCount());
  31. int width = 0;
  32. int height = 0;
  33. for (int i = 0; i < getItemCount(); i++) {
  34. measureScrapChild(recycler, i,
  35. View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
  36. View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
  37. mMeasuredDimension);
  38. if (getOrientation() == HORIZONTAL) {
  39. width = width + mMeasuredDimension[0];
  40. if (i == 0) {
  41. height = mMeasuredDimension[1];
  42. }
  43. } else {
  44. height = height + mMeasuredDimension[1];
  45. if (i == 0) {
  46. width = mMeasuredDimension[0];
  47. }
  48. }
  49. }
  50. switch (widthMode) {
  51. case View.MeasureSpec.EXACTLY:
  52. width = widthSize;
  53. case View.MeasureSpec.AT_MOST:
  54. case View.MeasureSpec.UNSPECIFIED:
  55. }
  56. switch (heightMode) {
  57. case View.MeasureSpec.EXACTLY:
  58. height = heightSize;
  59. case View.MeasureSpec.AT_MOST:
  60. case View.MeasureSpec.UNSPECIFIED:
  61. }
  62. setMeasuredDimension(width, height);
  63. }
  64. private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
  65. int heightSpec, int[] measuredDimension) {
  66. try {
  67. View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsException
  68. if (view != null) {
  69. RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
  70. int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
  71. getPaddingLeft() + getPaddingRight(), p.width);
  72. int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
  73. getPaddingTop() + getPaddingBottom(), p.height);
  74. view.measure(childWidthSpec, childHeightSpec);
  75. measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
  76. measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
  77. recycler.recycleView(view);
  78. }
  79. } catch (Exception e) {
  80. e.printStackTrace();
  81. } finally {
  82. }
  83. }
  84. }
然后在代码里添加如下:
[html] view plain copy
  1. FullyLinearLayoutManager linearLayoutManager = new FullyLinearLayoutManager(this);
  2. recyclerView.setNestedScrollingEnabled(false);
  3. //设置布局管理器
  4. recyclerView.setLayoutManager(linearLayoutManager);
												

关于ScrollView嵌套RecyclerView时RecyclerView不显示的问题相关推荐

  1. Android ScrollView嵌套GridView导致GridView只显示一行item

    Android ScrollView嵌套GridView导致GridView只显示一行item Android ScrollView在嵌套GridView时候,会导致一个问题发生:GridView只显 ...

  2. 解决ScrollView嵌套RecyclerView出现item显示不全的问题

    解决ScrollView嵌套RecyclerView出现item显示不全的问题 参考文章: (1)解决ScrollView嵌套RecyclerView出现item显示不全的问题 (2)https:// ...

  3. Android RecyclerView和ScrollView嵌套使用

    我们的recyclerView有多个layoutmanager,通过重写layoutmanager的方法就可以让recyclerView和ScrollView嵌套了.但是请注意,如果recyclerV ...

  4. NestedScrollView/ScrollView嵌套Recyclerview,导致Recyclerview抢占焦点,置顶了, 修改好后,引起Edittext又不能点击问题处理

    NestedScrollView/ScrollView嵌套Recyclerview,导致Recyclerview抢占焦点,置顶了, 修改好后,引起Edittext又不能点击问题处理 NestedScr ...

  5. Android Scrollview嵌套RecyclerView导致滑动卡顿问题解决(屡试不爽)

    今天开发的一个项目首页,布局还是比较复杂的,各种滑动冲突,(Banner+横向RecyclerView+纵向RecyclerView(item又是横向的RecyclerView)),  最外面的框架用 ...

  6. SwipeRefreshLayout+CoordinatorLayout+AppBarLayout+TabLayout+ViewPager+RecyclerView 嵌套,最后一项显示全

    SwipeRefreshLayout+CoordinatorLayout+AppBarLayout+TabLayout+ViewPager+RecyclerView 嵌套,导致RecyclerView ...

  7. Android RecyclerView 向上偏移解决、添加自定义分割线,瀑布流,ScrollView嵌套RecyclerView滑动卡顿

    向上偏移解决 android:focusableInTouchMode="true" android:focusable="true" android:fill ...

  8. android中ScrollView嵌套ListView或GridView显示位置问题

    Android中ScrollView中嵌套ListView或GridView时在开始进入界面时总是显示中间位置,开头的位置显示不出来.这种情况下只需要在ScrollView的父控件中添加以下两行代码即 ...

  9. android item list居中,RecyclerView选中item居中显示

    使用RecyclerView时需要将选中的item在View中居中显示,RecyclerView的scrollToPosition(position)方法只会将position所对应的item滚动到屏 ...

最新文章

  1. TANDEM 基于深度多视图立体视觉的实时跟踪和稠密建图
  2. R语言ggplot2可视化水平条形图的标题(title)、副标题(subtitle)和图片说明信息(caption)左对齐实战
  3. redhat安装wine教程_可能是最漂亮的国产Linux,U盘安装DeepinLinux 深度操作系统
  4. 反转!物联网火爆,开发者却很难入门?
  5. python简短语法_python 简单语法入门
  6. python 表名代码编码格式_14行代码示例讲解:教你解决python上传图片限制格式问题...
  7. 快速格式化代码(HTML、CSS)
  8. macos推荐使用的敲代码软件
  9. Lock接口和AQS原理与实现(Java并发编程的艺术整理)
  10. php模拟post提交 在线,curl post请求 , postman 模拟请求 , 在线测试工具模拟请求...
  11. 人脸识别之人脸识别技术综述
  12. Qt中容器类型控件介绍
  13. echarts柱状图加上数量
  14. 计算机组成原理 启航教育,2021计算机考研:计算机组成原理知识点CPU的功能和基本结构...
  15. mac 小程序开发者工具。 tunneling socket could not be established
  16. “大数据”爆发时代,国内存储产业加速发展
  17. Scala类和对象详解
  18. 记忆化结果再利用 进一步探讨递推关系
  19. 3.1.1计算机网络(数据链路层 差错控制 循环冗余码 海明码 流量控制)
  20. postman打开启动慢,原因:长时间没清理缓存,解决:删除postma缓存重启即可

热门文章

  1. 提取文章中的人名php,php中实现将姓名拆分为姓氏和名字
  2. arcgis如何打开tif_ArcGIS的目录、内容列表的布局
  3. 使用Wechaty实现微信机器人操作
  4. 硬盘插上后计算机无法识别,移动硬盘插入Mac后,在Windows无法识别
  5. Metal(二) Metal语法规范
  6. 读书是一种修养 ——《大河奔流的精神》(俞敏洪著)读后感
  7. android开发笔记之锁屏界面未读短信未接来电提醒(android 4.4)
  8. 互联网大厂的后端技术栈
  9. 问题排查 —— OLAP平台获取查询引擎连接严重耗时
  10. vr 计算机配置,能玩VR的电脑装机 | 装机配置分享_什么值得买