GridView的第一个项目没有显示出来

时间:2015-05-07 05:27:32

标签: android gridview

我尝试添加gridView并自定义其适配器。

但是第一个项目没有出现,而项目点击监听器也不起作用。 myImage

我的代码不正确吗?

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView text;
    if (convertView == null) {
        convertView = mLayoutInflater.inflate(R.layout.layout_mission_private_item, parent, false);
        convertView.setLayoutParams(new AbsListView.LayoutParams(parent.getWidth() / 3, parent.getHeight() / 2));
        text = (TextView) convertView.findViewById(R.id.tvContent);
        convertView.setTag(text);
    } else {
        text = (TextView) convertView.getTag();
    }
    text.setText(mData.get(position).first);
    if (mData.get(position).second)
        text.setBackgroundColor(Color.GREEN);
    return convertView;
}

编辑:完整的来源

public class MissionPrivateGvAdapter extends BaseAdapter {

private LayoutInflater mLayoutInflater;
public ArrayList<Pair<String, Boolean>> mData;

public MissionPrivateGvAdapter(LayoutInflater inflater, ArrayList<Pair<String, Boolean>> data) {
    mLayoutInflater = inflater;
    mData = data;
}

@Override
public int getCount() {

    return mData.size();
}

@Override
public Object getItem(int position) {
    return mData.get(position).first;
}

@Override
public long getItemId(int position) {

    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView text;
    if (convertView == null) {
        convertView = mLayoutInflater.inflate(R.layout.layout_mission_private_item, parent, false);
        convertView.setLayoutParams(new AbsListView.LayoutParams(parent.getWidth() / 3, parent.getHeight() / 2));
        text = (TextView) convertView.findViewById(R.id.tvContent);
        convertView.setTag(text);
    } else {
        text = (TextView) convertView.getTag();
    }
    text.setText(mData.get(position).first);
    if (mData.get(position).second)
        text.setBackgroundColor(Color.GREEN);
    return convertView;
}

}

1 个答案:

答案 0 :(得分:1)

首次调用getView方法时,父视图的宽度和高度为零。也许您可以获取屏幕内容的宽度和高度来设置它们的值。