写一个recyclerView类似相册支持多选删除的例子
时间: 2024-03-18 09:39:46 浏览: 68
好的,以下是一个简单的RecyclerView类似相册支持多选删除的例子,使用了Kotlin编写:
1. 首先,我们需要定义一个数据类来存储相册中的图片信息:
```kotlin
data class Photo(val id: Long, val uri: Uri)
```
2. 接着,我们需要创建一个RecyclerView的Adapter来显示相册中的图片列表:
```kotlin
class PhotoAdapter(private val context: Context, private val photos: List<Photo>) :
RecyclerView.Adapter<PhotoAdapter.ViewHolder>() {
private val selectedPhotos = mutableListOf<Photo>()
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val imageView: ImageView = itemView.findViewById(R.id.imageView)
private val checkBox: CheckBox = itemView.findViewById(R.id.checkBox)
fun bind(photo: Photo) {
Glide.with(context)
.load(photo.uri)
.into(imageView)
checkBox.isChecked = selectedPhotos.contains(photo)
itemView.setOnClickListener {
if (selectedPhotos.contains(photo)) {
selectedPhotos.remove(photo)
checkBox.isChecked = false
} else {
selectedPhotos.add(photo)
checkBox.isChecked = true
}
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view =
LayoutInflater.from(parent.context).inflate(R.layout.item_photo, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(photos[position])
}
override fun getItemCount(): Int {
return photos.size
}
fun getSelectedPhotos(): List<Photo> {
return selectedPhotos
}
}
```
在这个Adapter中,我们通过Glide库加载图片,并且通过CheckBox来表示用户是否选择了该图片。同时,我们在ViewHolder中添加了一个点击事件监听器,用于在用户点击图片时更新选中状态。
3. 接下来,我们需要在MainActivity中使用这个Adapter来显示相册中的图片列表:
```kotlin
class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var adapter: PhotoAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = GridLayoutManager(this, 3)
val photos = getPhotos()
adapter = PhotoAdapter(this, photos)
recyclerView.adapter = adapter
val deleteButton: Button = findViewById(R.id.deleteButton)
deleteButton.setOnClickListener {
val selectedPhotos = adapter.getSelectedPhotos()
for (photo in selectedPhotos) {
contentResolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"${MediaStore.Images.Media._ID} = ?", arrayOf(photo.id.toString()))
}
adapter.notifyDataSetChanged()
}
}
private fun getPhotos(): List<Photo> {
val photos = mutableListOf<Photo>()
val projection = arrayOf(MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA)
val cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, null, null, "${MediaStore.Images.Media.DATE_ADDED} DESC")
cursor?.use {
val idColumn = it.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
val uriColumn = it.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
while (it.moveToNext()) {
val id = it.getLong(idColumn)
val uri = Uri.parse(it.getString(uriColumn))
photos.add(Photo(id, uri))
}
}
return photos
}
}
```
在这个MainActivity中,我们使用ContentResolver从相册中获取图片列表,并且将其传递给PhotoAdapter来显示。我们还在删除按钮上添加了一个点击事件监听器,以便在用户点击该按钮时删除选中的图片。
这样,我们就完成了一个RecyclerView类似相册支持多选删除的例子。当用户在相册中选中图片后,点击删除按钮即可删除选中的图片。
阅读全文