Cocos2d-x中如何判断ScrollView结束?

How to determine the end of ScrollView in Cocos2d-x?

上下文
我通过 ui::ListView 制作了一个自定义排行榜。排行榜的每个元素都是 ui::Button。当用户点击某个位置时,他会在排行榜中收到有关该位置的详细统计信息。我正在使用 Cocos2d-x 版本。 3.8.1.为了清楚起见,这是我的代码:

    auto lvLeaderboard = ListView::create();
    for (int j = 0; j < linesCount; j++)
    {
        Button* btnSingleUser = Button::create("btn.png”);
        btnSingleUser->setTag(j + offsetResult);
        btnSingleUser->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type){
            switch (type)
            {
                case ui::Widget::TouchEventType::ENDED:
                {
                    Button* currentButton = (Button*)sender;
                    this->pressedUserStatistic(currentButton->getTag());
                    break;
                }
                default:
                    break;
            }
        });
    lvLeaderboard->pushBackCustomItem(btnSingleUser);
    …
    }
    lvLeaderboard->setItemsMargin(0);
    lvLeaderboard->setGravity(ListView::Gravity::CENTER_HORIZONTAL);
    lvLeaderboard->setSize(Size(winSize.width, _height));
    lvLeaderboard->setPosition(Point(0, 0));
    listContainer->addChild(lvLeaderboard);    

一切正常,我可以滚动排行榜,并查看点击的每个用户的统计信息。
问题
但是有一个问题。当用户到达列表末尾时,我需要加载下一部分数据(我一次下载 50 行的结果)。但是我找不到当用户到达 ListView 的第一个或最后一个元素时起作用的变量、方法或处理程序。
我试过了
ui:ListView inherits ScrollView,所以我试着在他们两个里面找一些方法。什么都试过了,结果不是我想要的
问题
如何确定用户何时到达 ListView 中的第一个和最后一个元素?或者,如果这不可能,如何确定 ScrollView 的结束和开始?

您可以安排一个更新方法或 运行 一个永远重复的动作来连续调用一个方法来检查列表视图的底部项目是否有最后一个标签(在您的情况下 tag-10 == linesCount-1 ).您需要全局引用 lvLeaderboard 才能访问以下功能。

static Vec2 calculateItemPositionWithAnchor(Widget* item, const Vec2& itemAnchorPoint)
{
    Vec2 origin(item->getLeftBoundary(), item->getBottomBoundary());
    Size size = item->getContentSize();
    return origin + Vec2(size.width * itemAnchorPoint.x, size.height * itemAnchorPoint.y);
}

static Widget* findClosestItem(const Vec2& targetPosition, const Vector<Widget*>& items, const Vec2& itemAnchorPoint, ssize_t firstIndex, float distanceFromFirst, ssize_t lastIndex, float distanceFromLast)
{
    CCASSERT(firstIndex >= 0 && lastIndex < items.size() && firstIndex <= lastIndex, "");
    if (firstIndex == lastIndex)
        return items.at(firstIndex);

    if (lastIndex - firstIndex == 1)
    {
        if (distanceFromFirst <= distanceFromLast)
            return items.at(firstIndex);
        else
            return items.at(lastIndex);
    }

    // Binary search
    ssize_t midIndex = (firstIndex + lastIndex) / 2;
    Vec2 itemPosition = calculateItemPositionWithAnchor(items.at(midIndex), itemAnchorPoint);
    float distanceFromMid = (targetPosition - itemPosition).length();
    if (distanceFromFirst <= distanceFromLast)
        return findClosestItem(targetPosition, items, itemAnchorPoint, firstIndex, distanceFromFirst, midIndex, distanceFromMid);
    else
        return findClosestItem(targetPosition, items, itemAnchorPoint, midIndex, distanceFromMid, lastIndex, distanceFromLast);
}

static Widget* getBottommostItemInCurrentView(ListView* lvLeaderboard)
{
    const Vec2& positionRatioInView = Vec2::ANCHOR_MIDDLE_BOTTOM;
    const Vec2& itemAnchorPoint = Vec2::ANCHOR_MIDDLE;

    // Calculate the target position
    Size contentSize = lvLeaderboard->getContentSize();
    Vec2 targetPosition = -lvLeaderboard->getInnerContainerPosition();
    targetPosition.x += contentSize.width * positionRatioInView.x;
    targetPosition.y += contentSize.height * positionRatioInView.y;

    // Find the closest item through binary search
    ssize_t firstIndex = 0;
    Vec2 firstPosition = calculateItemPositionWithAnchor(lvLeaderboard->getItems().at(firstIndex), itemAnchorPoint);
    float distanceFromFirst = (targetPosition - firstPosition).length();

    ssize_t lastIndex = lvLeaderboard->getItems().size() - 1;
    Vec2 lastPosition = calculateItemPositionWithAnchor(lvLeaderboard->getItems().at(lastIndex), itemAnchorPoint);
    float distanceFromLast = (targetPosition - lastPosition).length();

    return findClosestItem(targetPosition, lvLeaderboard->getItems(), itemAnchorPoint, firstIndex, distanceFromFirst, lastIndex, distanceFromLast);
}

void HelloWorld::checkIfAtEnd() {

    Button* bottom = (Button*)getBottommostItemInCurrentView(lvLeaderboard);

    CCLOG("Bottom Button Having = %d", bottom->getTag());
    if (bottom->getTag()-10 == linesCount-1) {
        CCLOG("Bottom item reached..");
    }
}

以下代码片段会在指定秒数(此处为 0.5 秒)后持续调用上述函数。

DelayTime* dl = DelayTime::create(0.5);
CallFunc* cb = CallFunc::create(CC_CALLBACK_0(HelloWorld::checkIfAtEnd, this));
Sequence* seq = Sequence::create(dl, cb, nullptr);
this->runAction(RepeatForever::create(seq));

您可以将 eventListener 添加到您的 scrollView 并像这样检测然后检查

scrollView->addEventListener([](Ref *r, ScrollView::EventType type) {
  if (type == ScrollView::EventType::SCROLL_TO_BOTTOM) {// SCROLL_TO_TOP
    // do what you need to do 
  }
});