gpt4 book ai didi

Java 类泛型导致类扩展该类型?

转载 作者:行者123 更新时间:2023-12-01 04:19:56 25 4
gpt4 key购买 nike

如果您不知道这些类(class)会讲什么,请查看 here!另外,我还不能 100% 确定这会起作用,我正在对此进行测试

<小时/>

我目前正在创建一个简化的基类,该基类将简化 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要么是我在网上研究中错过了如何做到这一点,要么是无法做到。如果无法完成,那么他们是否有任何建议来获取我上面的一些结果。如果您不知道这些类(class)中会发生什么,请查看 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

}
}

最佳答案

以下是如何完成此操作的示例:http://www.lukehorvat.com/blog/android-seekbardialogpreference/

编辑:

这样的设置会丢失吗?

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();
}

}

关于Java 类泛型导致类扩展该类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19041081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com