如何解决使用ViewPager2 + NestedScrollView + Horizontal RecyclerView时发生滚动冲突
MainActivity
包含一个ViewPager2
,其中填充了相同Fragment的实例。该片段包含一个nestedScrollView
,其中包含一些详细信息(文本,图像等),在底部有一个Horizontal RecyclerView
。该应用程序的设计类似于OLX应用程序。
问题是,当我尝试在RecyclerView
上向左或向右滑动时,ViewPager会消耗该滑动,从而切换片段。
片段布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/coordinatorLayout">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsingToolbarLayout">
<ImageView
android:id="@+id/backdropImageView"/>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.nestedScrollView
android:id="@+id/nestedScrollView"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout">
<Some other views: TextView,ImageView</>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView_with_problems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/someTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.nestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
这是我尝试过的:
recyclerView_with_problems.addOnItemTouchListener(object : RecyclerView.OnItemTouchListener {
override fun onInterceptTouchEvent(rv: RecyclerView,e: MotionEvent): Boolean {
when (e.action) {
MotionEvent.ACTION_DOWN -> rv.parent.requestdisallowInterceptTouchEvent(true)
}
return false
}
override fun onTouchEvent(@NonNull rv: RecyclerView,@NonNull e: MotionEvent) {}
override fun onRequestdisallowInterceptTouchEvent(disallowIntercept: Boolean) {}
})
通过此操作,我可以在RecyclerView上向左/向右滑动,但是我再也不能在RecyclerView上向上/向下滑动,这根本不是用户友好的。
还有其他人遇到过这个问题,愿意帮我吗?谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。