SmartRefreshLayout嵌套Scrollview嵌套RecyclerView出现IndexOutOfBoundsException
时间: 2023-11-16 18:58:53 浏览: 56
在嵌套使用 SmartRefreshLayout、ScrollView 和 RecyclerView 时,可能会出现 IndexOutOfBoundsException 异常。这是因为 RecyclerView 的高度设置可能会与其内部的子项高度计算不一致,导致出现异常。
解决方法是在 RecyclerView 的父布局中设置固定高度,或者将 RecyclerView 的高度设置为 wrap_content。另外,也可以尝试使用 NestedScrollView 替换 ScrollView,以便更好地支持嵌套滚动。
示例代码:
```
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
```