8

我有这个代码:

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 不允许多重继承,我将如何实现我的目标?

我读了一些关于组合设计模式的东西,但我不知道如何实现它。

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

4

3 回答 3

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

        ...

}
于 2013-02-21T22:40:20.670 回答
0

你必须重新考虑你的方法。

好像您正在使用布局来控制 VIEW 逻辑。不幸的是,您的问题没有太多关于您要达到的目标的信息。

你的可能性很小:

  • 使用自定义逻辑实现 LAYOUT 代理/委托(坏方法 IMO)
  • 制作一个专用的 HANDLER 类来控制您的 VIEW 对象......这些将独立于 LAYOUT
  • 制作您的 VIEW 对象并使用 VIEW 对象而不是 LAYOUT (可能是要走的路)
于 2013-02-11T14:33:59.907 回答
0

在我学习和使用的东西中,有两种方法:

  1. 你可以做你试图避免的事情(复制类文件并将“扩展线性布局”替换为“扩展相对布局”)

  2. 您可以创建2个接口和1个类:一个扩展LinearLayout的接口,另一个扩展RelativeLayout的接口以及实现扩展接口的方法和变量的类。

我希望这会有所帮助

于 2013-02-11T08:56:10.853 回答