繁体   English   中英

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

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

我想创建一个可点击项目列表:

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

其中box_xxx_text参数是不同项目的视图 ID。 有谁知道该怎么做?

由于您必须添加预定义的元素,因此使用数组而不是列表更方便。

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

创建 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" />

应该这样做。 第二步是定义一个数组适配器以将数组插入到列表视图中。

val arrayAdapter: ArrayAdapter<*>

然后你可以为列表视图创建一个变量,并将你的数组添加到其中

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

工作完成,希望对您有所帮助!

您可以尝试应用视图绑定,我会说这是最有效的方法......

这里描述了这个解决方案和几个更多的变体:

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