diff --git a/android/src/main/java/me/sectiontn/flume/FlumeRecyclerView.kt b/android/src/main/java/me/sectiontn/flume/FlumeRecyclerView.kt index 95ba61b..aafc8ae 100644 --- a/android/src/main/java/me/sectiontn/flume/FlumeRecyclerView.kt +++ b/android/src/main/java/me/sectiontn/flume/FlumeRecyclerView.kt @@ -133,6 +133,11 @@ class FlumeRecyclerView(context: Context) : invalidate() } + // ScrollView's flashScrollIndicators: the scroll bars show, then fade as after a scroll. + fun flashScrollIndicators() { + awakenScrollBars() + } + // Under the decorations, so a separator line draws over it. override fun onDraw(c: Canvas) { if (Color.alpha(contentBackground) != 0) drawContentBackground(c) diff --git a/android/src/main/java/me/sectiontn/flume/FlumeView.kt b/android/src/main/java/me/sectiontn/flume/FlumeView.kt index 842f47f..21672df 100644 --- a/android/src/main/java/me/sectiontn/flume/FlumeView.kt +++ b/android/src/main/java/me/sectiontn/flume/FlumeView.kt @@ -777,6 +777,14 @@ class FlumeView(context: Context) : FrameLayout(context) { requestJump(JumpTarget.End, animated, 1f, 0.0) } + // ScrollView's scrollTo carries no command id: it ends the command in flight, then moves along the list's axis. + fun scrollToPoint(xDp: Double, yDp: Double, animated: Boolean) { + endScrollCommand() + scrollToOffset(0, if (geometry.horizontal) xDp else yDp, animated) + } + + fun flashScrollIndicators() = recycler.flashScrollIndicators() + fun setListOverScrollMode(value: String?) { recycler.overScrollMode = OverScroll.mode(value) } diff --git a/android/src/main/java/me/sectiontn/flume/FlumeViewManager.kt b/android/src/main/java/me/sectiontn/flume/FlumeViewManager.kt index e3c5317..3eb6782 100644 --- a/android/src/main/java/me/sectiontn/flume/FlumeViewManager.kt +++ b/android/src/main/java/me/sectiontn/flume/FlumeViewManager.kt @@ -331,6 +331,10 @@ class FlumeViewManager : ViewGroupManager(), FlumeViewManagerInterfac override fun scrollIndexIntoView(view: FlumeView, requestId: Int, index: Int, animated: Boolean, viewOffset: Double) = view.scrollIndexIntoView(requestId, index, animated, viewOffset) + override fun scrollTo(view: FlumeView, x: Double, y: Double, animated: Boolean) = view.scrollToPoint(x, y, animated) + + override fun flashScrollIndicators(view: FlumeView) = view.flashScrollIndicators() + override fun setNativeRefreshing(view: FlumeView, refreshing: Boolean) = view.setNativeRefreshing(refreshing) companion object { diff --git a/example/src/screens/ScrollScreen.tsx b/example/src/screens/ScrollScreen.tsx index f98e2f4..931a0f7 100644 --- a/example/src/screens/ScrollScreen.tsx +++ b/example/src/screens/ScrollScreen.tsx @@ -2,11 +2,13 @@ import { type FunctionComponent, useCallback, useMemo, useState } from 'react'; import { Button, Platform, Text, TextInput, ToastAndroid, View } from 'react-native'; import { type CellNode, cell, Flume, type FlumeProps } from 'react-native-flume'; import Animated, { + scrollTo, + useAnimatedRef, useAnimatedScrollHandler, useAnimatedStyle, useSharedValue, } from 'react-native-reanimated'; -import { scheduleOnRN } from 'react-native-worklets'; +import { scheduleOnRN, scheduleOnUI } from 'react-native-worklets'; type Row = { id: string; title: string; body: string }; @@ -50,6 +52,15 @@ const renderRow = (item: Row): CellNode => { ]); }; +// The same row as a column, for the horizontal list. +const renderColumn = (item: Row): CellNode => { + 'worklet'; + return cell.view({ width: 160, padding: 12, gap: 4 }, [ + cell.text(item.title, { fontSize: 16, fontWeight: '600', color: '#111111' }), + cell.text(item.body, { fontSize: 13, color: '#555555' }), + ]); +}; + const toast = (message: string) => { if (Platform.OS === 'android') ToastAndroid.show(message, ToastAndroid.SHORT); }; @@ -61,7 +72,7 @@ const INSETS = { top: 40, bottom: 120 }; export default function ScrollScreen() { const rows = useMemo(() => makeRows(), []); - const [status, setStatus] = useState({ offset: 0, first: -1 }); + const [status, setStatus] = useState({ offset: 0, x: 0, first: -1 }); const [taps, setTaps] = useState(0); const [dismiss, setDismiss] = useState(0); const [insets, setInsets] = useState(false); @@ -79,16 +90,28 @@ export default function ScrollScreen() { }, []); const offset = useSharedValue(0); const reportOffset = useCallback( - (value: number) => setStatus((s) => ({ ...s, offset: value })), + (y: number, x: number) => setStatus((s) => ({ ...s, offset: y, x })), [], ); const onScroll = useAnimatedScrollHandler((e) => { offset.value = e.contentOffset.y; - scheduleOnRN(reportOffset, Math.round(e.contentOffset.y)); + scheduleOnRN(reportOffset, Math.round(e.contentOffset.y), Math.round(e.contentOffset.x)); }); const headerStyle = useAnimatedStyle(() => ({ transform: [{ translateY: -Math.min(Math.max(offset.value, 0), HEADER_HEIGHT) }], })); + const [horizontal, setHorizontal] = useState(false); + const [inverted, setInverted] = useState(false); + // Reanimated's scrollTo runs on the UI runtime and reaches the list as ScrollView's command. + const listRef = useAnimatedRef(); + const scrollTo400 = () => { + const x = horizontal ? 400 : 0; + const y = horizontal ? 0 : 400; + scheduleOnUI(() => { + 'worklet'; + scrollTo(listRef, x, y, true); + }); + }; return ( setFast((v) => !v)} />