Kotlin for Android Developers


 Writing your first class


Download 1.04 Mb.
Pdf ko'rish
bet18/79
Sana18.06.2023
Hajmi1.04 Mb.
#1588404
1   ...   14   15   16   17   18   19   20   21   ...   79
Bog'liq
Kotlin for Android Developers Learn Kotlin the Easy Way While Developing an Android App ( PDFDrive )

5 Writing your first class
We already have our
MainActivity.kt
class. This activity will render a list of daily forecast for the
next week, so the layout requires some changes.
5.1 Creating the layout
The main view that will render the forecast list will be a
RecyclerView
, so a new dependency is
required. Modify the
build.gradle
file:
1
dependencies {
2
compile fileTree(dir: 'libs', include: ['*.jar'])
3
compile "com.android.support:appcompat-v7:$support_version"
4
compile "com.android.support:recyclerview-v7:$support_version"
5
...
6
}
Now, in
activity_main.xml
:
1
2
android:layout_width="match_parent"
3
android:layout_height="match_parent">
4
5
6
android:id="@+id/forecast_list"
7
android:layout_width="match_parent"
8
android:layout_height="match_parent"/>
9
10

In
MainActivity.kt
, remove the line we added to test everything worked (it will be showing an
error now). We’ll continue using the good old
findViewById()
for the time being:
1
val forecastList = findViewById(R.id.forecast_list) as RecyclerView
2
forecastList.layoutManager = LinearLayoutManager(this)
19


5 Writing your first class
20
As you can see, we define the variable and cast it to
RecyclerView
. It’s a bit different from Java, and
we’ll see those differences in the next chapter. A
LayoutManager
is also specified, using the property
naming instead of the setter. A list will be enough for this layout, so a
LinearLayoutManager
will
make it.

Download 1.04 Mb.

Do'stlaringiz bilan baham:
1   ...   14   15   16   17   18   19   20   21   ...   79




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling