如何在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继承了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->addEventListener([](Ref *r, ScrollView::EventType type) {
if (type == ScrollView::EventType::SCROLL_TO_BOTTOM) {// SCROLL_TO_TOP
// do what you need to do
}
});
相关文章:
- 为什么在这个代码结束循环中没有得到结束
- 试图对缓存进行跨步测试,但程序并没有结束
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 为什么擦除方法会影响结束方法
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 删除映射和分割错误中的一个过去结束元素
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 为什么我的两个 cin 语句没有在程序结束时运行?
- 在函数结束后使用指向变量的指针是否安全?
- C ++尝试并捕获未结束的程序
- 为什么我的程序在输入某个形状的面积的测量值后没有结束?
- 取消引用结束指针到数组类型的一个
- 如何在 c++ 中确定一条指令(以字节为单位)在哪里结束,另一条指令从哪里开始?
- 结束另一个线程中使用的对象的生存期
- 全局向量导致 C++ 程序结束时出现段错误
- 程序显示以退出代码 0; 结束
- 如何显示函数开始、结束行和函数体?
- C++ 从具有开始位置和结束位置的列表中删除
- 使用 shared_ptr 在中断时结束多线程循环
- 如何在Cocos2d-x中确定ScrollView的结束