Android GridLayoutManager SpanSizeLookup dynamic set grid cell column count,Kotlin
import android.content.Context
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.imageview.ShapeableImageView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class MainActivity : AppCompatActivity() {
companion object {
const val TAG = "fly"
const val VIEW_TYPE = 0
const val GROUP_TYPE = 1
const val IMAGE_SIZE = 120
const val SPAN_COUNT = 9
const val ONE_CELL = 1
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rv = findViewById<RecyclerView>(R.id.rv)
val layoutManager = GridLayoutManager(this, SPAN_COUNT)
layoutManager.orientation = LinearLayoutManager.VERTICAL
rv.layoutManager = layoutManager
val adapter = MyAdapter(this)
rv.adapter = adapter
rv.setHasFixedSize(true)
layoutManager.spanSizeLookup = object : SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
val p = position % SPAN_COUNT
return if (p == 0) {
//group,标题
SPAN_COUNT * ONE_CELL
} else {
//单个小格子
ONE_CELL
}
}
}
lifecycleScope.launch(Dispatchers.IO) {
val items = readAllImage(this@MainActivity)
withContext(Dispatchers.Main) {
adapter.dataChanged(items)
}
}
}
class MyAdapter : RecyclerView.Adapter<MyVH> {
private var items = arrayListOf<MyData>()
private var mContext: Context? = null
constructor(ctx: Context) {
this.mContext = ctx
}
fun dataChanged(items: ArrayList<MyData>) {
this.items = items
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyVH {
return when (viewType) {
GROUP_TYPE -> {
val view = LayoutInflater.from(mContext!!).inflate(android.R.layout.simple_list_item_1, parent, false)
view.setBackgroundColor(ContextCompat.getColor(mContext!!, android.R.color.holo_orange_light))
MyVH(view)
}
else -> {
val view = MyImageView(mContext!!)
MyVH(view)
}
}
}
override fun getItemCount(): Int {
return items.size
}
override fun getItemViewType(position: Int): Int {
return if (position % SPAN_COUNT == 0) {
GROUP_TYPE
} else {
VIEW_TYPE
}
}
override fun onBindViewHolder(holder: MyVH, position: Int) {
Log.d(TAG, "onBindViewHolder $position")
when (getItemViewType(position)) {
GROUP_TYPE -> {
holder.itemView.findViewById<TextView>(android.R.id.text1).text = "$position GROUP"
}
else -> {
val uri = items[holder.adapterPosition].path
GlideApp.with(mContext!!)
.asBitmap()
.load(uri)
.centerCrop()
.override(IMAGE_SIZE, IMAGE_SIZE)
.into(holder.itemView as MyImageView)
}
}
}
}
class MyVH : RecyclerView.ViewHolder {
constructor(itemView: View) : super(itemView) {
}
}
class MyImageView : ShapeableImageView {
constructor(ctx: Context) : super(ctx) {
this.strokeColor = ContextCompat.getColorStateList(ctx, android.R.color.holo_red_light)
this.strokeWidth = 5f
}
}
class MyData(var path: String, var index: Int)
private fun readAllImage(context: Context): ArrayList<MyData> {
val photos = ArrayList<MyData>()
//读取所有图片
val cursor = context.contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null
)
var index = 0
while (cursor!!.moveToNext()) {
//路径 uri
val path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA))
//图片名称
//val name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME))
//图片大小
//val size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE))
photos.add(MyData(path, index++))
}
cursor.close()
return photos
}
}
Android RecyclerView性能优化及Glide流畅加载图片丢帧率低的一种8宫格实现,Kotlin-CSDN博客文章浏览阅读407次,点赞20次,收藏9次。【代码】Android Paging 3,kotlin(1)在实际的开发中,虽然Glide解决了快速加载图片的问题,但还有一个问题悬而未决:比如用户的头像,往往用户的头像是从服务器端读出的一个普通矩形图片,但是现在的设计一般要求在APP端的用户头像显示成圆形头像,那么此时虽然Glide可以加载,但加载出来的是一个矩形,如果要Glide_android 毛玻璃圆角。现在结合他人的代码加以修改,给出一个以原始图形中心为原点,修剪图片为头像的工具类,此类可以直接在布局文件中加载使用,比。文章浏览阅读670次。https://blog.csdn.net/zhangphil/article/details/137653692Android性能优化RecyclerView预加载LayoutManager的getExtraLayoutSpace,Kotlin-CSDN博客文章浏览阅读1k次,点赞29次,收藏16次。文章浏览阅读501次。文章浏览阅读428次。上面要预加载10条,每条item高度是100pix,也就是说,正确的情况下,如果RecyclerView不作任何调优,那它只加载当前屏幕可见区域position为0~21的item(每个item高度为100pix),如果配置了getExtraLayoutSpace,那么会多(Extra)加载10条position为22~31的item,其中22~31为屏幕底部不可见的区域中内容,被“预加载”出来。文章浏览阅读410次。
https://blog.csdn.net/zhangphil/article/details/137589193