处理ListView循环(滚动时重复)

时间:2016-08-08 13:39:13

标签: android listview

我有ListView动态添加图像。当用户向下滚动列表时,行会重复并显示错误的项目+丢失LayoutParams设置。 我该如何解决这个问题?

这是我的getView代码:

public View getView(int position, View view, ViewGroup parent) {
    View rowView = view;
    ViewHolder viewHolder = new ViewHolder();
    if (rowView == null) {
        LayoutInflater inflater = ctx.getLayoutInflater();
        rowView = inflater.inflate(R.layout.list_row, null, true);
        viewHolder.linearLayout = (LinearLayout) rowView.findViewById(R.id.ll_row);
        viewHolder.textView = (TextView) rowView.findViewById(R.id.textview_row);
        rowView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder)rowView.getTag();
    }
    imageResourseId = new ArrayList<>(Arrays.asList(convertStringToArray(imageNames.get(position))));
    for (int i = 0; i < imageResourseId.size(); i++) {
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                0,
                ViewGroup.LayoutParams.WRAP_CONTENT, 0.14f);
        ImageView imageView = new ImageView(ctx);
        //imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setLayoutParams(param);
        imageView.setImageResource(ctx.getResources().getIdentifier(imageResourseId.get(i), "drawable", ctx.getPackageName()));
        viewHolder.linearLayout.addView(imageView);
    }
    viewHolder.linearLayout.setTag("images");
    viewHolder.textView.setText(textArray.get(position));
    return rowView;
}

1 个答案:

答案 0 :(得分:0)

这是基于视图如何回收和膨胀。您应该做的第一件事是将Image的源设置为null,然后设置您想要的图像。这里有点早,我不记得具体细节。