java - Including many external layouts in a single XML file affects performance? -
i'm writing application uses bunch of forms. every form designed in separate xml file , included "main" xml file. then, programmatically set visibility of 1 of them visible, , rest set gone, , changes according spinner selects form want see.
so far i've added 4 forms , seems work fine, app meant hold 30 different forms.
is alright or should inflate , replace forms fragments? every form has own class manages widgets , logic, , these classes implements interface can use polymorphism clean or send form.
part of main.xml embeed forms
<relativelayout android:id="@+id/fr_form_layouts" android:layout_width="wrap_content" android:layout_height="wrap_content"> <include android:id="@+id/fr_layout_1" android:visibility="gone" layout="@layout/my_layout_1"/> <include android:id="@+id/fr_layout_2" android:visibility="gone" layout="@layout/my_layout_2"/> <include android:id="@+id/fr_layout_3" android:visibility="gone" layout="@layout/my_layout_3"/> <include android:id="@+id/fr_layout_4" android:visibility="gone" layout="@layout/my_layout_4"/> </relativelayout> this example of class use manage form
public class mr01formwidgets implements myforminterface { private edittext someedittext; private view view; public mr01formwidgets(view view) { this.view = view; //initialize widgets findviewbyid } @override public boolean isformvalid() { return true; } @override public void cleanform() { //cleans form } @override public void sendform() { //sends form } } then, on main activity have like:
//the spinner defines layoutid , calls method private void replaceformview(view view, int layoutid) { view.findviewbyid(visibleform).setvisibility(view.gone);//hides previous form visibleform = layoutid;//sets new visible form view.findviewbyid(visibleform).setvisibility(view.visible);//makes visible myforminterface currentform; if(visibleform == r.id.fr_layout_1) currentform = new mr01formwidgets(view); else if(visibleform == r.id.fr_layout_2) currentform = new mr02formwidgets(view); //and on... } so happens here have forms embeeded , hidden in single layout, can manage widgets single view object. again, ok?
when view's visibility set gone not rendered. means having hundreds of hidden views in layout not slow down performance when changes happen in views visible.
loading hundreds of views xml ll see performance issue when activity starts, because parsing such big xml file work.
if load views programmaticaly wont have such big performance issue @ activity's start...however views hold objects , leads increase of app's memory usage. depending on other things lead serious memory problem.
you should choose 1 of solutions provided android sdk handling cases this. viewpager, viewstub, fragments etc...these approaches save memory problems destroy unnecessary views when should destroyed (assuming implement them correctly)
Comments
Post a Comment