Gridview的自适应高度后第一个item点击无效的问题

我在自己写GridView的时候,想弄自适应高度,然后第一个item点击无响应,
后来在网上找到方法,试了还是无效。我的代码如下:
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint("NewApi")
public class CameraAdapter extends BaseAdapter {
private GridView mGridView;
private String data[] = null;
private int imgId[] = null;
private Context context;
private LayoutInflater mInflater;

public CameraAdapter(GridView mGridView, String[] data, int[] imgId,
        Context context) {
    super();
    this.mGridView = mGridView;
    this.data = data;
    this.imgId = imgId;
    this.context = context;
    mInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {

    return data.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

public class ViewHolder {
    TextView itemText;
    ImageView itemImage;

    public TextView getItemText() {
        return itemText;
    }

    public void setItemText(TextView itemText) {
        this.itemText = itemText;
    }

    public ImageView getItemImage() {
        return itemImage;
    }

    public void setItemImage(ImageView itemImage) {
        this.itemImage = itemImage;
    }

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder = null;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.grid_item, parent,false);
        // 得到GridView每一项的高度与宽度
        if (mGridView.getNumColumns() == 2) {
            convertView.setLayoutParams(new GridView.LayoutParams(
                    android.view.ViewGroup.LayoutParams.FILL_PARENT,
                    mGridView.getHeight() / 2));
        } else if (mGridView.getNumColumns() == 3) {
            convertView.setLayoutParams(new GridView.LayoutParams(
                    android.view.ViewGroup.LayoutParams.FILL_PARENT,
                    mGridView.getHeight() / 3));
        } else if (mGridView.getNumColumns() == 4) {
            convertView.setLayoutParams(new GridView.LayoutParams(
                    android.view.ViewGroup.LayoutParams.FILL_PARENT,
                    mGridView.getHeight() / 4));
        }
        viewHolder = new ViewHolder();
        viewHolder.itemText = (TextView) convertView
                .findViewById(R.id.itemText);
        viewHolder.itemImage = (ImageView) convertView
                .findViewById(R.id.itemImage);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
        // 得到GridView每一项的高度与宽度
        if (mGridView.getNumColumns() == 2) {
            convertView.setLayoutParams(new GridView.LayoutParams(
                    android.view.ViewGroup.LayoutParams.FILL_PARENT,
                    mGridView.getHeight() / 2));
        } else if (mGridView.getNumColumns() == 3) {
            convertView.setLayoutParams(new GridView.LayoutParams(
                    android.view.ViewGroup.LayoutParams.FILL_PARENT,
                    mGridView.getHeight() / 3));
        } else if (mGridView.getNumColumns() == 4) {
            convertView.setLayoutParams(new GridView.LayoutParams(
                    android.view.ViewGroup.LayoutParams.FILL_PARENT,
                    mGridView.getHeight() / 4));
        }
    }
    if (position == 0) {
        parent.postInvalidate();
    }
    viewHolder.itemText.setText(data[position]);
    viewHolder.itemImage.setImageResource(imgId[position]);

    //
    return convertView;
}

}

谁能指点下,到底还有哪里需要修改吗?

2个回答

GridView有一些bug.
为什么不用RecycleView?RecycleView能完美的自适应高度.
http://blog.csdn.net/rozol/article/details/50225593

onlyarce
莫为难 不是用的5.0,没有recycleView组件,没办法啊
接近 4 年之前 回复

谁能指点下吗?急急急急急急急呀!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!