0

我有一个大约 20 项的列表视图。每行适配器都有一个图像按钮,当我单击它时会执行不同的操作。当我单击列表视图的项目时,我设法正确获取位置编号。但是当我单击图像按钮时,我得到的位置是错误的。不是 0~19,而是显示 0 1 2 3 0 1 2 3 0 1 2 3 .....

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;        

    if (convertView == null) {
        convertView     = myInflater.inflate(R.layout.list_video, null);
        holder          = new ViewHolder();

        holder.rlContainer      = (RelativeLayout)  convertView.findViewById(R.id.rlContainer);
        holder.imageView        = (ImageView)       convertView.findViewById(R.id.ivLogo);
        holder.buttonBackground = (ImageView)       convertView.findViewById(R.id.ibButtonLebih);
        holder.tvtitle          = (TextView)        convertView.findViewById(R.id.tvTitle);

        holder.buttonBackground.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {               
                videoListClickListener.OnVideoMoreClickListener(position);
            }
        });

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }


    return convertView;
}
4

1 回答 1

1

问题解决了!通过将 onclicklistener 和 setTag 移到 if/else 之外。

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;        

if (convertView == null) {
    convertView     = myInflater.inflate(R.layout.list_video, null);
    holder          = new ViewHolder();

    holder.rlContainer      = (RelativeLayout)  convertView.findViewById(R.id.rlContainer);
    holder.imageView        = (ImageView)       convertView.findViewById(R.id.ivLogo);
    holder.buttonBackground = (ImageView)       convertView.findViewById(R.id.ibButtonLebih);
    holder.tvtitle          = (TextView)        convertView.findViewById(R.id.tvTitle);

} else {
    holder = (ViewHolder) convertView.getTag();
}

holder.buttonBackground.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {               
            videoListClickListener.OnVideoMoreClickListener(position);
        }
    });

    convertView.setTag(holder);


return convertView;

}

于 2012-08-11T02:34:31.680 回答