【问题标题】:highlight listview items android高亮listview项目android
【发布时间】:2012-10-30 10:53:31
【问题描述】:

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

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

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

【问题讨论】:

标签: android listview highlight


【解决方案1】:
if (entry.isSent()) {
        RelLayout.setBackgroundColor(Color.parseColor("#4400FF00"));
}else {
        RelLayout.setBackgroundColor(//default color);
}

【讨论】:

  • 你需要 else 部分,因为没有 else 部分,任何设置为 "#4400FF00" 的视图的背景颜色将永远不会回到原来的样子。
【解决方案2】:

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

RelLayout.setBackgroundResource(R.color.mycolor);

还要检查在任何情况下您的 isSent() 条件是否为真;

【讨论】:

    猜你喜欢
    • 2013-08-07
    • 2011-09-07
    • 1970-01-01
    • 2015-02-20
    • 2014-10-24
    • 2014-09-02
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    相关资源
    最近更新 更多