突出listview项目android

时间:2012-10-30 10:53:32

标签: android listview highlight

我有自定义基本适配器的listview。当我填充列表时,我必须在对象中检查一个布尔值,我正在填充列表视图,如果是,则更改该行的背景颜色。

public View getView(int position, View convertView, ViewGroup parent) {
    LoginsList entry = listOfLoginsLists.get(position);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.lists_row, null);
    }

    TextView ListName = (TextView) convertView.findViewById(R.id.tvListName);
    ListName.setText(entry.getListName());

    TextView ListDescription = (TextView) convertView.findViewById(R.id.tvListDescription);
    ListDescription.setText(entry.getListDescription());

    Button Send = (Button) convertView.findViewById(R.id.bSend);
    Send.setOnClickListener(this);
    Send.setTag(entry);

    RelativeLayout RelLayout = (RelativeLayout) convertView.findViewById(R.id.layoutListsRow);
    RelLayout.setFocusableInTouchMode(false);
    RelLayout.setFocusable(false);
    RelLayout.setOnClickListener(this);
    RelLayout.setTag(entry);

    if (entry.isSent()) {
        RelLayout.setBackgroundColor(Color.parseColor("#4400FF00"));
    }

    return convertView;
}

但是这段代码不能正常工作。当我滚动列表视图时,行颜色变得混乱。

2 个答案:

答案 0 :(得分:3)

if (entry.isSent()) {
        RelLayout.setBackgroundColor(Color.parseColor("#4400FF00"));
}else {
        RelLayout.setBackgroundColor(//default color);
}

答案 1 :(得分:0)

可能已经定义了列表选择器....或者使用

RelLayout.setBackgroundResource(R.color.mycolor);

同时检查你的isSent()条件是否为真;