Home » Android » android – recycling views in scroll view

android – recycling views in scroll view

Posted by: admin June 16, 2020 Leave a comment

Questions:

I have an app that show the tv guide for a list of channels. My UI is made from a a lot of custom views with different widths that show the tv programs, all these custom views are added into a horizontal scrollview that is added into a scrollview so my views can be scroll in 2 dimensions left-right and top-down. It all works good until i add add a lot of views and it starts to slow down very much. So i need a way to recycle views like listiew does in a scrollview maybe there is a custom made scrollview that does this, or someone has an idea how to do this, its strange that scrollview isnt backed up by an adapter like gridview and listview.

This is how my UI looks

How to&Answers:

I did something similar only my Views were not connected as yours but they were all different sizes.

First you need to define if your entire area (not just viewing screen) has definitive or dynamical number of your custom views.

If you have definitive number of views and their positions you should create their position map with list of Rect’s (Rect has a good function whether the xy point belongs). Then you define maximum of Views which are visible on your screen. For this to work without constant loading you should have maximum visible views + at least one line of border views of total objects. After all this you should easily have your own positioning system where you load views which are in bounds of your screen + some overhead (purpose of this is that you want your users to have smooth transition while scrolling at least for some length), if you need to load some in same time you unload (read reuse/ do not dispose objects and create them onScroll events) and place them according to your needs.

And if you want to determine which views should be visible you just go through list and ask whether Views Rect intersects with your Rect of area to be loaded.

Hope this image helps a little bit more

Example

I know it sounds a little bit confusing and difficult to implement but you did not asked a simple question 🙂
Hope this helps and enjoy your work.

Answer:

A ScrollView that is backed up by an adapter and recycles views is a ListView, with a couple of extra optional features on top.

Maybe you want a HorizontalScrollView that is backed by an adapter? Searching for HorizontalListView will give you a few results, ex: https://github.com/dinocore1/DevsmartLib-Android.