Java Android:扩展Linearlayout,但相对论需要同样的功能。重复代码不可避免?

Java Android:扩展Linearlayout,但相对论需要同样的功能。重复代码不可避免?,java,android,inheritance,android-linearlayout,android-relativelayout,Java,Android,Inheritance,Android Linearlayout,Android Relativelayout,我有以下代码: public class CopyOfLinearLayoutEntry extends LinearLayout implements Checkable { private CheckedTextView _checkbox; private Context c; public CopyOfLinearLayoutEntry(Context context) { super(context); this.c = con

我有以下代码:

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();
        }
    }
}
公共类CopyOfLinearLayoutEntry扩展了LinearLayout实现可检查{
private CheckedTextView _复选框;
私有上下文c;
在线在线外部中心的公共副本(上下文){
超级(上下文);
c=上下文;
setWillNotDraw(假);
}
公共CopyOfLinearLayoutEntry(上下文、属性集属性){
超级(上下文,attrs);
c=上下文;
setWillNotDraw(假);
}
@凌驾
受保护的void onDraw(画布){
绘制strokePaint=新绘制();
strokePaint.setARGB(200255230230);
strokePaint.setStyle(Paint.Style.STROKE);
冲程油漆。设置冲程宽度(12);
Rect r=canvas.getClipBounds();
矩形轮廓=新矩形(1,1,右-1,右-1,右-1);
帆布.抽绳(右左,右上,右右上,右上,strokePaint);
}
@凌驾
充气时受保护的空隙(){
超级充气();
//查找选中的文本视图
int childCount=getChildCount();
对于(int i=0;i
现在我还需要RelativeLayout的版本,因此我将复制类文件并将“extends LinearLayout”替换为“extends RelativeLayout”。我想那会很糟糕,因为我不想要任何重复的代码

看到Java不允许多重继承,我将如何实现我的目标

我读了一些关于合成设计模式的文章,但我不知道如何实现它


也许有人能给我一个起点,告诉我如何最优雅地解决这个问题?

关于我所学到和使用的,有两种方法:

  • 您可以执行您试图避免的操作(复制类文件并将“extends LinearLayout”替换为“extends RelativeLayout”)

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


  • 我希望这对你有所帮助,你必须重新考虑你的方法

    看起来您正在使用布局来控制视图逻辑。不幸的是,你的问题没有太多关于你想要实现什么的信息

    你几乎没有机会:

    • 使用自定义逻辑实现布局代理/委托(错误方法)
    • 创建一个专用的处理程序类来控制视图对象。。。这些将独立于布局
    • 创建视图对象并使用视图对象而不是布局(可能是正确的做法)

      • 您不需要同时扩展这两种代码来避免重复代码。您可以这样做:

        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);
            }
        
                ...
        
        }
        
        导入android.content.Context;
        导入android.graphics.Canvas;
        导入android.graphics.Paint;
        导入android.graphics.Rect;
        导入android.view.view;
        导入android.view.ViewGroup;
        导入android.widget.CheckedTextView;
        公共类GenericLayout扩展了视图组{
        private CheckedTextView _复选框;
        公共通用布局(上下文){
        超级(上下文);
        //TODO自动生成的构造函数存根
        }
        @凌驾
        受保护的void onDraw(画布){
        绘制strokePaint=新绘制();
        strokePaint.setARGB(200255230230);
        strokePaint.setStyle(Paint.Style.STROKE);
        冲程油漆。设置冲程宽度(12);
        Rect r=canvas.getClipBounds();
        矩形轮廓=新矩形(1,1,右-1,右-1,右-1);
        帆布.抽绳(右左,右上,右右上,右上,strokePaint);
        }
        @凌驾
        充气时受保护的空隙(){
        超级充气();
        //查找选中的文本视图
        int childCount=getChildCount();
        对于(int i=0;i