Android StateListDrawable错误?

时间:2013-10-29 14:27:38

标签: android statelistdrawable

我在xml中有StateListdrawable

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_activated="false" android:drawable="@drawable/ic_checked_off" />
    <item android:state_activated="true" android:drawable="@drawable/ic_checked_on" />
    <item android:drawable="@drawable/ic_checked_on" android:state_pressed="true" />
    <item android:drawable="@drawable/ic_checked_off" />

</selector>

我有GridViewStateListDrawable张图片,并且以编程方式尝试使用

更改可绘制状态
    if(mSelected.contains(photo)){
        view.findViewById(R.id.selector).setActivated(true);
    }

选择改变状态的图像,但是当我点击他的状态不改变时,我很抱歉我无法解释我想要的东西,我的英语更糟糕

我试着解释一下

如果我点击时mSelected.contains(photo) [state - active] -> [checked_on.jpg] - &gt; state drawable从first开始,忽略了我的程序状态变化

==编辑==

适配器项目 -

private View getPhoto(int position, View convertView, ViewGroup parent){
        View view = convertView;
        if(convertView == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.gallery_photo_item, parent, false);
        }

        ImageView mPhoto = (ImageView)view.findViewById(R.id.photoview);

        Photo photo = (Photo) getItem(position - COLUMNS_NUM);

        if(mSelected.contains(photo)){
            view.findViewById(R.id.selector).setActivated(true); // here i change my drawable state because it's front of my imageView
        }

        mLoader.displayImage(MediaStore.Images.Media.EXTERNAL_CONTENT_URI + File.separator + photo.id,
                mPhoto);

        return view;
    }

1 个答案:

答案 0 :(得分:0)

问题在于各州的秩序。它们从上到下进行检查,并使用与该情况匹配的第一个。这意味着前两个总是被使用。这应该有效:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/ic_checked_on"  />
    <item android:state_activated="false" android:drawable="@drawable/ic_checked_off" />
    <item android:state_activated="true" android:drawable="@drawable/ic_checked_on" />
    <item android:drawable="@drawable/ic_checked_off" />
</selector>