前提・実現したいこと
Kotlin Android ExtensionsではViewをTextView等にキャストした状態で参照できると思うのですが、<include/>を利用した場合にはキャストが行われない現象を解決したいです。
環境
Kotlin 1.2.61
minSdkVersion 19
compileSdkVersion 28
該当のソースコード
MainActivity
1import android.os.Bundle 2import android.support.v7.app.AppCompatActivity 3import android.widget.ScrollView 4import kotlinx.android.synthetic.main.activity_main.* 5import kotlinx.android.synthetic.main.included.view.* 6 7class MainActivity : AppCompatActivity() { 8 override fun onCreate(savedInstanceState: Bundle?) { 9 super.onCreate(savedInstanceState) 10 setContentView(R.layout.activity_main) 11 12 // できる 13 textView.text = "text" 14 included.textView.text = "text" 15 (included as ScrollView).maxScrollAmount // 明示的なキャストをしたくない 16 17 // できない 18 // included.maxScrollAmount 19 } 20}
========== activity_main.xml =========== <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="textView"/> <include layout="@layout/included" android:id="@+id/included" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
========== included.xml =========== <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="textView"/> </ScrollView>
MainActivity
のレイアウトはactivity_main.xml
で、activity_main.xml
の<include/>
でincluded.xml
のレイアウトをインクルードしています。MainActivity
のコメントに記述してあるように、included
がScrollView
にキャストされた状態で参照できるようにしたいです。
追記
included.xml
のScrollView
にscrollView
とidを振って以下のようにアクセスすると、実行時にnull
が帰ってきてしまいます。
Kotlin
1included.scrollView.maxScrollAmount
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/03 23:51