Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: extend Linearlayout, but need same for RelativeLayout. Duplicate code unavoidable?

I have this code:

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

Now I also need a version for RelativeLayout, so I would duplicate the class file and replace "extends LinearLayout" with "extends RelativeLayout". I think that would be bad, because I do not want any duplicate code.

How would I go about achieving my goal, seeing that Java does not allow multiple inheritance?

I read something about the composition design pattern, but I am not sure how to implement that.

Maybe someone could give me a starting point as to how to most elegantly solve this problem?

like image 283
cdbeelala89 Avatar asked Feb 10 '13 17:02

cdbeelala89


1 Answers

You don't need to extend both to avoid duplicate code. You can do something like this:

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

        ...

}
like image 82
mommcilo Avatar answered Oct 13 '22 03:10

mommcilo