我想添加两列交错的相册,但是当我将交错布局管理器设置为两列时我觉得很奇怪,就像这样:
图像:
我的适配器:
class AlbumAdapter: RecyclerView.Adapter<AlbumAdapter.VH>()
{
var list: ArrayList<Album>? = ArrayList<Album>()
fun addToList(addedList: List<Album>)
{
list?.addAll(addedList)
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
val inflater = parent.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view = inflater.inflate(R.layout.item_album, parent, false)
return VH(view)
}
override fun getItemCount(): Int = list?.size ?: 0
override fun onBindViewHolder(holder: VH, position: Int) {
val layoutParams = holder.itemView.getLayoutParams() as StaggeredGridLayoutManager.LayoutParams
//layoutParams.isMarginRelative = true
holder.bind(list!![position])
}
inner class VH(i: View): RecyclerView.ViewHolder(i) {
fun bind(model: Album)
{
Log.i("glide_flex", model.avatar)
Glide.with(App.appContext!!)
.asBitmap()
.load(model.avatar)
.into(object: SimpleTarget<Bitmap>()
{
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
Log.i("glide_flex", resource.density.toString())
(itemView as AppCompatImageView).setImageBitmap(resource)
val lp = itemView.layoutParams
if (lp is StaggeredGridLayoutManager.LayoutParams) {
Log.i("glide_flex", "LP")
// lp.flexGrow = 1.0f
lp.width = lp.width / 2
}
}
}
)
}
}
}