Android studio如何把item显示到RecyclerView中,item用来显示符合条件的个人信息,点击item进入详情页,示例代码
时间: 2023-06-26 08:09:25 浏览: 236
1. 首先,创建一个布局文件来定义RecyclerView和Item的样式。在布局文件中添加RecyclerView和一个Item的样式,例如:
```xml
<!-- activity_main.xml -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- item_layout.xml -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/avatar" />
<TextView
android:id="@+id/nameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name" />
</LinearLayout>
```
2. 接下来,创建一个数据类来存储个人信息,例如:
```kotlin
data class Person(val name: String, val age: Int, val job: String)
```
3. 创建一个Adapter类来将数据绑定到RecyclerView中的Item中,例如:
```kotlin
class PersonAdapter(private val personList: List<Person>, private val context: Context) : RecyclerView.Adapter<PersonAdapter.ViewHolder>() {
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val imageView: ImageView = itemView.findViewById(R.id.imageView)
val nameTextView: TextView = itemView.findViewById(R.id.nameTextView)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val person = personList[position]
holder.imageView.setImageResource(R.drawable.avatar)
holder.nameTextView.text = person.name
holder.itemView.setOnClickListener {
// 点击事件,跳转到详情页
val intent = Intent(context, DetailActivity::class.java)
intent.putExtra("person", person)
context.startActivity(intent)
}
}
override fun getItemCount(): Int {
return personList.size
}
}
```
4. 在MainActivity中使用RecyclerView和Adapter来显示数据,例如:
```kotlin
class MainActivity : AppCompatActivity() {
private val personList = listOf(
Person("Tom", 20, "Student"),
Person("Jerry", 25, "Engineer"),
Person("Bob", 30, "Teacher")
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
val layoutManager = LinearLayoutManager(this)
recyclerView.layoutManager = layoutManager
val adapter = PersonAdapter(personList, this)
recyclerView.adapter = adapter
}
}
```
5. 最后,创建一个详情页的Activity来显示个人信息的详细内容,例如:
```kotlin
class DetailActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
val person = intent.getSerializableExtra("person") as Person
val nameTextView = findViewById<TextView>(R.id.nameTextView)
val ageTextView = findViewById<TextView>(R.id.ageTextView)
val jobTextView = findViewById<TextView>(R.id.jobTextView)
nameTextView.text = person.name
ageTextView.text = person.age.toString()
jobTextView.text = person.job
}
}
```
以上就是使用RecyclerView和Adapter来显示符合条件的个人信息,并且点击Item进入详情页的示例代码。
阅读全文