Slot machine with endless list how to get aligned lines

廉价感情. 提交于 2020-01-17 05:04:07

问题


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

Here a bad result : lines are not aligned

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:


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



来源:https://stackoverflow.com/questions/33928815/slot-machine-with-endless-list-how-to-get-aligned-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!