我的 ListView 有问题。我想将 listView 中的每隔一个元素设置为使其背景透明。我使用以下代码:
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_row_object, null);
Drawable backgroundDrawable = context.getResources().getDrawable(R.drawable.abstract_gray);
if((int)(position%2)==1)
backgroundDrawable.mutate().setAlpha(ToolsAndConstants.BACKGROUND_TRANSPARENCY);
// It is not working sometimes when I just use v.setBackground(backgroundDrawable); here. Why?
v.setBackground(backgroundDrawable.mutate());
holder = new ViewHolder();
holder.rowNumber = (TextView) v.findViewById(R.id.list_row2_number);
holder.character = (TextView) v.findViewById(R.id.list_row2_char);
holder.strokesNumber = (TextView) v.findViewById(R.id.list_row2_strokes_number);
v.setTag(holder);
}
else
holder = (ViewHolder)v.getTag();
(...)
(...)
(...)
return v;
}
该列表加载正常,但问题是当我上下滚动它几次时它变得非常疯狂(?随机?设置透明背景和纯色背景)。请参考以下截图(滚动前后):
前:
后:
在适配器类之外,我只是添加了 onClickListener,在其中我用不同的片段替换了片段。没有 onScrollListener 等。为什么布局会发生变化?