开发一个Android自定义库,其中使用网格视图和framelayout作为gridview项(用于选择器的前景属性)。 everthing工作得很好,但滚动后它没有画出它的选择状态..
这是我的获取视图方法
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final GalleryHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.gridview_child_gallery_item, parent, false);
holder = new GalleryHolder();
holder.folderThumbnail = (RecyclingImageView) convertView.findViewById(R.id.iv_thumbnail);
holder.baseLayout = (GalleryFrameLayout) convertView.findViewById(R.id.baseItemLayout);
convertView.setTag(holder);
} else
holder = (GalleryHolder) convertView.getTag();
if(mSelections.contains(String.valueOf(position))){
holder.baseLayout.setSelected(true);
}
else{
holder.baseLayout.setSelected(false)
}
if(getGallerytype().equals(GallerySwitcherFragment.IMAGES))
loadBitmap(GallerySwitcherFragment.IMAGES,
Long.valueOf(mFiles.get(position).getThumbnailData()), holder.folderThumbnail);
else
loadBitmap(GallerySwitcherFragment.VIDEOS,
Long.valueOf(mFiles.get(position).getThumbnailData()),holder.folderThumbnail);
holder.baseLayout.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
setMultiSelectionEnabled(true);
holder.baseLayout.setChecked(true);
mController.toggleActionBarItems(true);
return true;
}
});
holder.baseLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(isMultiSelectionEnabled){
if(mSelections.contains(String.valueOf(position)))
mSelections.remove(String.valueOf(position));
else
mSelections.add(String.valueOf(position));
}
else
return;
}
});
return convertView;
}
这是我的xml
<?xml version="1.0" encoding="utf-8"?>
<com.sams.customcomponents.GalleryFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="119dp"
android:descendantFocusability="blocksDescendants"
android:id="@+id/baseItemLayout"
android:addStatesFromChildren="true"
android:foreground="@drawable/gridview_selector" >
<com.sams.customcomponents.RecyclingImageView android:id="@+id/iv_thumbnail"
android:contentDescription="@string/iv_content_description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@color/transparent_black" />
</com.sams.customcomponents.GalleryFrameLayout>
这是我的选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@color/selector_blue"/> <!-- pressed state -->
<item android:state_focused="true"
android:drawable="@color/selector_blue"/> <!-- focused state -->
<item android:state_selected="true"
android:drawable="@color/selector_blue"/> <!-- selected state -->
<item android:drawable="@android:color/transparent"/> <!-- default state -->
</selector>