【问题标题】:How to display images with a number inside according to a List如何根据列表显示带有数字的图像
【发布时间】: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


    【解决方案1】:
        Use Databinding
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    
    <data>
        <variable
            name="scoreVM"
            type="yourPackage.ui.main.ScoreViewModel" />
    </data>
    
            
    

    你的图片+分数:

    <androidx.coordinatorlayout.widget.CoordinatorLayout
                android:id="@+id/highScoreFab"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_marginBottom="24dp"
                android:background="@drawable/high_score"
                android:clickable="true"
                android:focusable="true"
                android:foregroundGravity="center"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:elevation="6dp"
                    android:fontFamily="sans-serif-black"
                    android:text="@={scoreVM.highCount}"
                    android:textColor="@color/design_default_color_error"
                    android:textSize="18sp" />
    

    视图模型:

    var highCount = MutableLiveData<Int>()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-22
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多