Android spinner highlight selected item

 ̄綄美尐妖づ 提交于 2019-12-13 13:13:12

问题


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.


回答1:


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;
        }
    }



回答2:


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.




回答3:


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, e.g. to right-justify the text:

                (view as? TextView)?.textAlignment = TEXT_ALIGNMENT_VIEW_END



回答4:


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.



来源:https://stackoverflow.com/questions/12406486/android-spinner-highlight-selected-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!