使用NestedScrollView嵌套RecycleView

使用原因

  • 项目需要。。。
  • 不推荐这么用,原因:链接

使用过程中的问题记录

xml布局文件如下

...
    <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
            <TextView
                    android:padding="15dp"
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:background="@color/colorAccent"
                    android:gravity="center"
                    android:text="嵌套在NestedScrollView里面的头部"
                    android:textColor="#fff"
                    android:textSize="20dp" />

            <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
...

1.自动滚动问题

解决办法同ScrollView一样,在Scrollview包裹的子View添加如下属性:

      android:descendantFocusability="blocksDescendants"

2.RecycleView复用机制失效问题

  • 导致出现这种问题的原因:
    链接
  • 解决方案:高度写死(当然为了适配也不能这么干)。
    在视图完成初始化的时候,通过下面方式,获取到视图高度,设置给recycleView
  flContainer.viewTreeObserver.addOnGlobalLayoutListener (object:ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                val height = flContainer.measuredHeight
                val layoutParams = recyclerView.layoutParams
                if (layoutParams.height<0){
                    layoutParams.height = height
                    recyclerView.layoutParams = layoutParams
                    flContainer.viewTreeObserver.removeOnGlobalLayoutListener(this)
                }
            }
        })
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 翻译自“Collection View Programming Guide for iOS” 0 关于iOS集合视...
    lakerszhy阅读 3,919评论 1 22
  • 1 UIScrollView原理 在滚动过程当中,其实是在修改原点坐标。当手指触摸后,scroll view...
    Kevin_Junbaozi阅读 1,022评论 0 1
  • 清晨的阳光,透过了黑暗的天空。 映射出金黄色的光柱。 太阳神,伸伸懒腰,绽开了笑容。 向日葵们也注视着他,庆祝新的...
    霄空阅读 444评论 0 1
  • 记得在初中时期历史老师说过:每个同学,特别是身在重点班的尖子生,你们应该要修炼一点中华民族文化底蕴,将来有机会你们...
    林小椰阅读 1,228评论 0 1
  • 家里小院二十多年来一直是颓败不堪,与我们一贯示人的形象实不相符,决定收拾。 第一步把门垛改下,一心想着快,那就弄个...
    自在的絮叨阅读 172评论 0 0