android - Adding view/layouts in already inflated hierarchy in runtime? -


i wonder if bad practice add view view hierarchy in runtime inflated view.
lets consider example. have following activity class hierarchy

  1. base activity - base activity include basic configuration lifecycle methods , without inflating layout(setcontentview).
  2. singleframgentactivity has setcontenview method in oncreate method. layout activity looks following

c

<?xml version="1.0" encoding="utf-8"?> <framelayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent">     <framelayout android:id="@+id/fragment_container"                  xmlns:android="http://schemas.android.com/apk/res/android"                  android:layout_width="match_parent"                  android:layout_height="match_parent"                  android:orientation="vertical">      </framelayout     <viewstub         android:id="@+id/stub_progress_bar"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:inflatedid="@+id/progress_bar_buttons"         android:layout="@layout/inflate_stub_progressbar_bg"/> </framelayout>   

there several problems here.
can see here, there 1 fragment container framelayout holding current fragment far singlefragmentactivity.

what need add toolbar layout , should present on fragment inside activity.
layout in case have

<?xml version="1.0" encoding="utf-8"?> <framelayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent">     <framelayout android:id="@+id/fragment_container"                  xmlns:android="http://schemas.android.com/apk/res/android"                  android:layout_width="match_parent"                  android:layout_height="match_parent"                  android:orientation="vertical">      </framelayout>     <include         android:id="@+id/toolbar"         layout="@layout/include_toolbar"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_gravity="top"/>     <viewstub         android:id="@+id/stub_progress_bar"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:inflatedid="@+id/progress_bar_buttons"         android:layout="@layout/inflate_stub_progressbar_bg"/> </framelayout>   

there several problems here.
1. have inflated layout in singleframgentactivity class. have way add inflated hierarchy, getting root view , calling addview method. if please see next question.
2. need add view, need add layout file view hierarchy between 2 existent views. framelayout , viewstub because of z placing of view in framelayout.

there several solutions problem
1. include toolbar in each fragment need (in fragments inside activity) seems not best way , because of code duplication. (don't repeat rule).
2. use basic toolbarfragment class , in case have add required views inherited fragments in runtime.
3. use decorator class toolbaractivity , have described above add inflated view hierarchy. 4. use layout same singleframgentactivity put there toolbar include tag , rename class singletoolbarfragmentactivity. seems single responsibility principle has been broken. , if need toolbar without fragment container or vice versa.

so need advice best way this. not bad practice performance ?
please suggest better use in case not cause performance issues , keep code readable possible

there no performance problem adding views @ runtime if not adding them 1 one, or changing layout bounds or, same, calling multiple times requestlayout/invalidate. should not cause performance problem.

given that, in case create lets call baseactivity methods can extended hastoolbar():boolean , in moment of creation of activity inflate needed , add view. 1 approach, of mention can work without performance problem since inflating single layout not time consuming (like happens in listviews example).


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -