What is the state of a spinner when it's displ

2019-07-20 17:15发布

I am creating a spinner with a custom view, anyway I managed to show different drawables for when the spinner is inactive and also for when it's pressed, I would like to keep the pressed state drawable when the dropdown list shows. Here is mi XML file:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_focused="true"
      android:state_pressed="false"
      android:drawable="@drawable/ComboBoxInactive" />
  <item android:state_focused="true"
      android:state_pressed="true"
      android:drawable="@drawable/ComboBoxActive" />
  <item android:state_focused="false"
      android:state_pressed="true"
  android:drawable="@drawable/ComboBoxActive" />
  <item android:drawable="@drawable/ComboBoxInactive" />
</selector>

what state should I add for when it is displaying the dropdown? I want it to display ComboBoxActive drawable. I already tried adding this:

  <item android:state_enabled ="false"
        android:drawable="@drawable/ComboBoxActive"/>

Any idea of what the state is?

2条回答
别忘想泡老子
2楼-- · 2019-07-20 18:02

There is no android:state_dropdown_showing state. The only one state on spinner dropdown list is state_enabled="true"

You can use my selector to differentiate dropdown list state

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- disabled state -->
<item android:state_enabled="false" android:drawable="@drawable/spinner_off"/>
<!-- pressed state -->
<item android:state_enabled="true" android:state_window_focused="true" android:state_pressed="true" android:drawable="@drawable/spinner_pressed"/>
<!-- unselected state -->
<item android:state_enabled="true" android:state_window_focused="true" android:drawable="@drawable/spinner_default"/>
<!-- dropdown list state -->
<item android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/spinner_dropdown_list_is_shown"/>
<!-- default -->
<item android:drawable="@drawable/spinner_default"/>
</selector>

Don't forget to set setFocusable and setFocusableInTouchMode properties on spinner.

查看更多
▲ chillily
3楼-- · 2019-07-20 18:07

Based on Olef Koshkin answer I can add that if you want save changed state after click to spinner and return it to default only after close spinner you can use. It works for me.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- disabled state -->
    <item android:drawable="@drawable/custom_spinner_inactive"
          android:state_enabled="false"/>
    <!-- pressed state -->
    <item android:drawable="@drawable/custom_spinner_inactive"
          android:state_enabled="true"
          android:state_pressed="true"
          android:state_window_focused="true"/>
    <!-- unselected state -->
    <item android:drawable="@drawable/custom_spinner_inactive"
          android:state_enabled="true"
          android:state_window_focused="true"/>
    <!-- dropdown list state -->
    <item android:drawable="@drawable/custom_spinner_inactive"
          android:state_enabled="true"
          android:state_focused="true"/>
    <!-- default -->
    <item android:drawable="@drawable/custom_spinner_active"/>
</selector>

In other cases I see blink of default state and I didn't like it.

查看更多
登录 后发表回答