Get position of ListView scrollbar thumb on screen - android

I am trying to get the location on screen of the ListView scrollbar thumb indicator. I'd like to position a label next to it and require the X,Y screen coordinates.
I attempted to get the height of the ListView and use the value from the 'firstVisibleItem' value obtained from the ListView OnScrollListener to position the label but due to the ListView items having varying heights this hasn't proved feasible.
Are there any other solutions to this?
Thanks

I suggest you can use a little bit of hack:
View c = listview.getChildAt(0);
int scrolly = -c.getTop() + listview.getFirstVisiblePosition() * c.getHeight();
But it will work correctly only if you will have same height for all list items.

Related

Android ListView with sticky header

Is it possible to make fixed (sticky) header row in ListView, but I want it to behave like on this picture:
So, when it's not scrolled I want to show "big" header, but when user scrolls down enough, header should go to limit value (50% of header row height or something like that)... I tried to use this library:
https://github.com/applidium/HeaderListView
But I didn't success with implementing this behaviour. It's possible to make the whole (full height) header row fixed, but not half or one part of it.
Use
View c = listview.getChildAt(0);
int scrolly = -c.getTop() + listview.getFirstVisiblePosition() * c.getHeight();
To get the current Y offset, then play witn onScrollListener to set the size of your header. This basically gets the position of the first visible item, multiplies it by the height of 1 row item and adds the height of visible part of the first item resulting in offset in pixels from the start of your listview.

Scroll ListView to coordinates of click, like QQ

I want to scroll to specific item depending upon where user has clicked. All my items of list view are of different heights. Please see the image below.
So if user clicks on the list item 1, lets suppose, so it's bottom should be scrolled till the top of the softkeyboard.
Heights of items and keyboard vary. I have tried getTop, getHeight, getMeasuredHeight but nothing has been working so far.
Main point is to find the distance between the clicked UI and your keyboard top.
This is the distance formula. You can achieve your functionality using this. You need
Clicked Coordinates
Coordinates of your listview item
Height of your listview item
Suppose following is your ListView, that is being clicked
int[] loc = new int[2];
lvItem_button.getLocationOnScreen(loc);
Put a layout at the bottom of your layout, mark your activity as AdjustPan in Android Manifest, now it will always be at the top of keybaord, when ever the keyboard will open.
Get it's coordinates the same way.
int[] cords= new int[2];
keyboard_top_layout.getLocationOnScreen(cords);
double distance = Math.sqrt(Math.pow((cords[0] - loc[0]), 2) + Math.pow((cords[1] - loc[1]), 2));
distance -= heightOfItem; //subtract height because we want to align bottom of item to top of keyboard
distance = Math.ceil(distance);
lvItem.smoothScrollBy((int) -distance, 500); //-distance because X is increasing at bottom and decreasing at top. 500 is delay in scrolling. so it's smooth scroll
All done :)

How can I get the y position of an item in an Android listview in the overriden 'getView' function?

I have a customlist in which I overload the getView function and I need to know the y position of the current item that is being drawn. And I need it in relation to the entire list and not just the ones visible on screen. How can I find this out?
Edit: The Items have different heights.
Since you want the one relative to the top of the list (not the top of the visible screen, you can calculate it using:
int yPos = view.getHeight() * position;
Edit:
Different heights? That probably hurts performance, since the recycler isn't helping much.
I don't think there's a magic method for this case. Realistically what you have to do is keep an array of list position and y offsets, so for instance
// Item at position 0, top starts at 0, height is 15
pos[0][0] = 0
pos[0][1] = 15
// Next item starts one pixel down from previous item's top, is 10 pixels tall
pos[1][0] = 16
pos[1][1] = 10
And just populate from within getView. Note that this solution tracks individual height as well as absolute offset for each item- since the previous item's position + height is required to determine the next item's position.

Center a ListView on its current selection

Does anyone know of a way to center a ListView based on its current selection or selection set with setSelection?
I did see this other StackOverflow question without any answers: Android ListView center selection
Thanks,
Kevin
First, get the height of the ListView using getHeight, which returns the height of the ListView in pixels.
Then, get the height of the row's View using the same method.
Then, use setSelectionFromTop and pass in half of the ListView's height minus half of the row's height.
Something like:
int h1 = mListView.getHeight();
int h2 = v.getHeight();
mListView.setSelectionFromTop(position, h1/2 - h2/2);
Or, instead of doing the math, you might just pick a constant for the offset from the top, but I would think it might be more fragile on different devices since the second argument for setSelectionFromTop appears to be in pixels rather than device independent pixels.
I haven't tested this code, but it should work as long as your rows are all roughly the same height.
You will need to have the scroll view and the view of the item selected. Then you can simply do:
scrollView.smoothScrollTo(0, selectedView.getTop() - (scrollView.getHeight() / 2) + (selectedView.getHeight() / 2), 0);
This will center the scroll view exactly on selectedView
I haven't tried any of this but based on the current selection could you use public void smoothScrollByOffset (int offset) to get the view to scroll to where you want so that your selection is in the middle of the view?

GridView Scroll By one row

Hi i am implementing a gridView and i have trouble making it scroll down 1 row at a time with every scroll event.
my grid has a height of 1 row item (items height is 75dp). i don't want scrolling to be left in a middle of a row.
is there a way i can intercept and modify the scroll distance so that it only returns fixed value ex: +-75dp.
i would appreciate any help or suggestions you can give me. tnx
APIv8 has new function, called smoothScrollBy(int distance, int duration)[1]
i think you should catch all scroll events & implement own method to scroll view.
if you want to scroll by 75dp, just convert it to pixels & use function above.
float density = getContext().getResources().getDisplayMetrics().density;
int scrollBy = (int)(density * 75);
smoothScrollBy(scrollBy, 0);
But would be nice to calculate scrollBy from your GridView, instead of using some constant value (like 75dp)
[1]: http://developer.android.com/reference/android/widget/AbsListView.html#smoothScrollBy(int, int)

Categories

Resources