i have StateListdrawable
in xml
<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>
我有GridView
具有StateListDrawable
的图像,并以编程方式尝试更改可绘制状态
if(mSelected.contains(photo)){
view.findViewById(R.id.selector).setActivated(true);
}
选择的图像更改状态,但是当我单击他的状态不变时,对不起,我无法解释我想要什么,我的英语更差
我试着解释一下
如果我点击时mSelected.contains(photo) [state - active] -> [checked_on.jpg]
->状态可绘制对象从第一个开始,忽略我的编程状态更改
编辑==
适配器项目 --
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;
}
问题是状态的顺序。它们从上到下检查,并使用第一个符合情况的。因此,这意味着始终使用前两个。这应该有效:
<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>