如何在GridView中设置第一行颜色

时间:2013-05-27 08:37:14

标签: android gridview

如何在gridView中仅设置第一行颜色?我尝试使用这段代码,但这在我滚动网格之前效果很好,而不是我用我的颜色设置了多个单元格。 任何人都可以帮我这个吗?

public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;

    if (convertView == null) {

        LayoutInflater li = getLayoutInflater();
        view = li.inflate(R.layout.main, null);
    }

    if (position == 0)
        view.setBackgroundColor(0x30FF0000);
    return view;

}

2 个答案:

答案 0 :(得分:1)

if(position == 0)         view.setBackgroundColor(0x30FF0000);   其他     view.setBackgroundColor(00000000);

适配器始终具有缓存

答案 1 :(得分:1)

这是因为View重用。滚动时弹出的convertView是从另一侧发出的同一回收视图。所以它仍然具有先前设置的背景。您可以添加此行以防止这种情况:

public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;

    if (convertView == null) {

        LayoutInflater li = getLayoutInflater();
        view = li.inflate(R.layout.main, null);
    }

    if (position == 0)
        view.setBackgroundColor(0x30FF0000);
    else  view.setBackgroundColor(/*default color for the other rows*/);
    return view;

}
相关问题