2013-09-27 138 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