ItemTouchHelper 滚动在嵌套在水平滚动视图中的 recyclerView 中不起作用
ItemTouchHelper scroll doesn't work in recyclerView that is nested in a horizontal Scroll view
我在自己的片段中的 LinearLayout 中有一个水平的 recyclerView。片段的占位符包装在 horizontalScrollView 中。当我添加片段时,recyclerview nestedScroll 设置为 false,HorizontalScrollView 控制滚动。
但是,我现在已经在 recyclerview 上实现了 ItemTouchHelper.Callback,以便能够重新排序单元格。但是,当我将一个单元格移出屏幕时,它不会随之滚动。我试过更改 nestedScroll 和 fixedSize 但没有任何效果。
我不能使用 NestedScrollView,因为 recyclerView 是水平的吗?
任何建议
主要xml
<HorizontalScrollView
android:id="@+id/timeline_horizontal_scroll_view"
style="@style/timeline_horizontal_scroll_view_style">
<FrameLayout
android:id="@+id/media_scrub_placeholder"
style="@style/media_scrub_placeholder_style" />
</HorizontalScrollView>
片段xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/timeline_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
我能够在我的片段中使用它来解决这个问题
reorderTimelineRecyclerView.layoutManager = LinearLayoutManager(context!!, LinearLayoutManager.HORIZONTAL, false)
reorderTimelineRecyclerView.isNestedScrollingEnabled = true
broadcastScrollState(true, context!!)
val callback = SimpleItemTouchHelperCallback(adapter, context!!, listOfLocalAssets.size)
val touchHelper = ItemTouchHelper(callback)
touchHelper.attachToRecyclerView(reorderTimelineRecyclerView)
broadcastScrollState 将回收器视图的滚动更新为在重新排序状态下启用,然后将其关闭。因此,当用户长按时,它使用 rcecyclerview 的滚动,但当不长按时,它使用水平滚动视图的滚动。希望这对某人有所帮助!
编辑
fun broadcastScrollState(scrollState: Boolean, context: Context) {
val intent = Intent("scroll-state-event")
intent.putExtra("scroll-state", scrollState)
LocalBroadcastManager.getInstance(context!!).sendBroadcast(intent)
}
然后在带有 recycerlview 的片段中,我观察互联网并切换滚动视图的滚动状态
private val scrollReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
updateScrollState(intent!!.getBooleanExtra("scroll-state", false))
}
}
private fun updateScrollState(scrollState: Boolean) {
timeline_horizontal_scroll_view.isEnabled = !scrollState
}
我在自己的片段中的 LinearLayout 中有一个水平的 recyclerView。片段的占位符包装在 horizontalScrollView 中。当我添加片段时,recyclerview nestedScroll 设置为 false,HorizontalScrollView 控制滚动。
但是,我现在已经在 recyclerview 上实现了 ItemTouchHelper.Callback,以便能够重新排序单元格。但是,当我将一个单元格移出屏幕时,它不会随之滚动。我试过更改 nestedScroll 和 fixedSize 但没有任何效果。
我不能使用 NestedScrollView,因为 recyclerView 是水平的吗?
任何建议
主要xml
<HorizontalScrollView
android:id="@+id/timeline_horizontal_scroll_view"
style="@style/timeline_horizontal_scroll_view_style">
<FrameLayout
android:id="@+id/media_scrub_placeholder"
style="@style/media_scrub_placeholder_style" />
</HorizontalScrollView>
片段xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/timeline_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
我能够在我的片段中使用它来解决这个问题
reorderTimelineRecyclerView.layoutManager = LinearLayoutManager(context!!, LinearLayoutManager.HORIZONTAL, false)
reorderTimelineRecyclerView.isNestedScrollingEnabled = true
broadcastScrollState(true, context!!)
val callback = SimpleItemTouchHelperCallback(adapter, context!!, listOfLocalAssets.size)
val touchHelper = ItemTouchHelper(callback)
touchHelper.attachToRecyclerView(reorderTimelineRecyclerView)
broadcastScrollState 将回收器视图的滚动更新为在重新排序状态下启用,然后将其关闭。因此,当用户长按时,它使用 rcecyclerview 的滚动,但当不长按时,它使用水平滚动视图的滚动。希望这对某人有所帮助!
编辑
fun broadcastScrollState(scrollState: Boolean, context: Context) {
val intent = Intent("scroll-state-event")
intent.putExtra("scroll-state", scrollState)
LocalBroadcastManager.getInstance(context!!).sendBroadcast(intent)
}
然后在带有 recycerlview 的片段中,我观察互联网并切换滚动视图的滚动状态
private val scrollReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
updateScrollState(intent!!.getBooleanExtra("scroll-state", false))
}
}
private fun updateScrollState(scrollState: Boolean) {
timeline_horizontal_scroll_view.isEnabled = !scrollState
}