【发布时间】:2020-10-01 06:01:26
【问题描述】:
我想显示一定数量的带有数字的图像,这些图像取决于列表。该列表最多包含 4 个整数,例如:(a) [4,2,2] 或 (b) [] 或 (c) [3,1] 等。 (a) 示例应显示 3 个图像,其中里面的相应数字(4 和 2 和 2),(b)示例应仅显示文本“none”,(c)示例应显示 2 个图像,其中分别具有数字 3 和 1。
我采用的方法是使用 4 个带有背景图像(菱形)的文本视图,根据列表的大小,我将 View.VISIBLE 的可见性更改为 View.GONE,并将文本更改为相应的 int。
以下屏幕截图显示了行布局文件(因为它将在回收站视图中查看)
这个屏幕截图显示了一个“真实”的例子。
我使用的代码包含很多样板,我很确定比我更聪明的人会有更好的解决方案。所以我的问题是,我该如何改进它以使其更短/更高效? 这是适配器中使用的代码:
private fun setDecoIcons(slots: List<Slot>) {
when(slots.size) {
0 -> {
binding.textViewRowArmorListItemNumberOfDecorationsDeco1.visibility = View.VISIBLE
binding.textViewRowArmorListItemNumberOfDecorationsDeco2.visibility = View.GONE
binding.textViewRowArmorListItemNumberOfDecorationsDeco3.visibility = View.GONE
binding.textViewRowArmorListItemNumberOfDecorationsDeco4.visibility = View.GONE
binding.textViewRowArmorListItemNumberOfDecorationsDeco1.setBackgroundResource(0)
binding.textViewRowArmorListItemNumberOfDecorationsDeco1.setText(R.string.textView_rowArmorListItem_deco_none)
}
1 -> {
binding.textViewRowArmorListItemNumberOfDecorationsDeco1.visibility = View.VISIBLE
binding.textViewRowArmorListItemNumberOfDecorationsDeco2.visibility = View.GONE
binding.textViewRowArmorListItemNumberOfDecorationsDeco3.visibility = View.GONE
binding.textViewRowArmorListItemNumberOfDecorationsDeco4.visibility = View.GONE
binding.textViewRowArmorListItemNumberOfDecorationsDeco1.setBackgroundResource(R.drawable.ll_icon_deco)
binding.textViewRowArmorListItemNumberOfDecorationsDeco1.text = slots[0].slotRank.toString()
}
2 -> {
binding.textViewRowArmorListItemNumberOfDecorationsDeco1.visibility = View.VISIBLE
binding.textViewRowArmorListItemNumberOfDecorationsDeco2.visibility = View.VISIBLE
binding.textViewRowArmorListItemNumberOfDecorationsDeco3.visibility = View.GONE
binding.textViewRowArmorListItemNumberOfDecorationsDeco4.visibility = View.GONE
binding.textViewRowArmorListItemNumberOfDecorationsDeco1.setBackgroundResource(R.drawable.ll_icon_deco)
binding.textViewRowArmorListItemNumberOfDecorationsDeco1.text = slots[0].slotRank.toString()
binding.textViewRowArmorListItemNumberOfDecorationsDeco2.text = slots[1].slotRank.toString()
}
3 -> {
binding.textViewRowArmorListItemNumberOfDecorationsDeco1.visibility = View.VISIBLE
binding.textViewRowArmorListItemNumberOfDecorationsDeco2.visibility = View.VISIBLE
binding.textViewRowArmorListItemNumberOfDecorationsDeco3.visibility = View.VISIBLE
binding.textViewRowArmorListItemNumberOfDecorationsDeco4.visibility = View.GONE
binding.textViewRowArmorListItemNumberOfDecorationsDeco1.setBackgroundResource(R.drawable.ll_icon_deco)
binding.textViewRowArmorListItemNumberOfDecorationsDeco1.text = slots[0].slotRank.toString()
binding.textViewRowArmorListItemNumberOfDecorationsDeco2.text = slots[1].slotRank.toString()
binding.textViewRowArmorListItemNumberOfDecorationsDeco3.text = slots[2].slotRank.toString()
}
4 -> {
binding.textViewRowArmorListItemNumberOfDecorationsDeco1.visibility = View.VISIBLE
binding.textViewRowArmorListItemNumberOfDecorationsDeco2.visibility = View.VISIBLE
binding.textViewRowArmorListItemNumberOfDecorationsDeco3.visibility = View.VISIBLE
binding.textViewRowArmorListItemNumberOfDecorationsDeco4.visibility = View.VISIBLE
binding.textViewRowArmorListItemNumberOfDecorationsDeco1.setBackgroundResource(R.drawable.ll_icon_deco)
binding.textViewRowArmorListItemNumberOfDecorationsDeco1.text = slots[0].slotRank.toString()
binding.textViewRowArmorListItemNumberOfDecorationsDeco2.text = slots[1].slotRank.toString()
binding.textViewRowArmorListItemNumberOfDecorationsDeco3.text = slots[2].slotRank.toString()
binding.textViewRowArmorListItemNumberOfDecorationsDeco4.text = slots[3].slotRank.toString()
}
}
}
【问题讨论】:
标签: android list kotlin textview