Cocos2dx ListView scroll

Cocos2dx ListView scroll
0

#1

Hello!
I have a silly, simple question. I have a ListView in my scene. It’s pretty long so when I enter the scene I want that it automatically scrolls from left to right to show the whole content of the ListView (then ofcorse the user can manually scroll the View as he wants). My code looks like this:

auto listView = ListView::create();
    
listView->setDirection(ui::ScrollView::Direction::HORIZONTAL);
listView->setTouchEnabled(true);
listView->setBounceEnabled(true);    
listView->setSize(Size(winSize.width, winSize.height/3));
listView->setClippingEnabled(true);
listView->setAnchorPoint(Vec2::ZERO);
listView->setPosition(Vec2::ZERO);
listView->setSwallowTouches(false);

auto spr1 = Sprite::create("sprite1.png");
auto spr2 = Sprite::create("sprite2.png");
auto spr3 = Sprite::create("sprite3.png");
auto spr4 = Sprite::create("sprite4.png");
auto spr5 = Sprite::create("sprite5.png");

spr1->setAnchorPoint(Vec2(0, 0.5f));
spr2->setAnchorPoint(Vec2(0, 0.5f));
spr3->setAnchorPoint(Vec2(0, 0.5f));
spr4->setAnchorPoint(Vec2(0, 0.5f));
spr5->setAnchorPoint(Vec2(0, 0.5f));

auto wid1 = Widget::create();
wid1->setContentSize(spr1->getContentSize());
wid1->addChild(spr1);

auto wid2 = Widget::create();
wid2->setContentSize(spr2->getContentSize());
wid2->addChild(spr2);

auto wid3 = Widget::create();
wid3->setContentSize(spr3->getContentSize());
wid3->addChild(spr3);

auto wid4 = Widget::create();
wid4->setContentSize(spr4->getContentSize());
wid4->addChild(spr4);

auto wid5 = Widget::create();
wid5->setContentSize(spr5->getContentSize());
wid5->addChild(spr5);

listView->pushBackCustomItem(wid1);
listView->pushBackCustomItem(wid2);
listView->pushBackCustomItem(wid3);
listView->pushBackCustomItem(wid4);
listView->pushBackCustomItem(wid5);

listView->scrollToRight(2.0f, true); <=====THIS FUNCTION SHOULD DO THE TRICK BUT DOES NOT WORK :( 

addChild(listView);

The ListView works nice except for the scrollToRight method. What am I missing guys?


#2

Have you tried adding the ListView to its parent before calling scrollToRight?


#3

@dotsquid
You mean something like this:

this->addChild(listView);
listView->scrollToRight(2.0f, true);

?
If so, then it does not work :frowning:


#4

Anyone? Something? :stuck_out_tongue:


#5

Call listView->refreshView() before the scrollTo*** or jumpTo***. It’s works for me.


#6

Jumping in here 3 years later, the current solution to keeping your ListView/ScrollView scrollable is to do listview->requestDoLayout/forceDoLayout(), depending on your needs, and then maybe a listview->scrolltoBottom/Top() as needed. This’ll resize the inner container and fit it around the children inside.


#7

I used these scrolls in cocos and they awful, full of bugs/ really badly programmed and too complicated.


#8

They’re a little weird, and definitely not much docs for them, but they’re pretty useful IMO. Sorta goes the same for most of cocos.

That’s probably my number one problem with cocos, there isn’t a solid set of English docs for it.