8

मैं इस कोड है:Android: 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 के लिए एक संस्करण की आवश्यकता है, तो मैं वर्ग फ़ाइल नकल और "RelativeLayout फैली" के साथ "फैली LinearLayout" होगा। मुझे लगता है कि यह बुरा होगा, क्योंकि मुझे कोई डुप्लिकेट कोड नहीं चाहिए।

मैं अपने लक्ष्य को प्राप्त करने के बारे में कैसे जाउंगा, यह देखते हुए कि जावा एकाधिक विरासत की अनुमति नहीं देता है?

मैंने रचना डिजाइन पैटर्न के बारे में कुछ पढ़ा है, लेकिन मुझे यकीन नहीं है कि इसे कैसे कार्यान्वित किया जाए।

शायद कोई मुझे इस समस्या को हल करने के तरीके के बारे में एक प्रारंभिक बिंदु दे सकता है?

उत्तर

0
मैं क्या सीखा है और किया गया है का उपयोग करने का

, वहाँ दो तरीके हैं:

  1. आप क्या आप (से बचने के वर्ग फ़ाइल नकल और की जगह के साथ "फैली LinearLayout" की कोशिश कर रहे कर सकते हैं "फैली RelativeLayout")

  2. आप 2 इंटरफेस और 1 वर्ग बना सकते हैं: एक इंटरफ़ेस जो लीनियरलाउट को बढ़ाता है, दूसरा रिलेवेटिवआउट और विस्तारित इंटरफेस के तरीकों और चर को लागू करने वाला वर्ग।

मुझे आशा है कि एक छोटे से

0

आप अपने दृष्टिकोण पर पुनर्विचार करने के लिए है में मदद करता है।

ऐसा लगता है कि आप दृश्य तर्क को नियंत्रित करने के लिए लेआउट का उपयोग कर रहे हैं। दुर्भाग्य से आपके प्रश्न के बारे में बहुत अधिक जानकारी नहीं है जो आप प्राप्त करने की कोशिश कर रहे हैं।

  • आपके विचार वस्तुओं को नियंत्रित करने के लिए कस्टम तर्क के साथ लेआउट प्रॉक्सी/प्रतिनिधि (IMO बुरा दृष्टिकोण)
  • को लागू करना एक समर्पित हैंडलर श्रेणी ... इन पर स्वतंत्र हो जाएगा:

    आप कुछ संभावनाएं है लेआउट

  • आपके विचार वस्तु बनाने के लिए और दृश्य वस्तु के बजाय लेआउट (शायद जाने का रास्ता)
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); 
    } 

     ... 

}