how to create an unselectable hint text for Spinne

2019-09-12 05:37发布

问题:

I have a spinner with drop down mode . I display my list of items with my custom adapter in that. But now i want to use a hint text for my spinner that is unselectable. How can i do this ? thanks in advance

回答1:

You have to add first item like i did in my code then on click of spinner it will hide so user will not able to select that first item

ArrayList cityArraList = new ArrayList();
cityArrayList.add("Select City"); 
Spinner citySpinner = (Spinner) findViewById(R.id.citySpinner);
        final ArrayAdapter<City> cityAdapter = new ArrayAdapter<City>(getActivity(),R.layout.my_simple_list_item_1,cityArrayList){
            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                View v = null;

                // If this is the initial dummy entry, make it hidden
                if (position == 0) {
                    TextView tv = new TextView(getContext());
                    tv.setHeight(0);
                    tv.setVisibility(View.GONE);
                    v = tv;
                } else {
                    // Pass convertView as null to prevent reuse of special case views
                    v = super.getDropDownView(position, null, parent);
                }

                // Hide scroll bar because it appears sometimes unnecessarily, this does not prevent scrolling
                //parent.setVerticalScrollBarEnabled(false);
                return v;
            }
        };