2017-06-22 7 views
2

ビュー所有者のビューを作成するために、recyckoViewアダプタでankoを使用しています。私はこれを正常に行っているが、それはビューidでkotlin合成を使用して参照する方法がわからない値/ ids.xmlでカスタムIDの参照を取得する方法

値/ ids.xml(私はfindViewByIdせずにそれを取得したい)

<resources> 
<item name="txv1" type="id"/> 
<item name="txv2" type="id"/> 

マイアンコgetViewメソッドコード:

private fun getView(context: Context): View{ 
     return with(context){ 
      linearLayout { 
       lparams(width = matchParent, height = wrapContent) 
       padding = dip(10) 
       orientation = android.widget.LinearLayout.HORIZONTAL 

       //Task Number 
       textView { 
        id = R.id.txv1 
        text = "TextView 22" 
        textSize = 16f 
        typeface = Typeface.MONOSPACE 
        padding =dip(5) 
       }.lparams(){ 
        weight = 1f 
       } 

       //Task Name 
       textView { 
        id = R.id.txv2 
        text= "TextView 33" 
        textSize = 16f 
        typeface = android.graphics.Typeface.DEFAULT_BOLD 
        padding =dip(5) 
       } 
      } 
     } 
    } 

私はids.xmlからカスタムIDを割り当てるだが、どのよう

findViewByIdせずにそれを得るために

ありがとう

+1

私はAnkoと同じ問題に遭遇しました。回避策として現在、静的定数を静的定数として使用しています。例: オブジェクトID { @IdRes val toolbar = View.generateViewId();/* ... */ } '' – kirillkh

答えて

1

多くの研究をした結果、今は安藤の作者による直接参照はできないという結論に至りました。

val txv1 = findViewById(R.id.txv1) as TextView 

OR
をアンコメソッド内作成されたビューの参照を保持する変数を宣言 -
問題を回避するには使用することです。
コードは以下のとおりである - これは他の人を助ける

var txv1: TextView? = null 

    private fun getView(context: Context): View{ 
     return with(context){ 
      linearLayout {   
       txv1 = textView { 
        text = "TextView" 
       } 
      } 
     } 
    } 

希望。ありがとう

関連する問題