碰到几个问题记录一下
布局xml大致是这样的
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<!-- 协调者布局 -->
<androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.appbar.CollapsingToolbarLayout
app:layout_scrollFlags="exitUntilCollapsed|scroll">
<!-- 会被下面列表顶上去的标题部分 -->
<androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<!-- 下面列表 -->
<androidx.recyclerview.widget.RecyclerView
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
上面是固定的标题,下面是列表,很正常的一种写法,然后就碰到了很多问题
(一)SwipeRefreshView和CoordinatorLayout滑动发生冲突
代码里控制SwipeRefreshView是否可用即可解决
// 解决CoordinatorLayout与SwipeRefresh冲突
app_bar_layout.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
swipe_refresh.isEnabled = verticalOffset == 0
})
(二)CoordinatorLayout里的RecylerView里再嵌套RecylerView滑动时的坑
滑动外部RecylerView没有问题,顶部标题会被顶上去;但是滑动内部RecylerView会导致外部RecylerView滑动,而顶部标题不顶上去。给内部RecylerView加上一行android:nestedScrollingEnabled="false"
即可解决
(三)只有AppBar部分可以滑动,RecyclerView无法滑动
如果数据够多,RecyclerView是可以滑动的,所以怀疑是高度的问题,把高度改成match_parent
即可
- 给RecyclerView设置LayoutManager
- 修改RecyclerView布局属性
android:layout_height="match_parent"
暂时只遇到这些坑,但以后会有更多的,蛇皮安卓