动态更改ListView项目大小

时间:2019-10-27 15:34:22

标签: android kotlin

我正在尝试以编程方式更改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!!
    }
}

1 个答案:

答案 0 :(得分:0)

您的问题的部分答案

height内的字段widthimageView是不可变的(val):

var imageView = v!!.findViewById(R.id.game_6_item_image) as ImageView

由于我不知道也看不到类/接口ImageView的实现,因此我无法说出如何实现更改。

相关问题