Pro Android with Kotlin
CHAPTER 9: User Interface
Download 5.42 Mb. Pdf ko'rish
|
@de android telegram Pro Android with Kotlin Developing Modern Mobile
197
CHAPTER 9: User Interface "http://schemas.android.com/apk/res/android" android:id="@+id/fl" android:layout_width="match_parent" android:layout_height="match_parent"> Use this as a layout XML file, say /res/layout/activity_main.xml, and write the following as a sample activity: class MainActivity : AppCompatActivity() { var tv:TextView? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // For example add a text at a certain position tv = TextView(this).apply { text = "Dynamic" x = 37.0f y = 100.0f } fl.addView(tv) } } To add a button that shifts the text from the previous example around, you can write the following: val WRAP = ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) fl.addView( Button(this).apply { text = "Go" setOnClickListener { v -> v?.run { x += 30.0f * (-0.5f + Math.random().toFloat()) y += 30.0f * (-0.5f + Math.random().toFloat()) } } }, WRAP ) If you don’t need total control and want a layout object to do its children positioning and sizing job the way it is designed, adding children to other layout types like, for example, a LinearLayout, this is possible from inside the code. Just use the addView() method without explicitly setting the position via setX() or setY(). Under certain circumstances you must 198 CHAPTER 9: User Interface use layoutObject.invalidate() to trigger a re-layout afterward. The latter has to be done from inside the UI thread or inside runOnUiThread{ ... }. Adapters and List Controls The need to display a list with a variable number of items happens quite often, especially in a corporate environment. While AdapterView and Adapter objects with various subclasses have been around for a while, we concentrate on the relatively new and high-performing recycler views. You will see that with Kotlin’s conciseness, implementing a recycler view happens in an elegant and comprehensive manner. The basic ides is as follows: you have an array or a list or another collection of data items, maybe from a database, and you want to send them to a single UI element that does all the presentation, including rendering all visible items and providing a scroll facility if necessary. Each item’s presentation either should depend on an item XML layout file or be generated dynamically from inside the code. The mapping from each data item’s member to the corresponding view element from inside the item’s UI representation is to be handled by an adapter object. With recycler views, this all happens in a straightforward manner, but first we have to include a support library because the recycler views are not part of the framework. To do so, inside your module’s build.gradle file, add the following: implementation 'com.android.support:recyclerview-v7:26.1.0' Add this inside the dependencies{ ... } section (on one line; remove the newline after implementation). To tell the app we want to use a recycler view, inside your activity’s layout file, add the following: android:scrollbars="vertical" ... /> Specify its layout options as for any other View. For the layout of an item from the list, create another layout file inside res/layout, say item.xml, with the following sample content: android:layout_height="?android:attr/ listPreferredItemHeight" android:padding="8dip" > |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling