Evg*_*eny 2 android android-listview
我的ListViewAndroid应用程序中有两个颜色(奇数元素一种颜色,偶数颜色一种颜色).它在Froyo上运行得很好.但是在Jelly Bean模拟器上,滚动期间会出现一些文物.一些元素的背景变黑.是的,我知道透明缓存颜色提示的解决方案!但它只有在我以这种方式设置背景时才有效:
在bindView()适配器的方法:
// ...
view.setBackgroundResource(
cursor.getPosition() % 2 == 0 ? R.color.list_item_bg1: R.color.list_item_bg2);
Run Code Online (Sandbox Code Playgroud)
但是这个方法不适合我,因为我想突出显示元素,然后用户点击它.所以我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);
}
Run Code Online (Sandbox Code Playgroud)
因此,使用此代码,即使我设置透明颜色提示,滚动期间也会出现黑色元素背景ListView.我花了几天时间调查这个问题,但无法克服它.所以,你是我最后的希望,StackOverflow!
摘要:
为列表项创建备用背景颜色.在你的适配器类中编写这行代码
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitem, null);
if (position % 2 == 0) {
v.setBackgroundResource(R.color.firstcolor);
} else {
v.setBackgroundResource(R.color.second color);
}
...............
.........
Run Code Online (Sandbox Code Playgroud)
嗨为了获得完整的帮助,请访问我的Android博客,希望您能找到所需的答案.刚才我已经从我身边做过测试,它对我来说很好.在底部,您将获得一个链接,以便您可以下载完整的源代码.
http://amitandroid.blogspot.in/2013/03/android-listview-with-alternate-list.html
如果它满足您的要求,请告诉我.
| 归档时间: |
|
| 查看次数: |
1506 次 |
| 最近记录: |