Suppose I have two TextViews which have theirs own logic and behaviour. They appears in app several times in various activities. So it's reasonably to create some wrapper(Controller) which controls these TextViews. Sample

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.some_layout);

    TextView textViewA = (TextView) findViewById(R.id.textViewA);
    TextView textViewB = (TextView) findViewById(R.id.textViewB);

    TextViewController textViewController = new TextViewController(textViewA, textViewB);
}

public class TextViewController{
    public TextViewController(TextView textViewA, TextView textViewB){
        //creating listeners, setting texts, fetching data from models and so on
    }
}

The question is how to call such classes? Is there some common name? Maybe ViewControllers?

有帮助吗?

解决方案

This sounds like a perfect job for a Fragment. The guide on Fragments on android is a good start. The motivation is explained here quite well:

You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).

其他提示

let's say these text views are used for input username and password. In this case the name LoginController will sound enough descriptive. Also it seems to be better to wrap this in separate class instead of copy and paste it over your activities.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top