Android's internal layout class

This may be a really stupid question, but I created such a structure and I really have performance problems.

public class OuterClass extends LinearLayout { private LinearLayout viewPort; public OuterClass(Context context) { super(context); initComponents(context); addInnerClass(); } private void initComponents(Context context){ LayoutInflater inflater = (LayoutInflater) getContext(). getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.view_port, this); viewPort = (LinearLayout) findViewById(R.id.view_port); } private void addInnerClass(){ for ( int i = 0; i < k; i++ ){ InnerClass functio = this.new InnerClass(); viewPort.addView(functio); } } private class InnerClass extends RelativeLayout{ private LineraLayout newLines; public InnerClass() { super(OuterClass.this.context); initComponents(OuterClass.this.context); addNLines(); } private void initComponents(Context context){ LayoutInflater inflater = (LayoutInflater) getContext(). getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.job_function_container, this); newLines = (LinearLayout) findViewById(R.id.newLines); } private void addInnerOfInner(View view){ newLines.addView(view); } private void addNLines(){ for ( int i = 0; i < k1; i++ ){ InnerOfInnerClass line = this.new InnerOfInnerClass(); addInnerOfInner(line); } } public class InnerOfInnerClass extends RelativeLayout{ private InnerOfInnerClass(){ super(OuterClass.this.context); initComponents(OuterClass.this.context); } private void initComponents(Context context){ LayoutInflater inflater = (LayoutInflater) getContext(). getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.new_line, this); // Setting some fields. } } } 

}

To be more spesific, it is very slow if I try to create 5 InnerClass objects and 5 InnerOfInner Class objects in each InnerClass. I would be extremely grateful if someone would explain to me why. thanks.

+4
source share
1 answer

Nothing seems to stand out from your code like a huge red flag. As for why it is really slow in terms of creation, it can be for various reasons. What to consider:

  • How to create an instance of OuterClass from an activity? Whether it is called only once or do you create multiple instances. The more you slow down, the better.

  • How many other widgets are in R.layout.new_line, R.layout.job_function_container, R.layout.view_port? Having only one widget versus 50 on each may cause some noticeable delays.

  • What device are you testing? Running on an old Droid will be significantly slower than testing on a Nexus 7. If you use an emulator ... well, always expect everything to work slowly.

  • Finally (and probably the most laconic), I think the biggest slowdown is the complexity or depth of the nesting layouts. As Ancanthus mentioned, try to avoid deep layers of the layout. Go too deep and weird things can begin. Mainly due to the amount of memory that such hierarchies consume.

If you need a dynamic tool for adding views, I would suggest looking at ListView, GridView, or even GridLayout, if your target platform allows. From head to toe I can’t figure out why you need such a complex design. Although there are special needs in the case, it is usually always easier and more correct.

Here are some useful Layout Optimization links that may or may not be useful to you:

+1
source

Source: https://habr.com/ru/post/1441296/


All Articles