Natjo
Natjo

Reputation: 2118

Program lags due to too many views

I wrote an android app, that creates multiple simple views (ImageViews showing a drawable). After each touch new views get added. While it works fine with a few views, it goes up too 10000 views in the end (on purpose). This makes the app really laggy.

Actually there is not that much happening, most of the views just stay the same during the whole run of the app. Just a few get updated each time a touch happens.

I don't know that much about android programming yet. Do you have any ideas, how I could implement this more efficient? Currently I'm just adding views to a RelativeLayout.

Edit

Here is an example of how it should look like: enter image description here

Clicking a big circle makes it into small circles. If nothing touches the circle, it won't change. I create a smaller circle once the bigger one was destroyed, so there is no overlapping of views.

Upvotes: 0

Views: 371

Answers (1)

Sergey Shustikov
Sergey Shustikov

Reputation: 15821

The best way - use RecyclerView for this.

From docs :

This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views. Use the RecyclerView widget when you have data collections whose elements change at runtime based on user action or network events.

RecyclerView.LayoutManager will be also a friend to you with relationship with RecyclerView. It will helpfull to you. You will can lay out your views as you need:

enter image description here

Update

If you want layout views as you pointed :

enter image description here

For the better performance you should abandon from RelativeLayout and ImageView concept.

For my point of view, the best way to do that - write a custom view.

Draw circles on Canvas - why not?

Upvotes: 1

Related Questions