1

I want to use CardViews in my app only for the visual effect.

All CardViews will contain very different views, so I cannot really design a good adapter and thus I cannot use a RecyclerView.

The result is perfectly clean, but for this I had to nest a lot of ConstraintLayout, and so the layout take several seconds to draw itself. (any advice on how to measure more precisely this delay ?)

Here the hierarchy of my layout (with 9 CardViews and not 2):

ConstraintLayout
-Image + text x4
-ScrollView
--ConstraintLayout
---Cardview
----ConstraintLayout
-----text, spinner, button, etc
---Cardview
----ConstraintLayout
-----text, spinner, button, etc

You can see the max depth is ConstraintLayout>ScrollView>ConstraintLayout>CardView>ConstraintLayout, which seems inefficient as hell, as stated in the Android Optimizing Layout Hierarchies Guide.

What should I do to flatten my layout ?

Adil Saiyad
  • 1,582
  • 2
  • 17
  • 34
Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92

3 Answers3

0

I think your setup is fine. And not too deep. The layout is probably not the problem.

Note that it only draws the item on the screen. It does create layouts for the others in the ScrollView, but does not render them. If the drawing was the problem, the scrolling would be slow. You can check that.

Layout might be slow, but I doubt it. There are ways to monitor this though.

Are you loading all images off the UI thread? Nothing on the screen is coming from a server or going through some other complicated (parsing) method?

Frank
  • 12,010
  • 8
  • 61
  • 78
  • Thanks for your answer. This is my home activity, it only loads the layout and set some onclicklistener that call other activities. The scrolling is not slow and nothing comes from a server (images are just icons from drawables). What are the ways to monitor this ? – Dan Chaltiel Aug 31 '17 at 13:48
  • Easies is to start with is not 'layouting' (visibility = gone) 90% of your views. See if it increases speed. If it does then you are right and it's the deep layout. If it does not, then it's elsewhere. – Frank Aug 31 '17 at 14:12
0

SOLUTION 1:

It's okay for RecyclerView to load different views and you can design good adapter for it. Check A RecyclerView with multiple item types and Heterogenous Layouts inside RecyclerView.

SOLUTION 2:

Considering your hierarchy you can place your cards directly to ScrollView without ConstraintLayout:

ConstraintLayout
-Image + text x4
-ScrollView
--Cardview
---ConstraintLayout
----text, spinner, button, etc
--Cardview
---ConstraintLayout
----text, spinner, button, etc
Eugene Brusov
  • 17,146
  • 6
  • 52
  • 68
0

Here is an easy solution: Don't use nested ConstraintLayouts, the idea of ConstraintLayout is to reduce the amount of nested objects, but there is a performance price for it.

Base on this comment it's up to 3 times slawer than RelativeLayout. And I think that nesting it only increase the problem.

So try replacing ConstraintLayout with RelativeLayout and please reply here with the difference in performance, I bet you will see one.

Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216