0

I have a custom ListView that scrolls down quite a bit. I want a background image that tiles and scrolls with the ListView.

I used the code from this answer but background images are jumpy in that at certain scrolling points the entire thing resets to what is currently the top of the viewable screen. I assume this is because of ListView optimization. How do I get around this if each item in the list has the same height? I don't need the image to load for nonexisting items, I just need the illusion of continuity.

Otherwise, tiling works just fine.

Community
  • 1
  • 1
Zeek Aran
  • 523
  • 4
  • 18
  • If your background is tiled, have you tried overriding the onDraw(Canvas) method and then translating the Canvas with a calculated scrolling offset (similar to the answer you posted in your question) – Cruceo Apr 21 '14 at 18:12
  • I was thinking that's what I needed to do, but I don't understand how to implement the offset. – Zeek Aran Apr 21 '14 at 19:34
  • Well it depends. If all the Views in your ListView have the same height, you can easily calculate the scrolling offset via overriding the ListView's onScroll method and use the code in the dispatchDraw method from the answer you posted. If they have different heights, though (Eg. you have a header/footer or variations in items), that method of calculation won't work (at least not as it is) because it relies on the the View's calculated height in order to determine how far the user has scrolled. – Cruceo Apr 21 '14 at 20:01
  • I haven't yet, but was going to add an action bar with a filter Spinner to sort the list items differently. Would that count as a header? – Zeek Aran Apr 21 '14 at 20:19
  • Not necessarily. A standard ActionBar would exist outside of the ListView, where as the ListView's Header would generally be the first item in the ListView, but is treated in a special case. It really depends on your definition of a header here. – Cruceo Apr 21 '14 at 21:20
  • That makes sense. So I don't fully understand ListView optimization. Could you tell me what offset I'm looking for? Currently it seems to jump as EVERY item is moved off the screen. OnScrollChanged will give me new x and ys (obviously the y is the only thing I care about) but I don't know what to do with this new y. – Zeek Aran Apr 21 '14 at 21:27
  • ListView doesn't have a way to get the actual scrolled position by itself, so in order to 'figure it out' you need to calculate it by finding the top margin of the first visible item and also taking into account all the heights of the Views that occur before that position, as well as the spacing in between each of them individually. When the View is no longer on screen, the next visible item becomes the first visible item, and its top is now the 'new top' used in that method, which explains why your system is 'jumpy.' – Cruceo Apr 21 '14 at 21:32

0 Answers0