简体   繁体   English

想在android studio创建查看项目列表

[英]Want to create a list of view items in android studio

I want to create a list of clickable items as:我想创建一个可点击项目列表:

val clickableViews: List<View> = listOf(box_one_text, box_two_text, box_three_text, box_four_text, box_five_text, constraint_layout)

where box_xxx_text parameters are view-ids of different items.其中box_xxx_text参数是不同项目的视图 ID。 Does anyone know how to do it?有谁知道该怎么做?

Since you have to add predefined elements, it's more convenient to use an array instead of a list.由于您必须添加预定义的元素,因此使用数组而不是列表更方便。

val array = arrayOf(
            "One", "Two", "Three")

The first step to create a ListView is of course defining a ListView in your xml layout file.创建 ListView 的第一步当然是在 xml 布局文件中定义一个 ListView。

<ListView
android:id="@+id/list_view"
  android:layout_width="0dp"
  android:layout_height="0dp"
  app:layout_constraintBottom_toBottomOf="parent"
  app:layout_constraintEnd_toEndOf="parent"
  app:layout_constraintStart_toStartOf="parent"
  app:layout_constraintTop_toTopOf="parent" />

This should do.应该这样做。 The second step is to define an array adapter to insert your array into your listview.第二步是定义一个数组适配器以将数组插入到列表视图中。

val arrayAdapter: ArrayAdapter<*>

Then you can create a variable for the listview, and add your array into it然后你可以为列表视图创建一个变量,并将你的数组添加到其中

    var mListView = findViewById<ListView>(R.id.list_view)
    arrayAdapter = ArrayAdapter(this,
        android.R.layout.layoutfilename, array)
    mListView.adapter = arrayAdapter

Job done, hope it helps!工作完成,希望对您有所帮助!

You could try applying view binding, I'd say that's the most efficient way...您可以尝试应用视图绑定,我会说这是最有效的方法......

This solution and a couple more variants are described here:这里描述了这个解决方案和几个更多的变体:

https://github.com/udacity/andfun-kotlin-color-my-views/issues/11 https://github.com/udacity/andfun-kotlin-color-my-views/issues/11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM