转载

java – Android:extend 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的版本,所以我会复制类文件并将“extends LinearLayout”替换为“extends RelativeLayout”.我认为那会很糟糕,因为我不想要任何重复的代码.

我怎样才能实现我的目标,看到Java不允许多重继承?

我读了一些有关合成设计模式的内容,但我不确定如何实现它.

也许有人可以给我一个关于如何最优雅地解决这个问题的起点?

您不需要扩展它们以避免重复代码.你可以这样做:
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);
    }

        ...

}

翻译自:https://stackoverflow.com/questions/14800686/android-extend-linearlayout-but-need-same-for-relativelayout-duplicate-code-u

原文  https://codeday.me/bug/20190111/505030.html
正文到此结束
Loading...