简体   繁体   中英

Custom spinner adapter's getDropDownView() acting inconsistently

I have created a custom spinner adapter to apply a custom view to both the selected item view and drop down views of a spinner I have. The layout files for these are as such.

The selected item view

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerItem"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:textAppearance="@style/StandardText"
android:gravity="center"/>

The drop down views

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/firingcycleSpinnerItem"
style="?android:attr/spinnerDropDownItemStyle"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:gravity="center">

<CheckedTextView
    android:id="@+id/firingcycleName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="@style/SubtitleText"
    android:layout_gravity="center_vertical|left"
    />

<TextView
    android:id="@+id/firingcycleSubtitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="@style/SmallText"
    android:layout_gravity="center_vertical|left"
    android:layout_marginTop="8dp"
    />

</LinearLayout>

Within the getDropDownView() method, I am attempting to style the first drop down item differently using the code below.

if (position == 0) {
        name.setTextColor(name.getHintTextColors());
        subtitle.setVisibility(GONE)
}

However, for some reason, in addition to styling the first element, the currently selected item's drop down view is also styled this same way, as if this code ran for it.

I have inserted breakpoints, and found that getDropDownView() is run to generate every element in the spinner upon opening it. The section of code provided above is indeed only run one time - when creating the first element.

What confuses me is that this problem is only with the selected item's drop down view . I also apply a custom style to the first element in getView() , which creates the selected item view of the spinner. While I use a different layout file for this view, I run the exact same check as the code above, and this styling works perfectly. If the first element is selected, it's view is styled. If any other element is selected, it's view is totally fine.

I have tried applying different colors, using a variety different checks to identify the first element, and switching the layouts. The selected item view still works perfectly, and the drop down items still have problems.

If I remove this code altogether from getDropDownView() , each element - including the selected one - appears the same, so there is no other code running somewhere modifying the selected item's drop down view. I have no idea what the problem could be at this point.

Its because views are recycled, so the in the drop down view, the first view gets modified like its supposed to but then if it is closed and reopened it could use that same view in another spot. to deal with this you cant just change the view if its on the first spot you need to also change it back if it is not in the first spot ex:

if (position == 0) {
    name.setTextColor(name.getHintTextColors());
    subtitle.setVisibility(GONE)
}
else {
    name.setTextColor(name.regularColor());
    subtitle.setVisibility(VISIBLE)
}

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