2016-04-19 7 views
6

के लिए Attrs तक कैसे पहुंचे I Kotlin में एक कस्टम दृश्य बनाएं, और इसके गुण संसाधनों तक पहुंच बनाना चाहूंगा।कोटलिन: कस्टमव्यू

नीचे मेरी कोड

class CustomCardView : FrameLayout { 

    constructor(context: Context) : super(context) 

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) 

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) 

    init { 
     LayoutInflater.from(context).inflate(R.layout.view_custom_card, this, true) 

     if (attrs != null) { 
      val a = context.obtainStyledAttributes(attrs, R.styleable.custom_card_view) 
      if (a.hasValue(R.styleable.custom_card_view_command)) { 
       var myString = a.getString(R.styleable.custom_card_view_command) 
      } 
     } 
    } 
} 

ध्यान दें कि यह init समारोह में attrs में त्रुटि नहीं है। मैं सोच रहा हूं कि attrs तक कैसे पहुंचे?

उत्तर

7

आप init ब्लॉक से द्वितीयक कन्स्ट्रक्टर पैरामीटर तक नहीं पहुंच सकते हैं। लेकिन कम से कम दो तरीके हैं कि आप समान कार्यक्षमता को कैसे कार्यान्वित कर सकते हैं।

पहला दृष्टिकोण कई माध्यमिक रचनाकारों के बजाय डिफ़ॉल्ट पैरामीटर के साथ एक प्राथमिक कन्स्ट्रक्टर का उपयोग कर रहा है। कोटलिन को तीन अलग-अलग रचनाकार उत्पन्न करने के लिए आपको इस मामले में @JvmOverloads कन्स्ट्रक्टर को एनोटेशन लागू करना होगा।

class CustomCardView @JvmOverloads constructor(
    context: Context, 
    attrs: AttributeSet? = null, 
    defStyleAttr: Int = 0 
) : FrameLayout { 

    init { 
    LayoutInflater.from(context).inflate(R.layout.view_custom_card, this, true) 

    if (attrs != null) { 
     val a = context.obtainStyledAttributes(attrs, R.styleable.custom_card_view) 
     if (a.hasValue(R.styleable.custom_card_view_command)) { 
     var myString = a.getString(R.styleable.custom_card_view_command) 
     } 
    } 
    } 
} 

एक सेकंड दृष्टिकोण दो श्रृंखला निर्माणकर्ता है और तीन तर्कों के साथ इनिट ब्लॉक सामग्री को कन्स्ट्रक्टर में ले जाएं।

class CustomCardView : FrameLayout { 

    constructor(context: Context) : 
     this(context, null) 

    constructor(context: Context, attrs: AttributeSet) : 
     this(context, attrs, 0) 

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : 
     super(context, attrs, defStyleAttr) { 

    LayoutInflater.from(context).inflate(R.layout.view_custom_card, this, true) 

    if (attrs != null) { 
     val a = context.obtainStyledAttributes(attrs, R.styleable.custom_card_view) 
     if (a.hasValue(R.styleable.custom_card_view_command)) { 
     var myString = a.getString(R.styleable.custom_card_view_command) 
     } 
    } 
    } 
} 
+0

धन्यवाद! मैं सोच रहा था कि डिफ़ॉल्ट init() कन्स्ट्रक्टर के माध्यम से कैसे पहुंचे। – Elye

+0

आप ** का उपयोग भी कर सकते हैं? {} ** कथन, attrs? .let {initAttrs (संदर्भ, यह)} – Yvgen

0

अपने कोड के अनुकूल बनाना, मुझे लगता है कि आप भी कुछ इस तरह कर सकते हैं:

class CustomCardView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : 
     FrameLayout(context, attrs, defStyleAttr) { 

    constructor(context: Context) : this(context, null) 

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) 


    init { 
     LayoutInflater.from(context).inflate(R.layout.view_custom_card, this, true) 

     if (attrs != null) { 
      val a = context.obtainStyledAttributes(attrs, R.styleable.custom_card_view) 
      if (a.hasValue(R.styleable.custom_card_view_command)) { 
       var myString = a.getString(R.styleable.custom_card_view_command) 
      } 
      a.recycle() 
     } 
    } 
}