在GridView中,项目宽度和高度是代码控制,但第一个项目的ImageView没有响应OnClickListener

时间:2015-05-19 06:16:59

标签: android gridview

这是我的使用适配器,当我点击第一项headImageView时,我不会跳过OtherUserActivity,我点击第二项headImageView,打开许多OtherUserActivity 。我删除LayoutParams,点击正常

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LoopDetailsDataList loopDetailsDataList = list.get(position);

    ViewHolder holder = ViewHolder.get(context, convertView, parent, R.layout.item_loop_details, position);
    convertView = holder.getConvertView();

    int itemParams = screenWidth / 2 - Utils.newInstance(context).dip2px(10);
    AbsListView.LayoutParams params = new AbsListView.LayoutParams(itemParams, itemParams);
    convertView.setLayoutParams(params);

    ImageView mainImageView = holder.getView(R.id.item_loop_details_image);
    CircleImageView headImageView = holder.getView(R.id.item_loop_details_head_image);

    TextView userName = holder.getView(R.id.item_loop_details_user_name);
    TextView title = holder.getView(R.id.item_loop_details_title);
    TextView praise = holder.getView(R.id.item_loop_details_praise);
    TextView comments = holder.getView(R.id.item_loop_details_comments);

    headImageView.setTag(position);
    headImageView.setOnClickListener(new MyClickListener(position));

    return convertView;
}

private class MyClickListener implements View.OnClickListener {

    private int index;

    private MyClickListener(int index) {
        this.index = index;
    }

    @Override
    public void onClick(View v) {
        String userSign = list.get(index).getaCreateUserSign();
        Log.i(TAG, "CreateUserSign:" + userSign);
        Intent intent = new Intent(context, OtherUserActivity.class);
        intent.putExtra("userSign", userSign);
        context.startActivity(intent);
    }
}

这是我的ViewHolder

private ViewHolder(Context context, ViewGroup parent, int layoutId,
                   int position) {
    this.mPosition = position;
    this.mViews = new SparseArray<>();
    mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
    // setTag
    mConvertView.setTag(this);
}

public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder(context, parent, layoutId, position);
    } else {
        holder = (ViewHolder) convertView.getTag();
        holder.mPosition = position;
    }
    return holder;
}

public View getConvertView() {
    return mConvertView;
}

@SuppressWarnings("unchecked")
public <T extends View> T getView(int viewId) {
    View view = mViews.get(viewId);
    if (view == null) {
        view = mConvertView.findViewById(viewId);
        mViews.put(viewId, view);
    }
    return (T) view;
}

public ViewHolder setText(int viewId, String text) {
    TextView view = getView(viewId);
    view.setText(text);
    return this;
}

public ViewHolder setImageResource(int viewId, int drawableId) {
    ImageView view = getView(viewId);
    view.setImageResource(drawableId);

    return this;
}


public ViewHolder setImageBitmap(int viewId, Bitmap bm) {
    ImageView view = getView(viewId);
    view.setImageBitmap(bm);
    return this;
}

public ViewHolder setImageByUrl(int viewId, String url) {
    ImageLoaderUtils.getInstance(3, ImageLoaderUtils.Type.LIFO).loadImage(url,
            (ImageView) getView(viewId));
    return this;
}

public int getPosition() {
    return mPosition;
}

1 个答案:

答案 0 :(得分:0)

您不应该将视图的宽度更改为屏幕的一半,因为如果您使用的是gridview,如果您指定了该号码,它会为您解决此问题列或它会将其设置为默认的 2 列。我认为由于调整大小有一些重叠的观点,所以希望能解决它。 您还应该对代码执行另外两项操作:

首先您需要将holder = (ViewHolder) convertView.getTag();放在if else旁边,因为无论如何都要这样做,这是为了惯例。

第二次您没有在else块中的mConvertView get方法中设置holder