При создании пользовательского представления я заметил, что многие люди, кажется, делают это так:
public MyView(Context context) {
super(context);
// this constructor used when programmatically creating view
doAdditionalConstructorWork();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// this constructor used when creating view through XML
doAdditionalConstructorWork();
}
private void doAdditionalConstructorWork() {
// init variables etc.
}
Моя проблема в том, что это мешает мне сделать мои переменные окончательными. Есть ли причина не делать следующее?
public MyView(Context context) {
this(context, null);
// this constructor used when programmatically creating view
}
public MyView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
// this constructor used when creating view through XML
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// this constructor used where?
// init variables
}
Мне удалось создать представление с помощью XML и кода, но я не уверен, есть ли у этого подхода какие-либо недостатки. Будет ли это работать во всех случаях?