Slot machine with endless list how to get aligned

2019-09-19 07:54发布

I'm trying to implement a slot machin with 3 endless ListView. I wanted to do that with scroll view but I didn't find a solution to loop on a scrollView.

So basically I have three listview = 3 columns of my slot machine and when I click on button I want to scroll to a random position (a random integer between 0 (inclusive) and 300 (exclusive) which is my lists size) :

Random r = new Random();
int rPos1 = r.nextInt(300 - 0) +0;
int rPos2 = r.nextInt(300 - 0) + 0;
int rPos3 = r.nextInt(300 - 0) +0;

listSlotOne.smoothScrollToPosition(rPos1);
listSlotTwo.smoothScrollToPosition(rPos2);
listSlotThree.smoothScrollToPosition(rPos3);

So all this part is working but I can't manage to display each time as a result a clean result which mean three aligned lines on my screen ::

Here a good result : three aligned lines

enter image description here

Here a bad result : lines are not aligned

enter image description here

Is it possible to manage the behaviour of my random scroll to display every time a good result : three aligned lines. I've noticed, for my 3 click on the play button I got a good result but after a fourth click I get a wrong result with not lines aligned and then it's random.

1条回答
我想做一个坏孩纸
2楼-- · 2019-09-19 08:34

If the smoothScrollToPosition() method doesn't snap, you are going to have to add your own snap meaning something like:

listSlotOne.smoothScrollToPosition((rPos1 % snap_height) * snap_height);
listSlotTwo.smoothScrollToPosition((rPos2 % snap_height) * snap_height);
listSlotThree.smoothScrollToPosition((rPos3 % snap_height) * snap_height);

or listSlotOne.smoothScrollToPosition((rPos1 / snap_height) * snap_height); listSlotTwo.smoothScrollToPosition((rPos2 / snap_height) * snap_height); listSlotThree.smoothScrollToPosition((rPos3 / snap_height) * snap_height);

I may be wrong, I'm not 100% sure but I believe that will solve your problem.

Snap To Grid I remember doing it with MOD though... well maybe this will help.

Cheers,
Demetry

查看更多
登录 后发表回答