gpt4 book ai didi

android - Android中选择、检查和激活的状态有什么区别?

转载 作者:IT老高 更新时间:2023-10-28 13:04:32 26 4
gpt4 key购买 nike

我想知道这些州有什么不同。我没有找到任何网页来说明这一点。

最佳答案

Checked 和 Activated 的区别其实很有趣。甚至谷歌文档也很抱歉(在下面添加了重点):

... For example, in a list view with single or multiple selection enabled, the views in the current selection set are activated. (Um, yeah, we are deeply sorry about the terminology here.) The activated state is propagated down to children of the view it is set on.

所以这里有区别:

  1. 在 Honeycomb 中引入了Activated,因此在此之前您无法使用它
  2. Activated 现在是每个 View 的属性。它有方法 setActivated() 和 isActivated()
  3. Activated 传播到设置它的 View 的子级
  4. Checked 围绕实现 Checkable 接口(interface)的 View 展开。方法 setChecked()、isChecked()、toggle()
  5. ListView(在 Honeycomb 之后)调用 setChecked() 或 setActivated() 取决于 Android 版本如下(取自 Android 源代码):

    if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) {
    if (child instanceof Checkable) {
    ((Checkable) child).setChecked(mCheckStates.get(position));
    } else if (getContext().getApplicationInfo().targetSdkVersion
    >= android.os.Build.VERSION_CODES.HONEYCOMB) {
    child.setActivated(mCheckStates.get(position));
    }
    }

    注意 mCheckStates 变量。它会跟踪您列表中的哪些位置被选中/激活。这些可以通过例如 getCheckedItemPositions() 访问。另请注意,对 ListView.setItemChecked() 的调用会调用上述内容。换句话说,它同样可以被称为 setItemActivated()。

  6. 在 Honeycomb 之前,我们必须实现变通方法以在列表项中反射(reflect) state_checked。这是因为 ListView 仅在布局中的最顶层 View 上调用 setChecked() (并且布局不实现可检查)......并且它不会在没有帮助的情况下传播。这些变通方法具有以下形式: 扩展根布局以实现 Checkable。在其构造函数中,递归查找所有实现 Checkable 的子代。当 setChecked() 等被调用时,将调用传递给这些 View 。如果这些 View 使用不同的 state_checked 可绘制对象实现状态列表可绘制对象(例如 CheckBox),则选中状态会反射(reflect)在 UI 中。

  7. 要在 Honeycomb 之后为列表项做一个漂亮的背景,你需要做的就是有一个可绘制的状态列表,其中 state_activated 的可绘制状态如下所示(当然还要使用 setItemChecked()):

    <item android:state_pressed="true"
    android:drawable="@drawable/list_item_bg_pressed"/>
    <item android:state_activated="true"
    android:drawable="@drawable/list_item_bg_activated"/>
    <item android:drawable="@drawable/list_item_bg_normal"/>

  8. 要在 HoneyComb 之前为列表项做一个漂亮的背景,你可以为 state_checked 做类似上面的事情,你还需要扩展你的顶层 View 来实现 Checkable 接口(interface)。然后,您需要通过实现 onCreateDrawableState() 并在状态更改时调用 refreshDrawableState() 来告诉 Android 您正在实现的状态是真还是假。

    <item android:state_pressed="true"
    android:drawable="@drawable/list_item_bg_pressed"/>
    <item android:state_checked="true"
    android:drawable="@drawable/list_item_bg_checked"/>
    <item android:drawable="@drawable/list_item_bg_normal"/>

...在RelativeLayout中结合state_checked实现Checkable的代码可能是:

public class RelativeLayoutCheckable extends RelativeLayout implements Checkable {

public RelativeLayoutCheckable(Context context, AttributeSet attrs) {
super(context, attrs);
}

public RelativeLayoutCheckable(Context context) {
super(context);
}

private boolean mChecked = false;

@Override
protected void onFinishInflate() {
super.onFinishInflate();
}
@Override
public boolean isChecked() {
return mChecked;
}

@Override
public void setChecked(boolean checked) {
mChecked = checked;
refreshDrawableState();
}

private static final int[] mCheckedStateSet = {
android.R.attr.state_checked,
};

@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, mCheckedStateSet);
}
return drawableState;
}

@Override
public void toggle() {
setChecked(!mChecked);
}
}

感谢以下人员:

http://sriramramani.wordpress.com/2012/11/17/custom-states/

堆栈溢出:How to add a custom button state

堆栈溢出:Custom Checkable View which responds to Selector

http://www.charlesharley.com/2012/programming/custom-drawable-states-in-android/

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

http://blog.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/

关于android - Android中选择、检查和激活的状态有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11504860/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com