简体   繁体   中英

Android spinner highlight selected item

I've got basic android spinner and I would like to have, after clicking it, the list with items with one of them highlighted, the one, that was originally selected.

Like it's done here: http://www.warriorpoint.com/blog/wp-content/uploads/2009/05/05spinner-thumb.png

But with my own layout of the items and not with the radio box, but with my own background instead.

How can I achieve this? Is there anything of use in the selector, or do I have to do it programatically?

Any help is appreciated.

Below is an solution I tested and verified. You can use setSelection(N) to highlight the item you want.

class HighLightArrayAdapter extends ArrayAdapter<CharSequence> {

        private int mSelectedIndex = -1;


        public void setSelection(int position) {
            mSelectedIndex =  position;
            notifyDataSetChanged();
        }

        public HighLightArrayAdapter(Context context, int resource, CharSequence[] objects) {
            super(context, resource, objects);
        }


        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            View itemView =  super.getDropDownView(position, convertView, parent);

            if (position == mSelectedIndex) {
                itemView.setBackgroundColor(Color.rgb(56,184,226));
            } else {
                itemView.setBackgroundColor(Color.TRANSPARENT);
            }

            return itemView;
        }
    }
public class MainActivity extends Activity {

Spinner mySpinner;
String[] myArray={"1","2","3","4","5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mySpinner = (Spinner)findViewById(R.id.spinner_test);

    mySpinner.setAdapter(new MyAdapter(this,android.R.layout.simple_spinner_item, myArray));

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public class MyAdapter extends ArrayAdapter<String>{

    public MyAdapter(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getDropDownView(int position, View cnvtView, ViewGroup prnt){
        LayoutInflater inflater = getLayoutInflater();
        View spinnerItem = inflater.inflate(android.R.layout.simple_spinner_item, null);

        TextView mytext= (TextView)spinnerItem.findViewById(android.R.id.text1);
        mytext.setText(myArray[position]);

        //int selected = Spinner.
        int selected = mySpinner.getSelectedItemPosition();
        if(position == selected){
            spinnerItem.setBackgroundColor(Color.BLUE);
        }
        return spinnerItem;

    }

}

}

This should help.

The Kotlin version of this is more straighforward:

    val spinner = Spinner(context)
    val adapter = object: ArrayAdapter<Any>(context, android.R.layout.simple_spinner_item, choices.toTypedArray()) {
        override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
            return super.getDropDownView(position, convertView, parent).also { view ->
                if(position == spinner.selectedItemPosition)
                    view.setBackgroundColor(Color.rgb(204, 255, 255))
            }
        }
    }
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item
    spinner.adapter = adapter

You can obviously set other properties of the view - the view returned by android.R.layout.simple_spinner_dropdown_item is a subclass of TextView so you can set text alignment and similar properties, eg to right-justify the text:

                (view as? TextView)?.textAlignment = TEXT_ALIGNMENT_VIEW_END

To answer my question, you need to have something like this:

public class mySpinnerAdapter extends SimpleCursorAdapter implements SpinnerAdapter {

public OrderSpinnerAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
    // TODO whatever you need
}

public View getDropDownView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(android.R.layout.simple_dropdown_item_1line, parent, false);
    }

    // TODO set the background color of the convertView
    // depending on your wishes

    return convertView;
}
}

So that we can control creating of the dropdown list like this. If you need different selectors, you can do it easily in the XML file.

Then, when you just simply create the adapters and bind them to the spinner with method setAdapter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM