【问题标题】:how to detect whether scrolling up or scrolling down in radlistview如何检测在radlistview中是向上滚动还是向下滚动
【发布时间】:2026-02-11 21:20:03
【问题描述】:

本机脚本 rad-list-view 未检测是向上滑动还是向下滑动项目列表。

通过使用本机脚本滑动事件,将获得左、右、上、下 4 个值,即 1、2、4、8。但滑动操作不适用于 rad-list-view。

        <lv:RadListView pullToRefresh="true" pullToRefreshInitiated="{{onPullToRefreshInitiated}}"
        scrolled="onScrolled" scrollOffset="scrollOffset" scrollStarted="onScrollStarted" scrollEnded="onScrollEnded"
        id="id_content" row="1" items="{{ source }}" loaded="onLoaded" backgroundColor="transparent"
        itemLoading="onItemLoading" itemTap="onItemTap">

            <lv:RadListView.itemTemplate>
                <StackLayout orientation="vertical" 
                backgroundColor="antiquewhite" margin="5" padding="10" borderWidth="1">

                    <Label fontSize="20" text="{{ name }}"/>
                    <Label fontSize="14" text="{{ name }}"/>

                </StackLayout>
            </lv:RadListView.itemTemplate>

            <lv:RadListView.listViewLayout>
                <lv:ListViewStaggeredLayout scrollDirection="Vertical" spanCount="2"/>
            </lv:RadListView.listViewLayout>

            <lv:RadListView.pullToRefreshStyle>
                <lv:PullToRefreshStyle indicatorColor="red" indicatorBackgroundColor="antiquewhite"/>
            </lv:RadListView.pullToRefreshStyle>

        </lv:RadListView>

    </GridLayout>

如何检测 radlistview 上的向上或向下滑动事件。

【问题讨论】:

    标签: nativescript gesture radlistview


    【解决方案1】:

    比较scrollStartedscrollEnded事件之间的scrollOffset,如果开始位置小于结束位置,则用户向上滚动,否则向下滚动。

    let lastOffset;
    
    export function onScrollStarted(args) {
        lastOffset = args.object.getScrollOffset();
    }
    
    export function onScrollEnded(args) {
        let currentOffset = args.object.getScrollOffset();
        console.log(currentOffset < lastOffset ? "Up" : "Down");
        lastOffset = currentOffset;
    }
    

    Playground Sample

    【讨论】:

    • 这可以正常工作,但不如预期。我正在为操作栏设置动画,即向下滚动时隐藏,向上滚动时显示。但是从上面的答案中,当我立即向下滚动时,动画将不会执行。只有在滚动结束后,动作栏才会被隐藏。有没有针对这个问题的建议......
    • 您的问题是关于检测向上/向下滚动,因此我必须考虑最佳选择,而不会多次点击事件。如果您想立即检测用户是否向上/向下滚动,则只需使用scrolled 事件而不是scrollEnded 事件。
    • 感谢您的回答。我刚刚将 scrollEnded 事件更改为 onScrollDragEnded 事件。这很好用。但是,在制作动画时,我注意到一些小故障,而且我还观察到动画与使用 listView 相比并不流畅。谢谢你.....