문제

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