Java – ListView background scrolling with data

ListView background scrolling with data… here is a solution to the problem.

ListView background scrolling with data

Is it possible to have the background stretch over the entire ListView, rather than just staying in a fixed position?

I

remember when making a website in CSS you could pin the background attachment as the background so that it stays in the same position as you scroll, I was trying to archive the opposite of this, making the background follow the data as you scroll down. Does ListView have such a thing?

Solution

It’s impossible to do what you want with a normal ListView.

The ListView displays only a few rows at a time to save on View creation/management, etc. This can bog down the system. As a result, the ListView doesn’t even know its own total height – it doesn’t render the row until it’s visible, and the height of the row can change. If it doesn’t even know its height, how can it have a background that spans the entire of it?

That said, the idea that you use ScrollView will be possible. However, you will lose the optimization of the ListView – if you only have a few rows, then this is not a big deal; But if you’re talking about dozens of lines (or more), your app can stutter badly. Also, you don’t get a framework for ListView, which is designed to make simpler row-based UIs.

You can write your own ListView subclass (or custom ListView) to calculate its total height (as long as you know that the height of each row is fixed) and then draw the background accordingly. If I’m asked to do what you describe and have too many lines to cram into the ScrollView easily, I might do that.

Related Problems and Solutions