如何创建一个每行颜色不同的旋转器

huangapple go评论73阅读模式
英文:

How to create a spinner with different colors in each row

问题

我想在每一行中创建一个具有不同颜色的旋转器,我知道有很多类似于我问题的解释,但它们都是用Java编写的,对我来说很复杂,我已经执行了以下步骤。

我的代码:

val lista = listOf<Mood>(
    Mood(resources.getColor(R.color.blue, null), "Color1"),
    Mood(resources.getColor(R.color.purple, null), "Color2"),
    Mood(resources.getColor(R.color.green, null), "Color3"),
    Mood(resources.getColor(R.color.darkred, null), "Color4")
)

val adaptador = MoodArrayAdapter(this, lista)
spinner1.adapter = adaptador

spinner1.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
    override fun onItemSelected(p0: AdapterView<*>, p1: View?, p2: Int, p3: Long) {
        when (spinner1.selectedItem.toString()) {
            "Color1" -> textView.setBackgroundResource(R.color.blue)
            "Color2" -> textView.setBackgroundResource(R.color.purple)
            "Color3" -> textView.setBackgroundResource(R.color.green)
            "Color4" -> textView.setBackgroundResource(R.color.darkred)
        }
    }
    override fun onNothingSelected(p0: AdapterView<*>?) {
        TODO("Not yet implemented")
    }
}

我希望以这种方式创建我的旋转器:

如何创建一个每行颜色不同的旋转器

英文:

I would like to create a sppiner with different colors in each row, I know that there are many explanations similar to my question but they are all in Java and it has been complicated for me, I carry out the steps.

My code

val lista = listOf&lt;Mood&gt;(
    Mood(resources.getColor(R.color.blue, null), &quot;Color1&quot;),
    Mood(resources.getColor(R.color.purple, null), &quot;Color2&quot;),
    Mood(resources.getColor(R.color.green, null), &quot;Color3&quot;),
    Mood(resources.getColor(R.color.darkred, null), &quot;Color4&quot;)
)

val adaptador = MoodArrayAdapter(this, lista)
spinner1.adapter = adaptador

spinner1.onItemSelectedListener = object :
    AdapterView.OnItemSelectedListener {
    override fun onItemSelected(p0: AdapterView&lt;*&gt;?, p1: View?, p2: Int, p3: Long) {
        when (spinner1.selectedItem.toString()) {
            &quot;Color1&quot; -&gt; textView.setBackgroundResource(R.color.blue)
            &quot;Color2&quot; -&gt; textView.setBackgroundResource(R.color.purple)
            &quot;Color3&quot; -&gt; textView.setBackgroundResource(R.color.green)
            &quot;Color4&quot; -&gt; textView.setBackgroundResource(R.color.darkred)
        }
    }
    override fun onNothingSelected(p0: AdapterView&lt;*&gt;?) {
        TODO(&quot;Not yet implemented&quot;)
    }
}

I want to create my spinner in this way

如何创建一个每行颜色不同的旋转器

答案1

得分: 1

  1. 将数据模型更改为:
data class Mood(val backgroundColor: Color, 
    val description: String)
  1. 将项目布局更改为(虽然你不需要 ImageView,但对于单个 TextView 布局,甚至不需要 ConstraintLayout,不过我暂时保留它):
<android.support.constraint.ConstraintLayout
    android:id="@+id/rootLayout"
    ...>
    <TextView
        android:id="@+id/moodText"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"/>
</android.support.constraint.ConstraintLayout>
  1. Adapter 类更改为:
class MoodArrayAdapter(ctx: Context,
    moods: List<Mood>) :
    ArrayAdapter<Mood>(ctx, 0, moods) {
    override fun getView(position: Int, recycledView: View?, parent: ViewGroup): View {
        return this.createView(position, recycledView, parent)
    }
    override fun getDropDownView(position: Int, recycledView: View?, parent: ViewGroup): View {
        return this.createView(position, recycledView, parent)
    }
    private fun createView(position: Int, recycledView: View?, parent: ViewGroup): View {
        val mood = getItem(position)
        val view = recycledView ?: LayoutInflater.from(context).inflate(
            R.layout.demo_spinner,
            parent,
            false
        )
        view.rootLayout.setBackgroundColor(mood.backgroundColor)
        view.moodText.text = mood.description
        return view
    }
}
  1. 最后,将适配器设置给下拉框(spinner):
moodSpinner.adapter = MoodArrayAdapter(
    this,
    listOf(
        Mood(Color.RED, "Angry"),
        Mood(Color.GRAY, "Happy"),
        Mood(Color.CYAN, "Playful"),
        Mood(Color.GREEN, "Wondering")
    )
)

现在,你可以根据需要更改变量/名称,以适应你的情况。此外,我正在传递颜色,你可以使用 Color.ValueOf(r, g, b) 来使用自定义颜色,或者你可以将数据模型中的 backgroundColor 的数据类型更改为 int,然后从 colors.xml 中传递颜色资源。

编辑 -> 要访问颜色资源,请按照以下方式传递:

从 Activity -> Mood(resources.getColor(R.color.blue,null), "Angry")
从 Fragment -> Mood(context.resources.getColor(R.color.blue,null), "Angry")

因此,根据需要更改代码:

moodSpinner.adapter = MoodArrayAdapter(
    this,
    listOf(
        Mood(resources.getColor(R.color.blue,null), "Angry"),
        Mood(resources.getColor(R.color.red,null), "Happy"),
        Mood(Color.CYAN, "Playful"),
        Mood(Color.GREEN, "Wondering")
    )
)
英文:

You've not shared the complete code though, but here's what you actually have to do.

  1. Change the data model as :

    data class Mood(val backgroundColor: Color, 
        val description: String)
    
  2. Change the item layout as (You don't need the ImageView though, you don't even need the ConstraintLayout though for a single TextView layout, but I'm keeping it for now) :

    &lt;android.support.constraint.ConstraintLayout
        android:id=&quot;@+id/rootLayout&quot;
        ...&gt;
        &lt;TextView
            android:id=&quot;@+id/moodText&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;20dp&quot;
            app:layout_constraintTop_toTopOf=&quot;parent&quot;
            app:layout_constraintEnd_toEndOf=&quot;parent&quot;
            app:layout_constraintStart_toStartOf=&quot;parent&quot;
            app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
            android:layout_marginStart=&quot;8dp&quot;
            android:layout_marginEnd=&quot;8dp&quot;/&gt;
    &lt;/android.support.constraint.ConstraintLayout&gt;
    
  3. Change the Adapter class as :

    class MoodArrayAdapter(ctx: Context,
        moods: List&lt;Mood&gt;) :
        ArrayAdapter&lt;Mood&gt;(ctx, 0, moods) {
        override fun getView(position: Int, recycledView: View?, parent: ViewGroup): View {
            return this.createView(position, recycledView, parent)
        }
        override fun getDropDownView(position: Int, recycledView: View?, parent: ViewGroup): View {
            return this.createView(position, recycledView, parent)
        }
        private fun createView(position: Int, recycledView: View?, parent: ViewGroup): View {
            val mood = getItem(position)
            val view = recycledView ?: LayoutInflater.from(context).inflate(
                R.layout.demo_spinner,
                parent,
                false
            )
            view.rootLayout.setBackgroundColor(mood.backgroundColor)
            view.moodText.text = mood.description
            return view
        }
    }
    
  4. Finally, set the adapter to the spinner as :

    moodSpinner.adapter = MoodArrayAdapter(
        this,
        listOf(
            Mood(Color.RED, &quot;Angry&quot;),
            Mood(Color.GRAY, &quot;Happy&quot;),
            Mood(Color.CYAN, &quot;Playful&quot;),
            Mood(Color.GREEN, &quot;Wondering&quot;)
        )
    )
    

Now, you can change the variables/names where word "mood" is written as it suits you. Furthermore, I'm passing colors, you can use Color.ValueOf(r,g,b) for custom colors or you can change the DataType of backgroundColor in the data model to int and pass color resources from colors.xml.

Edit -> To access a color resource for this, pass it as :

From Activity -&gt; Mood(resources.getColor(R.color.blue,null), &quot;Angry&quot;)
From Fragment -&gt; Mood(context.resources.getColor(R.color.blue,null), &quot;Angry&quot;)

So, change your code accordingly as :

moodSpinner.adapter = MoodArrayAdapter(
    this,
    listOf(
    Mood(resources.getColor(R.color.blue,null), &quot;Angry&quot;),
    Mood(resources.getColor(R.color.red,null), &quot;Happy&quot;),
    Mood(Color.CYAN, &quot;Playful&quot;),
    Mood(Color.GREEN, &quot;Wondering&quot;)
    )
)

huangapple
  • 本文由 发表于 2020年9月13日 08:06:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63865981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定