onlyarce
莫为难
2016-01-05 11:47

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

  • android
  • item点击
  • gridview
  • 自适应高度

我在自己写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条回答