简体   繁体   中英

How to create an adaptive Android app

I want an Android app to be able to look and behave differently depending on what user group a user is in. For example, a user logs in, and the app tells a server which user it is. The information returned from the server can be different from user to user, so I want the app to be able to create different UI elements matching the information.

The kind of things that might change are:

  • Sorting lists of data based on different indexes
  • Dialogues with different input-fields, and a varying number of them
  • Colours and icons might change, although that shouldn't be that hard.
  • Maybe functions altogether might be in-/excluded, like adding/removing a field in a database, showing different graphs etc.

I'm sorry this is vague, maybe it's not clever to ask a question this general, but I don't know the specifics of the app yet, just the outline, and I'd like somewhere to start.

Is the only way to do this a bunch of

if(condition)
   //intitialize one GUI element
else if(condition2)
   //initialize another GUI element

until infinity, or is there another way?

Colleagues, why not to use the Strategy Pattern?

In our case I suggest to:

1) separate needed behavior in interfaces that will be implemented by concrete classes. Eg:

public interface IViewBehavior {
    public void setSorting();
    public void setFields();
    public void setColors();

}
public interface IDataBehavior {
    public void setData(); 
}

2) use composition and integrate (add) those behaviors into the Application object by delegating:

public class RickardApp extends Application {

    IDataBehavior dataBehavior;
    IViewBehavior viewBehavior;

    public void setDataBehavior(IDataBehavior dataBehavior) {
        this.dataBehavior = dataBehavior;
    }

    public void setViewBehavior(IViewBehavior viewBehavior) {
        this.viewBehavior = viewBehavior;
    }

}

set*Behavior() methods will define behavior of the app dynamically. We need only to create “behavior”-classes when details will be known.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM