安卓开发 原生View 垂直recyclerview 嵌套水平recyclerview 帮我写完整kotlin 代码
时间: 2024-03-01 15:55:29 浏览: 97
好的,以下是一个垂直 RecyclerView 嵌套水平 RecyclerView 的 Kotlin 代码示例:
```
class MainActivity : AppCompatActivity() {
private lateinit var verticalRecyclerView: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 初始化垂直 RecyclerView
verticalRecyclerView = findViewById(R.id.verticalRecyclerView)
verticalRecyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
val verticalAdapter = VerticalAdapter()
verticalRecyclerView.adapter = verticalAdapter
// 设置垂直 RecyclerView 的数据源
val verticalDataList = mutableListOf<VerticalItem>()
verticalDataList.add(VerticalItem("Item 1"))
verticalDataList.add(VerticalItem("Item 2"))
verticalDataList.add(VerticalItem("Item 3"))
verticalAdapter.setData(verticalDataList)
}
// 垂直 RecyclerView 的适配器
inner class VerticalAdapter : RecyclerView.Adapter<VerticalAdapter.VerticalViewHolder>() {
private var dataList = mutableListOf<VerticalItem>()
fun setData(dataList: List<VerticalItem>) {
this.dataList.clear()
this.dataList.addAll(dataList)
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VerticalViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_vertical, parent, false)
return VerticalViewHolder(view)
}
override fun onBindViewHolder(holder: VerticalViewHolder, position: Int) {
val verticalItem = dataList[position]
holder.titleTextView.text = verticalItem.title
// 初始化水平 RecyclerView
val horizontalLayoutManager = LinearLayoutManager(holder.itemView.context, RecyclerView.HORIZONTAL, false)
holder.horizontalRecyclerView.layoutManager = horizontalLayoutManager
val horizontalAdapter = HorizontalAdapter()
holder.horizontalRecyclerView.adapter = horizontalAdapter
// 设置水平 RecyclerView 的数据源
val horizontalDataList = mutableListOf<HorizontalItem>()
horizontalDataList.add(HorizontalItem("Subitem 1"))
horizontalDataList.add(HorizontalItem("Subitem 2"))
horizontalDataList.add(HorizontalItem("Subitem 3"))
horizontalAdapter.setData(horizontalDataList)
}
override fun getItemCount(): Int {
return dataList.size
}
inner class VerticalViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val titleTextView: TextView = itemView.findViewById(R.id.titleTextView)
val horizontalRecyclerView: RecyclerView = itemView.findViewById(R.id.horizontalRecyclerView)
}
}
// 水平 RecyclerView 的适配器
inner class HorizontalAdapter : RecyclerView.Adapter<HorizontalAdapter.HorizontalViewHolder>() {
private var dataList = mutableListOf<HorizontalItem>()
fun setData(dataList: List<HorizontalItem>) {
this.dataList.clear()
this.dataList.addAll(dataList)
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HorizontalViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_horizontal, parent, false)
return HorizontalViewHolder(view)
}
override fun onBindViewHolder(holder: HorizontalViewHolder, position: Int) {
val horizontalItem = dataList[position]
holder.titleTextView.text = horizontalItem.title
}
override fun getItemCount(): Int {
return dataList.size
}
inner class HorizontalViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val titleTextView: TextView = itemView.findViewById(R.id.titleTextView)
}
}
}
// 数据类
data class VerticalItem(val title: String)
data class HorizontalItem(val title: String)
```
其中,垂直 RecyclerView 使用 `VerticalAdapter` 作为适配器,水平 RecyclerView 使用 `HorizontalAdapter` 作为适配器,分别用两个数据类 `VerticalItem` 和 `HorizontalItem` 表示它们的数据项。这个示例代码仅供参考,具体实现还需要根据项目需求进行调整。
阅读全文