说明:
kotlin语言,用recycleview实现gridview效果,监听点击item,回调
效果图:
step1:
package com.example.flushfragmentdemo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.GridLayoutManager
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
private lateinit var courseAdapter: CourseAdapter
private val courseList = listOf(
Course(R.mipmap.ic_launcher, "Android + Java + Firebase"),
Course(R.mipmap.ic_launcher, "Java Foundations"),
Course(R.mipmap.ic_launcher, "Android"),
Course(R.mipmap.ic_launcher, "Firebase")
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
courseAdapter = CourseAdapter(courseList) { course, position ->
Log.e("TAG","click:"+position)
}
rvLanguage.layoutManager = GridLayoutManager(this, 3) // 2 columns
rvLanguage.adapter = courseAdapter
}
}
step2:
package com.example.flushfragmentdemo
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class CourseAdapter(
private val courseList: List<Course>,
private val clickListener: (Course, Int) -> Unit // Add this parameter
) : RecyclerView.Adapter<CourseAdapter.CourseViewHolder>() {
// ViewHolder class that binds the views
class CourseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val courseImage: ImageView = itemView.findViewById(R.id.courseImage)
val courseText: TextView = itemView.findViewById(R.id.courseText)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CourseViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_course, parent, false)
return CourseViewHolder(view)
}
override fun onBindViewHolder(holder: CourseViewHolder, position: Int) {
val course = courseList[position]
holder.courseImage.setImageResource(course.imageResId)
holder.courseText.text = course.name
// Set a click listener on the itemView (the entire item)
holder.itemView.setOnClickListener {
clickListener(course, position) // Call the passed in listener
}
}
override fun getItemCount(): Int {
return courseList.size
}
}
step3:
package com.example.flushfragmentdemo
data class Course(val imageResId: Int, val name: String)
step4:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_margin="8dp">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvLanguage"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
step5:
<?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="wrap_content"
android:orientation="vertical"
android:layout_margin="4dp"
android:background="@android:color/holo_red_dark"
android:padding="8dp">
<!-- Course Image -->
<ImageView
android:id="@+id/courseImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="center"
android:src="@mipmap/ic_launcher" />
<!-- Course Text -->
<TextView
android:id="@+id/courseText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="4dp"
android:gravity="left"
android:text="Course Name"
android:textSize="12sp"
android:textColor="@color/black" />
<!-- Grey Divider -->
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@android:color/holo_green_dark"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp" />
</LinearLayout>
end