简体   繁体   English

从不调用onCreateDrawableState

[英]onCreateDrawableState Never Called

All, 所有,

I've got a ViewGroup subclass which overrides OnCreateDrawableState() (Xamarin.Android is in C# so forgive the Pascal Casing). 我有一个重写OnCreateDrawableState()的ViewGroup子类(Xamarin.Android在C#中,因此请原谅Pascal外壳)。

My override of OnCreateDrawableState() never gets called, however. 但是,我对OnCreateDrawableState()覆盖从未被调用。 I've tried calling RefreshDrawableState() , DrawableStateChanged() . 我尝试调用RefreshDrawableState()DrawableStateChanged() RequestLayout() , and Invalidate() . RequestLayout()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. ViewGroup本身嵌套在ListView和/或LinearLayout但似乎无济于事。

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) 编辑:(假设您正在实现Checkable接口,则我编辑了先前的答案不正确的答案)

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. 仅当子元素(叶)在Textview中声明了android:duplicateParentState ,OnCreateDrawableState才似乎从叶节点传播到其父节点(ViewGroups)。 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. 据我了解,如果设置了android:duplicateParentState="true" ,那么RefreshDrawableState会从页面开始,而OnCreateDrawableState会从页面开始,但是我没有做过详尽的分析,会对任何指向文档的好的指针感兴趣。

The following layout as an item in a ListView will cause onCreateDrawableState on MyLinearLayout to be called: 以下布局作为ListView中的一项,将导致MyLinearLayout上的onCreateDrawableState被调用:

<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): 这也不会(LinearLayout不会向上传播):

<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. 请注意,Checkable接口的实现似乎有些汞齐。

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. Checkable接口可以由ListView,GridView等中的直接子级视图实现。不需要在视图树下进一步实现它。

In this case the ListView when in choice mode will call setChecked on the Checkable interface: 在这种情况下,处于选择模式的ListView将在Checkable接口上调用setChecked:

http://androidxref.com/4.3_r2.1/xref/frameworks/base/core/java/android/widget/ListView.java#1899 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. Checkable的实现通常通过覆盖OnCreateDrawableState添加可绘制状态。 See my other post for an example of this: MvxListView checkable list item 有关此示例,请参见我的其他文章: MvxListView可检查列表项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM