我在我的应用程序中使用StaggeredGridView并使用我创建的自定义选择器,但我的选择器必定会出现严重错误,因为当我在gridview中选择一个项目时,所有项目的颜色都会变为绿色。 / p>
以下是gridview项目的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/grid_item_selector">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ro.gebs.zonizbeacon.ui.custom.ScaledNetworkImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ro.gebs.zonizbeacon.ui.custom.AngledTextView
android:id="@+id/expired_tv"
android:layout_width="@dimen/size_favorite_angle_text"
android:layout_height="@dimen/size_favorite_angle_text"
app:angledText="@string/expired"
app:backgroundColor="@android:color/holo_orange_dark"
app:angledTextColor="@android:color/white"
app:angledTextSize="@dimen/font_size_plus" />
</RelativeLayout>
<ro.gebs.zonizbeacon.ui.custom.fonts.CustomFontTextView
android:id="@+id/store_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/default_padding"
android:textSize="@dimen/font_size_normal"
android:text="Zara"
app:fontName="Bold" />
<ro.gebs.zonizbeacon.ui.custom.fonts.CustomFontTextView
android:id="@+id/offer_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:fontName="Regular"
android:paddingLeft="@dimen/default_padding"
android:paddingRight="@dimen/default_padding"
android:paddingBottom="@dimen/default_padding"
android:textSize="@dimen/font_size_small"
android:text="@string/info_about" />
</LinearLayout>
这是我的选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="true" android:drawable="@color/green_main_color" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="@color/green_main_color" />
<item android:state_enabled="true" android:state_selected="true" android:drawable="@color/green_main_color" />
<item android:drawable="@android:color/white" />
</selector>
这是我的适配器类:
public class FavoritesAdapter extends ArrayAdapter<Offer> {
LayoutInflater mLayoutInflater;
public FavoritesAdapter(Context context, List<Offer> objects) {
super(context, 0, objects);
mLayoutInflater = LayoutInflater.from(context);
}
/* @Override
public long getItemId(int position) {
return position;
}*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.item_favorite_offer, null);
holder = new ViewHolder();
holder.imageView = (ScaledNetworkImageView) convertView.findViewById(R.id.imageView1);
holder.offerText = (CustomFontTextView) convertView.findViewById(R.id.offer_text);
holder.storeName = (CustomFontTextView) convertView.findViewById(R.id.store_name);
holder.expiredTv = (AngledTextView) convertView.findViewById(R.id.expired_tv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//holder.offerText.setText("POSITION + "+position);
final Offer offer = getItem(position);
Date d = Calendar.getInstance().getTime();
if (offer.getActiveUntil() < d.getTime()) {
holder.expiredTv.setVisibility(View.VISIBLE);
} else {
holder.expiredTv.setVisibility(View.INVISIBLE);
}
ImageUtils.load(holder.imageView, offer.getPngLink(), R.drawable.image_placeholder, R.drawable.image_placeholder);
holder.offerText.setText(offer.getDescription());
holder.storeName.setText(offer.getStoreName());
return convertView;
}
static class ViewHolder {
AngledTextView expiredTv;
ScaledNetworkImageView imageView;
CustomFontTextView offerText;
CustomFontTextView storeName;
}
}
在我的片段中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_favorites, container, false);
gridView = (StaggeredGridView) view.findViewById(R.id.staggeredGridView1);
//gridView.setSelector(new ColorDrawable(getResources().getColor(R.color.green_main_color)));
gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
emptyText = (CustomFontTextView) view.findViewById(R.id.empty_grid_view);
int margin = getResources().getDimensionPixelSize(R.dimen.default_padding);
int itemMargin = getResources().getDimensionPixelSize(R.dimen.padding_tell_button);
gridView.setItemMargin(itemMargin); // set the GridView margin
gridView.setPadding(margin, margin, margin, 0); // have the margin on the sides as well
mFavoritesAdapter = new FavoritesAdapter(getActivity(), new ArrayList<Offer>());
mFavoritesAdapter.setNotifyOnChange(false);
gridView.setAdapter(mFavoritesAdapter);
gridView.setOnItemClickListener(new StaggeredGridView.OnItemClickListener() {
@Override
public void onItemClick(StaggeredGridView parent, View view, int position, long id) {
Offer o = mFavoritesAdapter.getItem(position);
if (o != null) {
Intent intent = new Intent(getActivity(), WebViewActivity.class);
intent.putExtra(WebViewActivity.BUNDLE_HTML_LINK, o.getHtmlLink());
intent.putExtra(WebViewActivity.STORE_NAME, o.getStoreName());
startActivity(intent);
}
//getActivity().finish();
}
});
gridView.setOnItemLongClickListener(mOnItemLongClickListener);
//restartLoader(LOADER_FAVORITES);
return view;
}
关于为什么会出错的任何想法?
答案 0 :(得分:4)
也许你的listitems获得了焦点状态(虽然没有打算)。我总是使用这样的选择器构建:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime" >
<item android:state_selected="true" android:drawable="@color/green_bit_transparent"/>
<item android:state_pressed="false" android:state_focused="true" android:state_selected="false" android:drawable="@color/green_bit_transparent" />
<item android:state_pressed="true" android:state_selected="false" android:drawable="@color/green" />
<item android:drawable="@android:color/transparent" />
</selector>