highlight listview items android

女生的网名这么多〃 提交于 2019-12-25 07:37:36

问题


I have listview with custom base adapter. When I populate the list I have to check a boolean in the object I'm populating the listview and if it is true to change the background color of that row.

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;
}

But this code doesn't work right. When I scroll the list view the rows colors get messed up.


回答1:


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



回答2:


may be have have defined list selector .... or else use

RelLayout.setBackgroundResource(R.color.mycolor);

Also check whether it in any case your isSent() condtion is true or not;



来源:https://stackoverflow.com/questions/13137465/highlight-listview-items-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!