android - Various activities use same views from one layout - how to refactor? -


i have 6 activities this. tried make custom activity hold imageviews won't have repeat myself in every activity. should leave or can make somehow in 1 place , let used (like layout - it's 1 , works):

public class activityone extends appcompatactivity implements view.onclicklistener {      @bind(r.id.iv1) imageview iv1;     @bind(r.id.iv2) imageview iv2;     @bind(r.id.iv3) imageview iv3;     @bind(r.id.iv4) imageview iv4;     @bind(r.id.iv5) imageview iv5;     @bind(r.id.iv6) imageview iv6;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         butterknife.bind(this);          iv1.setimagedrawable(getresources().getdrawable(r.drawable.c1));         iv2.setimagedrawable(getresources().getdrawable(r.drawable.c2));         iv3.setimagedrawable(getresources().getdrawable(r.drawable.c3));         iv4.setimagedrawable(getresources().getdrawable(r.drawable.c4));         iv5.setimagedrawable(getresources().getdrawable(r.drawable.c5));         iv6.setimagedrawable(getresources().getdrawable(r.drawable.c6)); }    

you can create abstract activity imageryactivty needs override method getcontentview provides layout id:

public abstract class imageryactivity extends appcompatactivity {    @bind(r.id.iv1) imageview iv1;   @bind(r.id.iv2) imageview iv2;   @bind(r.id.iv3) imageview iv3;   @bind(r.id.iv4) imageview iv4;   @bind(r.id.iv5) imageview iv5;   @bind(r.id.iv6) imageview iv6;    public abstract int getcontentview();    protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(getcontentview());     butterknife.bind(this);      iv1.setimagedrawable(getresources().getdrawable(r.drawable.c1));     iv2.setimagedrawable(getresources().getdrawable(r.drawable.c2));     iv3.setimagedrawable(getresources().getdrawable(r.drawable.c3));     iv4.setimagedrawable(getresources().getdrawable(r.drawable.c4));     iv5.setimagedrawable(getresources().getdrawable(r.drawable.c5));     iv6.setimagedrawable(getresources().getdrawable(r.drawable.c6));   }     } 

and child activities must inherit one:

public class activityone extends imageryactivity {    protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);   }    public int getcontentview() {     return r.layout.activity_one;   }  } 

of course layout must contain imageview's proper id's. i´d recommend create reusable layout imagery_layout , include in each of child activities:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:orientation="vertical">    <include     layout="@layout/imagery_layout"     android:layout_width="match_parent"     android:layout_height="wrap_content"/>    <!-- , here comes content particular activity in case there's 1 -->  </linearlayout> 

Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -