2016-04-19 17 views
6

Creo una vista personalizzata in Kotlin e desidero accedere alla sua Attributes Resource.Kotlin: come accedere a Attrs per CustomView

Qui di seguito è il mio codice

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

Nota che questo errore nei attrs nella funzione init. Mi chiedo come accedere allo attrs?

risposta

7

Non è possibile accedere a un parametro del costruttore secondario da un blocco init. Ma ci sono almeno due modi per implementare funzionalità simili.

Il primo approccio utilizza un singolo costruttore primario con parametri predefiniti invece di più costruttori secondari. In questo caso devi applicare l'annotazione @JvmOverloads al costruttore per fare in modo che Kotlin generi tre diversi costruttori.

Un approccio di secondi è costituito da due costruttori a catena e sposta il contenuto del blocco di init nel costruttore con tre argomenti.

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

Grazie! Stavo pensando a come accedere tramite il costruttore init() predefinito. – Elye

+0

Puoi anche usare l'istruzione ** let?. {} **, attrs? .let {initAttrs (context, it)} – Yvgen

0

Adattare il codice, penso che si può anche fare qualcosa di simile:

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() 
     } 
    } 
} 
Problemi correlati