tags:

views:

248

answers:

1

I'm trying to make a ScrollView with a list of numbers in it that the user can select from. After the user touches and scrolls, after the finger is lifted I want the scroll to "snap" to the item in the middle of the ScrollView and give me the output. My ScrollView contains a vertical linear layout with TextView objects in it. The ScrollView's vertical size is about 60sp.

I think this would involve the ScrollView.onTouch with a ACTION_UP, but I don't know how to use it. Also, is there a way to get an item's index or value based on a position in the list?

Thanks!

A: 

Are you using ListView? See Hello ListView to get started with one. I'm not sure what exactly you mean by snapping to the item in the middle, but when scrolling a ListView I believe you will always end up with the top of the listView aligned with the top of an item.

As to the item's index or position, a ListView item has two concepts. The position is the location of the item in the whole list (typically the ArrayAdapter that was used to fill the ListView). The id is the visible position on the screen. Thus, if top item of the screen would have id 0, but may have postion 20. See Javadocs

Mayra
Right now what I'm doing is making a ScrollView with a linearLayout as its child. Inside the LinearLayout is a bunch of numbers (100,125,150...1000). The user will touch his finger down and drag until the desired number is in (or nearest) the middle. After the finger is removed, ACTION_UP, the number nearest to the middle will be snapped exactly to the middle (by way of smoothScrollTo) and the middle number will be selected and used. That's what I'm trying to do.
Matt
I've got this to sort of work right now. What I am doing is 1) on ACTION_UP I get the current position of the ScrollView with .getScrollY(). 2) I use some math to get the desired scroll position: ((int)Math.round(posY/20) * 20)+ 10. Note that the numbers 20 is the approximate distance between items and 10 is the offset for the middle. 3) I use smoothScroll to move. 4) I use some math to figure out what number in the list is in the middle based off of the position. 5) I get the Text View with (TextView)findViewById(R.id.(listview.getChildAt(x).getId())). 6) I convert the text to an int.
Matt
Glad you got it to work.. Is there a reason you don't want to use ListView? It seems like a better fit for your situation than a scrollView. Also, are you depending on pixel math there? Keep in mind that different phones have different resolutions, so that will likely break down.
Mayra
The reason I'm not using list view is because I want it to select the one in the middle on the ACTION_UP. With list view, it looks like the user needs to scroll and then tap to select. I don't like the idea of measuring by pixels either, but I'm hoping that the resolution independent pixels will help with that. I'll look into getting it to work with a ListView because it seems like it might be a better fit. Just as long as I can get it to behave the way I want
Matt