Search code examples
kotlinandroid-jetpack-composemotioneventlazycolumn

LazyColumn that respects MotionEvent.ACTION_SCROLL


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.


Solution

  • 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
        )
    }