Probably this is really stupid question but i created structure like this and i really have issues with performance.
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 realy slow if i will try to create 5 InnerClass objects and 5 of InnerOfInner Class objects in each InnerClass. I would be extremely grateful if some one will explain me why. Thx.
There doesn’t seem to be anything that stands out from your code as a huge red flag. As to why it’s really slow in the creation part could be for a variety of reasons. Things to consider:
How is OuterClass being instantiated from the activity? Is it only being called once or are you creating several instances. The more you have the slower it’ll be.
How many other widgets are on the R.layout.new_line, R.layout.job_function_container, R.layout.view_port? Having just one widget vs 50 on each may cause some noticeable delays.
What device are you testing with? Running on a old Droid will be significantly slower then testing on a Nexus 7. If your using an emulator…well, always expect everything to run slow.
Finally (and probably the most pertinent), I believe the biggest slow down is the complexity or the depth of your nesting layouts. As Ancantus mentioned, try to avoid deep layout layers. Go too deep, and weird things may start to happen. Mostly due to the amount of memory such hierarchies consume.
If you need a dynamic means of adding views, I’d suggest looking into a ListView, GridView, or even a GridLayout if your target platform allows. Off the top of my head, I can’t think of why you would require such a complex design. Though there are special case needs, usually there’s always an easier, more correct way.
Here’s some helpful links on Layout Optimization that may or may not be helpful for you: