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

Android 如何在GridView中设置第一行颜色,android,gridview,Android,Gridview,如何在gridView中仅设置第一行颜色?我试着使用这段代码,但在我在网格中滚动之前,这段代码工作得很好,因为我用我的颜色设置了多个单元格。 有人能帮我吗 public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if (convertView == null) { LayoutInflater li = getLayoutI

如何在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;

}
如果(位置==0) 视图.setBackgroundColor(0x30FF0000); 其他的 view.setBackgroundColor(0x00000000)


适配器始终具有缓存这是因为视图重用<代码>转换视图滚动时弹出的视图与从另一侧弹出的循环视图相同。因此,它仍然具有先前设置的背景。您可以添加此行以防止出现以下情况:

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;

}