如果你不知道什么会去这些类,那么请看看here!另外,我不是100%肯定这将工作还没有,我测试了这一点导致类扩展该类型的Java类泛型?
我目前正在打造一个简化的基类,可以简化类的AttributeSet
使用自定义XML属性
基本上,这就是我有点找一个最终结果 ...
public class SimpleViewImplementation extends SimpleView<LinearLayout> {
// List of members here
private String value;
public SimpleViewImplementation(Context context) {
super(context);
}
public SimpleViewImplementation(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void setFromStyledAttributes(TypedArray attr) {
// Set conditions for each member here via TypedArray (use setters)
setValue(attr.getString(R.styleable.SimpleViewImplementation_value));
}
@Override
protected void initView() {
// Set initial conditions for each member here
this.value = "this is the default value!";
}
// Getters & Setters here for members
public String getValue() { return this.value; }
public void setValue(String value) {
this.value = value;
this.updateViewOnSet();
}
}
这是所有魔法的“基础”类。问题实际上就是班级“签名”。我需要它来扩展类型T
。 我错过了如何在网上做我的研究,或者它不能完成。如果它不能完成,那么他们是否有任何建议来获得我上面的结果。如果你不知道什么会去这些类,那么请看看here!
public abstract class SimpleView<T> { // I would like this class to extend Type T. ie SimpleView<LinearLayout> would extend this class to be a LinearLayout...getting rid of compile-time errors below
// ^can I put anything here????
public SimpleView(Context context) {
super(context); // CTE (Compile-time error)
initView();
}
public SimpleView(Context context, AttributeSet attrs) {
super(context, attrs); // CTE
initView();
TypedArray attr = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DrawerSongDetail, 0, 0);
try {
this.setFromStyledAttributes(attr);
} finally {
attr.recycle();
}
}
// Sets all members based on AttributeSet parameter
abstract protected void setFromStyledAttributes(TypedArray attr);
// Sets all initial values of members
abstract protected void initView();
private void updateViewOnSet() {
this.requestLayout(); // CTE
this.invalidate(); // CTE
}
}
你想'simple查看'延长'T'?这是不能做到的。 –
这就是我所害怕的。你看到为什么我会喜欢这个背后的一般概念吗? **您是否看到提供我想要的抽象** –