简体   繁体   中英

onCreateDrawableState Never Called

All,

I've got a ViewGroup subclass which overrides OnCreateDrawableState() (Xamarin.Android is in C# so forgive the Pascal Casing).

My override of OnCreateDrawableState() never gets called, however. I've tried calling RefreshDrawableState() , DrawableStateChanged() . RequestLayout() , and Invalidate() .

Nothing seems to work. This is the method:

/// <summary>
/// Handles the create drawable state event by adding in additional states as needed.
/// </summary>
/// <param name="extraSpace">Extra space.</param>
protected override int[] OnCreateDrawableState (int extraSpace)
{
    int[] drawableState = base.OnCreateDrawableState(extraSpace + 3);

    if (Completed)
    {
        int[] completedState = new int[] { Resource.Attribute.completed };
        MergeDrawableStates(drawableState, completedState);
    }

    if (Required)
    {
        int[] requiredState = new int[] { Resource.Attribute.required };
        MergeDrawableStates(drawableState, requiredState);
    }

    if (Valid)
    {
        int[] validState = new int[] { Resource.Attribute.valid };
        MergeDrawableStates(drawableState, validState);
    }

    Android.Util.Log.Debug("ROW_VIEW", "OnCreateDrawableState Called");

    return drawableState;
}

I assume it'll work OK - but it just never gets called. The ViewGroup itself is nested in a ListView and/or a LinearLayout but nothing seems to help.

This related question has no answers that work for me.

Edit: (I edited my previous answer incorrect answer where assumed you were implementing the Checkable interface)

OnCreateDrawableState seems to be propagated from the leaf nodes up to it's parent nodes (ViewGroups) only if a child element (leaf) declares android:duplicateParentState for instance in a Textview. As I understand it RefreshDrawableState goes down to the leaves and OnCreateDrawableState up from the leaves if android:duplicateParentState="true" is set, I haven't done a thorough analysis though, would be interested in any good pointers to documentation.

The following layout as an item in a ListView will cause onCreateDrawableState on MyLinearLayout to be called:

<com.example.android.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   ...>    
    <TextView
        ...
        android:duplicateParentState="true"/>    
</com.example.android.MyLinearLayout>

This won't:

<com.example.android.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   ...>    
    <TextView
        ...
        android:duplicateParentState="false"/>    
</com.example.android.MyLinearLayout>

Neither will this (The LinearLayout won't propagate up):

<com.example.android.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   ...>    
    <LinearLayout 
        ...
        android:duplicateParentState="false">
        <TextView
            ...
            android:duplicateParentState="true"/>    
    </LinearLayout>
</com.example.android.MyLinearLayout>

This again will:

<com.example.android.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   ...>    
    <LinearLayout 
        ...
        android:duplicateParentState="true">
        <TextView
            ...
            android:duplicateParentState="true"/>    
    </LinearLayout>
</com.example.android.MyLinearLayout>

Note that there seems to be some amalgam with the implementation of the Checkable interface.

The Checkable interface can be implemented by the Views that are immediate children in a ListView, GridView etc. There is no need to have it implemented further down the view tree.

In this case the ListView when in choice mode will call setChecked on the Checkable interface:

http://androidxref.com/4.3_r2.1/xref/frameworks/base/core/java/android/widget/ListView.java#1899

if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) {
            if (child instanceof Checkable) {
                ((Checkable) child).setChecked(mCheckStates.get(position));
            } 

The implementation of the Checkable usually adds a drawable state by overriding OnCreateDrawableState. See my other post for an example of this: MvxListView checkable list item

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