我的适配器中有以下代码:
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView==null)
{
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.box_afisare, null);
}
final TextView titlu = (TextView) convertView.findViewById(R.id.titlu);
titlu.setText(Html.fromHtml(textt.get(position)));
titlu.setTextSize(TypedValue.COMPLEX_UNIT_SP,font);
final Integer pos = position;
titlu.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
if (main_contextt.selectie.contains(id_post.get(pos)))
{
Toast.makeText(mContext," REMOVE ",Toast.LENGTH_LONG).show();
titlu.setBackgroundColor(Color.parseColor("#0077CC"));
}
else
{
main_contextt.selectie.add(id_post.get(pos));
titlu.setBackgroundColor(Color.parseColor("#404040"));
}
}
});
return convertView;
}
我设法对选定的一行或多行进行整理。但是当我滚动列表视图并且那些选定的行不再在手机的视野范围内时......背景颜色消失。
仅当线条/线条不在视野范围内时才会消失。我认为适配器正在重绘?
即使在滚动列表视图后,如何保持线条/线条上的颜色设置?
感谢
答案 0 :(得分:0)
ListViews回收他们的子视图。因此,如果您的ListView中有20个项目,但屏幕上只有足够的空间一次显示4个项目,则ListView将只有4个子项,它将循环显示所有20个项目。当一个视图滚出视图时,它将被回收用于下一个视图进入视图。 See this answer获得了很好的解释。
您需要做的是将颜色与某些基础数据联系起来。然后你会使用像。
这样的代码 titlu.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// set data.get(pos).color
}
});
titlu.titlu.setBackgroundColor(data.get(pos).color);
......类似的东西。