Androidアプリケーションに2色ListView
のがあります(奇数要素用に1色、偶数要素用に1色)。そして、それはフローズンヨーグルトでうまく機能します。しかし、Jelly Beanエミュレーターでは、スクロール中にいくつかのアーティファクトが表示されます。一部の要素の背景が黒くなります。はい、透明なキャッシュカラーヒントを使用したソリューションについて知っています。ただし、このように背景を設定した場合にのみ機能します。
bindView()
アダプターの方法:
// ...
view.setBackgroundResource(
cursor.getPosition() % 2 == 0 ? R.color.list_item_bg1: R.color.list_item_bg2);
しかし、要素を強調表示したいので、この方法は私には適していません。次に、ユーザーはそれをタップします。だから私StateListDrawable
はこの目的のために使用します:
mOddColorDrawable = new ColorDrawable(
context.getResources().getColor(R.color.list_item_bg2));
mEvenColorDrawable = new ColorDrawable(
context.getResources().getColor(R.color.list_item_bg1));
mSelector = new ColorDrawable(
context.getResources().getColor(R.color.list_item_selector));
public void bindView(View view, Context context, Cursor cursor) {
// ...
setBackground(cursor.getPosition % 2 != 0, view);
}
public void setBackground(boolean isOdd, View listItem) {
StateListDrawable dr = new StateListDrawable();
Drawable drColor = isOdd ? mOddColorDrawable : mEvenColorDrawable;
dr.addState(new int[] { android.R.attr.state_pressed }, mSelectorDrawable);
dr.addState(new int[] { -android.R.attr.state_pressed }, drColor);
listItem.setBackgroundDrawable(bg);
}
したがって、このコードでは、に透明な色のヒントを設定しても、スクロール中に黒い要素の背景が表示されListView
ます。私はこの問題を調査するために何日も費やしましたが、それを克服することはできませんでした。だから、あなたは私の最後の希望です、StackOverflow!
概要:
- 2つの異なる要素色のListViewが欲しいのですが。
- カスタムカラーのアイテムセレクターが欲しいのですが。
- StateListDrawableを使用した場合、setCacheColorHint(transparent)は役に立ちませんでした。
- フローズンヨーグルトではすべて正常に機能しますが、ジェリービーンズでは機能しません。