2013-09-27 93 views
1

如果你不知道什么会去这些类,那么请看看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 

    } 
} 
+1

你想'simple查看'延长'T'?这是不能做到的。 –

+0

这就是我所害怕的。你看到为什么我会喜欢这个背后的一般概念吗? **您是否看到提供我想要的抽象** –

回答

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

} 
+0

这是一个与我在评论一开始就分享的链接相似的示例。我知道该怎么做。我正在寻找**抽象出额外的细节,并试图使课程尽可能简单。我喜欢这个链接,以显示它是如何正常完成的很好的参考。我的问题是具体处理泛型并将基类扩展到该泛型 –

+0

好吧,错过了这种差异,如果没有回答,看看明天的错误 – cYrixmorten

+0

我的编辑与你想要的有多远? – cYrixmorten