我是Android系统的新手,我正在研究RecycleView是如何工作的。为此,我创建了一个简单的示例项目,其中显示了一个项目列表。我的activity_main布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewItems"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
我为 RecycleView 中的元素创建了一个list_layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="Title"/>
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description"
android:layout_below="@id/title"/>
</RelativeLayout>
使用这些布局,我创建了这样的Item模型:
data class Item(
val title: String,
val description: String
)
我在一个名为ItemsAdapter的类中为Item模型创建了适配器:
class ItemsAdapter(val items: List<Item>) : RecyclerView.Adapter<ItemsAdapter.ItemViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
return ItemViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.list_layout, parent, false)
)
}
override fun getItemCount() = items.size
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
val item = items[position]
holder.view.title.text = item.title
holder.view.description.text = item.description
}
class ItemViewHolder(val view: View) : RecyclerView.ViewHolder(view)
}
现在,在我的MainActivity类中,我创建了一个ArrayList,其中包含要在应用程序中显示的Item对象,如下所示:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerViewItems.layoutManager = LinearLayoutManager(this)
val items = ArrayList<Item>()
items.add(Item("Item 1", "This is the first added item"))
items.add(Item("Item 2", "This is the second added item"))
items.add(Item("Item 3", "This is the third added item"))
recyclerViewItems.adapter = ItemsAdapter(items)
}
}
问题是当应用程序运行时,列表只显示数组列表中的第一个元素,没有显示两个左边的项目,为什么会这样?
您已经在回收器视图布局文件(list_layout)中设置了< code > Android:layout _ height = " match _ parent " 。您应该将其设置为< code > Android:layout _ height = " wrap _ content " :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="Title"/>
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description"
android:layout_below="@id/title"/>
</RelativeLayout>