如何设置第一线的颜色在GridView [英] how to set first line color in a GridView

查看:239
本文介绍了如何设置第一线的颜色在GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能只设置在GridView第一线的颜色?我试图用这个code,但这个工作得很好,直到我滚动发车,比我有多个细胞,我的颜色设置。
谁能帮我这个?

 公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    查看查看= convertView;    如果(convertView == NULL){        LayoutInflater李= getLayoutInflater();
        鉴于= li.inflate(R.layout.main,NULL);
    }    如果(位置== 0)
        view.setBackgroundColor(0x30FF0000);
    返回视图。}


解决方案

这是因为观重用。 convertView 当您滚动是一样的再生认为,从另一个侧面出来就是蹦出来。因此,它仍然有后台previously设置。您可以将此行添加到prevent这样的:

 公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    查看查看= convertView;    如果(convertView == NULL){        LayoutInflater李= getLayoutInflater();
        鉴于= li.inflate(R.layout.main,NULL);
    }    如果(位置== 0)
        view.setBackgroundColor(0x30FF0000);
    其他view.setBackgroundColor(/ *为其他行默认颜色* /);
    返回视图。}

How can I set only the first line color in a gridView? I tried to use this code, but this works well until I scroll in the grid, than I have multiple cells set with my color. Can anyone help me with this?

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;

}

解决方案

This is because of View reuse. convertView that is popped out when you scroll is the same recycled View that comes out from the other side. So it still has the background previously set. You can add this line to prevent this:

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;

}

这篇关于如何设置第一线的颜色在GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆