8

我有這樣的代碼:Android:擴展Linearlayout,但RelativeLayout需要相同。重複的代碼不可避免?

public class CopyOfLinearLayoutEntry extends LinearLayout implements Checkable { 
    private CheckedTextView _checkbox; 
    private Context c; 

    public CopyOfLinearLayoutEntry(Context context) { 
     super(context); 
     this.c = context; 
     setWillNotDraw(false); 
    } 

    public CopyOfLinearLayoutEntry(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.c = context; 
     setWillNotDraw(false); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     Paint strokePaint = new Paint(); 
     strokePaint.setARGB(200, 255, 230, 230); 
     strokePaint.setStyle(Paint.Style.STROKE); 
     strokePaint.setStrokeWidth(12); 
     Rect r = canvas.getClipBounds(); 
     Rect outline = new Rect(1, 1, r.right - 1, r.bottom - 1); 
     canvas.drawLine(r.left, r.top, r.right, r.top, strokePaint); 
    } 

    @Override 
    protected void onFinishInflate() { 
     super.onFinishInflate(); 
     // find checked text view 
     int childCount = getChildCount(); 
     for (int i = 0; i < childCount; ++i) { 
      View v = getChildAt(i); 
      if (v instanceof CheckedTextView) { 
       _checkbox = (CheckedTextView) v; 
      } 
     } 
    } 

    @Override 
    public boolean isChecked() { 
     return _checkbox != null ? _checkbox.isChecked() : false; 
    } 

    @Override 
    public void setChecked(boolean checked) { 
     if (_checkbox != null) { 
      _checkbox.setChecked(checked); 
     } 
    } 

    @Override 
    public void toggle() { 
     if (_checkbox != null) { 
      _checkbox.toggle(); 
     } 
    } 
} 

現在我還需要RelativeLayout的一個版本,所以我會複製類文件和替換「擴展的LinearLayout」與「擴展RelativeLayout的」。我認爲那會很糟糕,因爲我不想要任何重複的代碼。

我會如何去實現我的目標,看到Java不允許多重繼承?

我讀了一些關於構圖設計模式,但我不知道如何實現。

也許有人可以給我一個起點,如何最優雅地解決這個問題?

回答

0

中我學到和一直在使用,有兩種方式:

  1. 你可以做你正試圖避免(複製類文件和替換「擴展的LinearLayout」什麼「延伸的RelativeLayout」 )

  2. 您可以創建2個接口和1個類:一個接口,用於擴展LinearLayout,另一個用於擴展RelativeLayout以及實現擴展接口的方法和變量的類。

我希望幫助一點點

0

你必須重新考慮你的方法。

似乎你是使用佈局來控制VIEW邏輯。不幸的是,您的問題沒有太多關於您想要實現的信息。

你有幾種可能性:

  • 實現與定製邏輯編排代理/代表(IMO不錯的辦法)
  • 做一個專門的處理程序類,以控制您的視圖對象......這些將是獨立於在佈局
  • 讓你的視圖對象並使用View對象,而不是佈局(可能要走的路)
1

你並不需要延長既避免了重複的代碼。你可以這樣做:

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Rect; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.CheckedTextView; 

public class GenericLayout extends ViewGroup{ 

    private CheckedTextView _checkbox; 

    public GenericLayout(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     Paint strokePaint = new Paint(); 
     strokePaint.setARGB(200, 255, 230, 230); 
     strokePaint.setStyle(Paint.Style.STROKE); 
     strokePaint.setStrokeWidth(12); 
     Rect r = canvas.getClipBounds(); 
     Rect outline = new Rect(1, 1, r.right - 1, r.bottom - 1); 
     canvas.drawLine(r.left, r.top, r.right, r.top, strokePaint); 
    } 

    @Override 
    protected void onFinishInflate() { 
     super.onFinishInflate(); 
     // find checked text view 
     int childCount = getChildCount(); 
     for (int i = 0; i < childCount; ++i) { 
      View v = getChildAt(i); 
      if (v instanceof CheckedTextView) { 
       _checkbox = (CheckedTextView) v; 
      } 
     } 
    } 

    public boolean isChecked() { 
     return _checkbox != null ? _checkbox.isChecked() : false; 
    } 

    public void setChecked(boolean checked) { 
     if (_checkbox != null) { 
      _checkbox.setChecked(checked); 
     } 
    } 

    public void toggle() { 
     if (_checkbox != null) { 
      _checkbox.toggle(); 
     } 
    } 

    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) { 
     // TODO Auto-generated method stub 

    } 

} 



public class Linear extends LinearLayout { 

    GenericLayout generic; 

    public Linear(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
     generic = new GenericLayout(context); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     // TODO Auto-generated method stub 
     generic.onDraw(canvas); 
    } 

     ... 

} 

public class Relative extends RelativeLayout{ 

    GenericLayout generic; 

    public Relative(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     // TODO Auto-generated method stub 
     generic.onDraw(canvas); 
    } 

     ... 

}