全部,
我有一个覆盖 OnCreateDrawableState()
的 ViewGroup 子类(Xamarin.Android 在 C# 中,所以请原谅 Pascal 外壳)。
然而,我对 OnCreateDrawableState()
的覆盖从未被调用。我试过调用 RefreshDrawableState()
、DrawableStateChanged()
。 RequestLayout()
和 Invalidate()
。
似乎没有任何效果。这是方法:
/// <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;
}
我认为它会正常工作 - 但它永远不会被调用。 ViewGroup 本身嵌套在 ListView
和/或 LinearLayout
中,但似乎没有任何帮助。
这related question没有适合我的答案。
最佳答案
编辑:(我编辑了我之前的答案错误的答案,假设您正在实现 Checkable 接口(interface))
OnCreateDrawableState 似乎仅当子元素(叶)在 Textview 中声明 android:duplicateParentState
时才从叶节点传播到它的父节点(ViewGroups)。据我了解,如果 android:duplicateParentState="true"
已设置,RefreshDrawableState 会下降到树叶,而 OnCreateDrawableState 会从树叶上升,不过我还没有做彻底的分析,会对任何好的东西感兴趣指向文档的指针。
以下布局作为 ListView 中的一个项目将导致调用 MyLinearLayout 上的 onCreateDrawableState:
<com.example.android.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...>
<TextView
...
android:duplicateParentState="true"/>
</com.example.android.MyLinearLayout>
这不会:
<com.example.android.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...>
<TextView
...
android:duplicateParentState="false"/>
</com.example.android.MyLinearLayout>
这也不会(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>
这又将:
<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>
请注意,Checkable 接口(interface)的实现似乎有些混杂。
Checkable 接口(interface)可以由 ListView、GridView 等中的直接 subview 实现。无需在 View 树的下方进一步实现它。
在这种情况下,处于选择模式的 ListView 将在 Checkable 接口(interface)上调用 setChecked:
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));
}
Checkable 的实现通常通过覆盖 OnCreateDrawableState 添加一个可绘制状态。有关此示例,请参阅我的另一篇文章: MvxListView checkable list item
关于c# - onCreateDrawableState 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18596795/