使用NestedScrollView嵌套RecycleView

2020-04-17  本文已影响0人  卖炭少年炭治郎

使用原因

使用过程中的问题记录

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复用机制失效问题

  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)
                }
            }
        })
上一篇 下一篇

猜你喜欢

热点阅读