如果您不知道这些课程会发生什么,请看这里!另外,我还不是 100% 确定这会起作用,我正在测试这个
我目前正在创建一个简化的基类,该基类将简化在类中使用自定义 xmlAttributeSet
属性
基本上,这就是我想要的最终结果......
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();
}
}
这是具有所有魔力的“基础”类。问题实际上是类“签名”。我需要它来扩展 type T
。要么我错过了在线研究中如何做到这一点,要么无法做到。如果它不能完成,那么他们是否有任何建议可以得到我上面的一些结果。如果您不知道这些课程会发生什么,请看这里!
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
}
}