Android: extend Linringayout, but for RelativeLayout it is necessary. Is duplicate code inevitable?

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 to duplicate the code.

How can I achieve my goal by seeing that Java does not allow multiple inheritance?

I read something about the composition design template, but I'm not sure how to implement this.

Can someone give me a starting point on how to most elegantly solve this problem?

+8
java android inheritance relativelayout android-linearlayout
source share
3 answers

You do not need to extend both options to avoid code duplication. 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); } ... } 
+1
source share

From what I learned and use, there are two ways:

  • You can do what you are trying to avoid (duplicate the class file and replace "extends LinearLayout" with "extends RelativeLayout")

  • You can create 2 interfaces and 1 class: one interface extending LinearLayout, another one for extending RelativeLayout and a class that implements methods and variables of extension interfaces.

I hope this helps a little

0
source share

You should reconsider your approach.

It looks like you are using a layout to control the VIEW logic. Unfortunately, your question does not contain too much information about what you are trying to achieve.

You have few options:

  • implement proxy server LAYOUT / delegate with user logic (bad IMO approach)
  • create a dedicated HANDLER class to manage your VIEW objects ... they will be independent of LAYOUT
  • make your VIEW object and use the VIEW object instead of LAYOUT (maybe a path)
0
source share

All Articles