覆盖 ArrayAdapter 中的 getDropDownView 以更改下拉列表中特定行的颜色,同时更改另一行的颜色

Override of getDropDownView in ArrayAdapter in order to change color of particular row in dropdown also change color of another rows

我想更改微调器下拉列表中第一项的颜色。 解决方案之一是覆盖 getDropDownView 方法:

ArrayAdapter<CharSequence> sprache_ratoromanisch_adapter =
            new ArrayAdapter<CharSequence>(
                    getContext(),
                    android.R.layout.simple_spinner_item,
                    list_sprache_ratoromanisch) {

                @Override
                public View getDropDownView (int position, View convertView, ViewGroup parent){
                    View row = super.getDropDownView(position, convertView, parent);
                    if(position == 0) {
                       row.setBackgroundColor(Color.LTGRAY);
                    }
                    return(row);
                }
            };

但我有一个问题 - 如果元素的数量足够多并且它们超出了微调器的可见部分 - 滚动前不可见的第一和第二个元素在它们变得可见时也会改变颜色。 items in spinner's dropdown change color

我发现了问题。这是正确的版本。

final ArrayAdapter<CharSequence> sprache_ratoromanisch_adapter =
            new ArrayAdapter<CharSequence>(
                    getContext(),
                    android.R.layout.simple_spinner_item,
                    list_sprache_ratoromanisch) {

                @Override
                public View getDropDownView (int position, View convertView, ViewGroup parent){
                    View row = null;

                    if(position == 0) {
                        row = super.getDropDownView(position, convertView, parent);
                        row.setBackgroundColor(Color.LTGRAY);
                     }
                    else {
                        row = super.getDropDownView(position, null, parent);
                    }
                    return row;
                }
            };