简体   繁体   中英

Spinner won't highlight when item is selected

By default, when you select an item in spinner, it highlights briefly before disappearing.

I changed the color of my spinner rows to alternate color with the following code, and the highlight disappears. R.layout.textviewinside and R.layout.textview don't cause this, just the @Override for getDropDownView, because everything works if I comment out that block.

How can I restore that functionality but keep the row colors?

products = new ArrayAdapter<String>(this,   R.layout.textview, thedata){
                @Override
                public View getDropDownView(int position, View convertView, ViewGroup parent) {
                    View v = super.getDropDownView(position, convertView, parent);
                    if (position % 2 == 0) { // we're on an even row
                        v.setBackgroundColor(0xffEBF4FA);//Color.BLUE)
                    } else {
                        v.setBackgroundColor(Color.WHITE);
                    }
                    ((TextView) v).setGravity(Gravity.CENTER);

                    return v;
                }
            };
            products.setDropDownViewResource(R.layout.textviewinside);

            spitem.setAdapter(products);

Instead of using setBackgroundColor, you'll need to use setBackgroundDrawable, and use an xml state list drawable file with pressed/default states.

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

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