我正在尝试以编程方式更改ListView项的高度和宽度,但出现“无法重新分配val的错误”,能否请您告诉我,此处无法重新分配确切的val,我如何最终设置List的高度和宽度是我的自定义适配器吗?
class Game6Adapter(
private val mContext: Context,
private val resourceLayout: Int,
private val items: ArrayList<Drawable>
) : ArrayAdapter<Drawable>(mContext, resourceLayout, items) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var v: View? = convertView
var vi: LayoutInflater = LayoutInflater.from(mContext)
v = vi.inflate(resourceLayout, null)
val p = getItem(position)
var imageView = v!!.findViewById(R.id.game_6_item_image) as ImageView
imageView.setImageDrawable(items[position])
var layoutParams = parent.layoutParams
imageView.height = (parent.height * 0.1).toInt() // val cannot be reassigned
imageView.width = (parent.height * 0.1).toInt() val cannot be reassigned
imageView.requestLayout()
return v!!
}
}
答案 0 :(得分:0)
您的问题的部分答案
height
内的字段width
和imageView
是不可变的(val
):
var imageView = v!!.findViewById(R.id.game_6_item_image) as ImageView
由于我不知道也看不到类/接口ImageView
的实现,因此我无法说出如何实现更改。