Quantcast
Channel: cocos2d-x - Cocos Forums
Viewing all articles
Browse latest Browse all 2748

ListView not aligning magnetically like expected with scrolltoitem

$
0
0

@rob_type25 wrote:

Hi there

Been battling with a ListView for a while and while i sort of have it working when i now try to use the scrolltoitem method it's not aligning centrally in the listview as i expect.

I have a pretty simple setup, it's a portrait game and i have a listview that spans the top half of the screen with basically one large circle visible at one particular time, as below:

I'm using gravity center and magnetictype center_horizontal and the user can flick through the circles and they all align in the center, perfect!

The code i'm using for creating the listview is as follows:

  listView = cocos2d::ui::ListView::create();
    listView->setDirection(ui::ScrollView::Direction::HORIZONTAL);
    listView->setTouchEnabled(true);
    listView->setBounceEnabled(true);
    listView->setScrollBarEnabled(false);
    listView->setContentSize(Size(visibleSize.width, 130));
    listView->setAnchorPoint(Vec2(0.5,1));
    listView->setPosition(Vec2(origin.x + visibleSize.width/2, visibleSize.height + origin.y - 25));
    listView->setClippingEnabled(false);
    listView->setMagneticAllowedOutOfBoundary(false);
    listView->setSwallowTouches(false);
    listView->setInertiaScrollEnabled(false);
    listView->setItemsMargin(10.0f);
    listView->setMagneticType(cocos2d::ui::ListView::MagneticType::CENTER);
    listView->setGravity(cocos2d::ui::ListView::Gravity::CENTER_HORIZONTAL);
    addChild(listView);

I then populate some widgets like this:

for (int i =0;i<arr.size();i++){

        btn->setTouchEnabled(true);
        btn->ignoreContentAdaptWithSize(false);
        btn->setContentSize(Size(130, 130));
        btn->setAnchorPoint(Vec2(0, 0));
        btn->setTag(qCurrent->id);
        btn->setName("button");

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

        listView->pushBackCustomItem(wid1);
    }

So far so good, everything snaps to the center of the screen.

My issue is now when i use this:

listView->scrollToItem(5, Vec2(0,0), Vec2(0,0));

It scrolls to the item OK but it's off center, see below

I'm not entirely sure if i'm adding my items correctly or my listview is configured right for this setup?

Any guidance greatly appreciated.

Posts: 2

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 2748

Trending Articles