Spinner with a different look for each item.

I am trying to create a Spinner where the first item in dropdownview does not have a switch. I am redefining the types of arrayadapter types to make sure that there are two different types in the spinner and that the views are not returned for the wrong element. Then I override getDropDownView from the array to remove the switch from the first element. This works, but the selected item does not show a switch. I think I did not need to install CheckMarkDrawable (android.R.drawable.btn_radio), because it should always be there, but it is not. Any ideas? Thanks!

@Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int position) { if (position == 0) return 0; else return 1; } @Override public View getDropDownView(int position, View convertView, android.view.ViewGroup parent) { if (position == 0) { View vw = super.getDropDownView(position, convertView, parent); CheckedTextView tv = (CheckedTextView) vw; if (tv != null) { tv.setCheckMarkDrawable(null); tv.setTextColor(Color.GRAY); return tv; } return vw; } else { View vw = super.getDropDownView(position, convertView, parent); CheckedTextView tv = (CheckedTextView) vw; if (tv != null) { tv.setCheckMarkDrawable(android.R.drawable.btn_radio); tv.setTextColor(Color.BLACK); return tv; } return vw; } } 
+4
source share
3 answers

I have the same problem. And YES, as pzagor2 said - Spinner does not support multiple recycling for the drop-down list (getDropDownView). It just does not call getViewTypeCount and getItemViewType. Here is the problem - # 17128 .

But he still calls these methods on getView, which is used to display the current spinner element and determine the width of the counter.

Workaround: You can simply check if the supplied convertView has the correct type and not use it if it is not. This will help in some cases, for example, when you have two types, and one type is used less often than the other.

+3
source

I think getItemViewType and getViewTypeCount are not called when using ArrayAdapter with Spinner. One solution is to not convert convertView and assume that it is always zero. But at LogCat you get camps and lots of GC calls.

0
source

Because link browsing does not work for Spinner, do not use Spinner if you have multiple views. Instead, use a TextView created as a Spinner, and in onClick your user dialog will open showing a drop down menu.

 <TextView android:id="@+id/labels_spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" style="@style/Base.Widget.AppCompat.Spinner.Underlined"/> @OnClick(R.id.labels_spinner) public void onSpinnerClick(View view) { // Open dropdown dialog } 
0
source

Source: https://habr.com/ru/post/1416012/


All Articles