导致类扩展该类型的Java类泛型?
如果你不知道在这些课程中会发生什么,那么请看,我不是100%确定这会起作用,我正在测试这个导致类扩展该类型的Java类泛型?,java,android,generics,view,abstraction,Java,Android,Generics,View,Abstraction,如果你不知道在这些课程中会发生什么,那么请看,我不是100%确定这会起作用,我正在测试这个 我目前正在创建一个简化的基类,它可以简化classAttributeSet中自定义xml属性的使用 基本上,这就是我想要的最终结果… public class SimpleViewImplementation extends SimpleView<LinearLayout> { // List of members here private String value;
我目前正在创建一个简化的基类,它可以简化class
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();
}
}
下面是一个如何做到这一点的示例: 编辑: 在这样的设置中会丢失吗
public abstract class SimpleView {
private View view;
public SimpleView(Context context, View view) {
if (view == null || !(view instanceof View)) {
throw new RuntimeException("SimpleView expects an instance of View");
}
this.view = view;
initView();
}
public SimpleView(Context context, View view, AttributeSet attrs) {
if (view == null || !(view instanceof View)) {
throw new RuntimeException("SimpleView expects an instance of View");
}
this.view = view;
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();
protected void updateViewOnSet() {
view.requestLayout();
view.invalidate();
}
}
我想你可以用它来:
public class SimpleViewImplementation extends SimpleView {
// List of members here
private String value;
public SimpleViewImplementation(Context context) {
super(context, new RelativeLayout(context));
// Define your View here (demonstrated with RelativeLayout)
}
public SimpleViewImplementation(Context context, View view) {
// Predefined view
super(context, view);
}
public SimpleViewImplementation(Context context, AttributeSet attrs) {
super(context, new LinearLayout(context), attrs);
// Define your View here (demonstrated with LinearLayout)
}
public SimpleViewImplementation(Context context, View view, AttributeSet attrs) {
super(context, view, attrs);
// Predefined view
}
@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();
}
}
这是一个与我在评论开头分享的链接相当的例子。我知道怎么做。我正在考虑抽象出额外的细节,并试图使类尽可能简单。我确实喜欢这个链接,以显示一个很好的参考,它通常是如何做的。我的问题是专门处理泛型,并将基类扩展到那个泛型,忽略了这个区别,我明天会看一看,如果HMMM没有回答,我将不得不看更多。看起来这可能是我一直在寻找的。昨晚我提出了一个稍微不受欢迎的解决方案,这个解决方案似乎有效,但我不喜欢它的开销。你的这个解决方案可能会解决这个问题。不管怎样,我今天晚些时候会去看看。很公平,我希望它能帮助你朝着正确的方向前进。研究它,我至少学会了克里特泛型:D,尽管这在这个用例中是不可能的。我还不能让你的或我的工作。我将从头开始。类似于您原来的链接,看看我是否可以从那里再次扩展它,您想要
SimpleView
扩展T
?那不可能,这就是我害怕的。你知道为什么我会喜欢这个的一般概念吗?你看到了我想要的抽象吗
public class SimpleViewImplementation extends SimpleView {
// List of members here
private String value;
public SimpleViewImplementation(Context context) {
super(context, new RelativeLayout(context));
// Define your View here (demonstrated with RelativeLayout)
}
public SimpleViewImplementation(Context context, View view) {
// Predefined view
super(context, view);
}
public SimpleViewImplementation(Context context, AttributeSet attrs) {
super(context, new LinearLayout(context), attrs);
// Define your View here (demonstrated with LinearLayout)
}
public SimpleViewImplementation(Context context, View view, AttributeSet attrs) {
super(context, view, attrs);
// Predefined view
}
@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();
}
}