简体   繁体   English

如何在列表视图中使用视图绑定?

[英]how to use viewbinding in listview?

I'm a newbie.I find the listview doesn't show up if i use viewbinding to control the listview.the code is我是新手。如果我使用视图绑定来控制列表视图,我发现列表视图不会显示。代码是

class MainActivity : AppCompatActivity() {
private val data = listOf("Apple", "Banana", "Orange", "Watermelon",
    "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango")
private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    val listviewdapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data)
    binding.listview1.adapter = listviewdapter
    
    }
}

and the xml is simple xml 很简单

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ListView
    android:id="@+id/listview1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

but if i use findViewById the listview will show up, why?但是如果我使用findViewById列表视图会出现,为什么?

class MainActivity : AppCompatActivity() {
private val data = listOf("Apple", "Banana", "Orange", "Watermelon",
    "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango")
private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    val listviewdapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data)
    val listview1 = findViewById<ListView>(R.id.listview1)
    listview1.adapter=listviewdapter
    }
}

Because list items aren't inflated when the list is created.因为在创建列表时列表项不会膨胀。 The binding won't work for the listview at all.该绑定根本不适用于列表视图。 It will only work for the main activity.它只适用于主要活动。 And in fact it couldn't work for the listview- you're going to have multiple of each id, 1 for each on screen element (plus a few possibly).事实上,它不适用于列表视图 - 每个 id 都有多个,屏幕上的每个元素有 1 个(可能还有一些)。 If you want to use viewbinding inside the list view, the binding would have to be in the adapter.如果你想在列表视图中使用视图绑定,绑定必须在适配器中。

Also, you probably shouldn't be using ListView anymore.此外,您可能不应该再使用 ListView 了。 Everything moved to RecyclerView about 8 years ago now.大约 8 年前,一切都转移到了 RecyclerView。

Do like this...这样做...

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
 
    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ListView
            android:id="@+id/listview1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>
</layout>

and then you can access your view in code file然后您可以在代码文件中访问您的视图

binding.listview1

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

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