0

How to force compose' LazyColumn to act like traditional scrollable elements like RecyclerView or ListView?

Useful when want to scroll with mouse, e.g. with vysor.

ThinkDeep
  • 519
  • 1
  • 4
  • 19
  • If you wanna share your knowledge, on SO it should be done in Q&A way: edit your question by describing what're you trying to do, and post your solution as an answer. – Phil Dukhov Mar 07 '22 at 02:40

1 Answers1

0

The solution is to add filter to modifier

const val VERTICAL_SCROLL_MULTIPLIER = -4

// Helps with scrolling with mouse wheel (just like recycler view/list view without compose)
@ExperimentalComposeUiApi
@Composable
fun MyLazyColumn(
    modifier: Modifier = Modifier,
    state: LazyListState, // rememberLazyListState()
    content: LazyListScope.() -> Unit
) {
    LazyColumn(
        state = state,
        modifier = modifier
            .pointerInteropFilter {
                if (it.action == MotionEvent.ACTION_SCROLL) {
                    state.dispatchRawDelta(it.getAxisValue(MotionEvent.AXIS_VSCROLL) * VERTICAL_SCROLL_MULTIPLIER)
                }
                false
            },
        content = content
    )
}
ThinkDeep
  • 519
  • 1
  • 4
  • 19