1

如果您不知道这些课程会发生什么,请看这里!另外,我还不是 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

    }
}
4

1 回答 1

0

这是如何完成的示例: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();
}

}
于 2013-09-27T01:08:22.627 回答