Android ListView with sticky header - android

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.

Related

Calculate maximum itemview height in recyclerview before setting data (onBindViewHolder)

We have horizontal recycler view. Each item view of recycler view contains a textView.
Width of each item view is constant, 100dp. Height is dynamic & we have to set it as per the max data.
List of string is coming from server & hence any data can come. So, e.g. 1st cell can have text of 1 line whereas 2nd cell can have text of 4 lines.
Requirement:
We have to calculate maximum height of the cell (i.e cell consuming max height due to biggest text) and set that maximum height to all cells of recycler view.
This is required because we want height of all item views to be same & also making biggest text visible to user without getting cut (ellipse).
One of the ways that I know:
Before setting adapter, we can iterate the list containing Strings. We can create a dummy textview (invisible to user & width 100 dp). We can set each of the text in the textview & then get height of the textview. We will save the maximum height to adapter while calling setAdapter.
Is this good approach?
Please help.

Get position of ListView scrollbar thumb on screen

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.

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