getView在自定义适配器中显示错误的图像?

时间:2012-08-02 15:25:13

标签: android listview custom-adapter

我有一个包含自定义适配器和自定义行的列表。

问题是当我滚动列表视图时,图像处于错误的位置......

这是自定义行xml:

<LinearLayout
    android:id="@+id/imagequality_listview_row_linearlayout"
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:weightSum="1"
    android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android">


    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imagequality_listview_row_textview"
android:layout_width="0dp"
android:layout_height="40dip"
android:gravity="right"
android:paddingLeft="5dip"
android:drawableLeft="?android:attr/listChoiceIndicatorMultiple"
android:layout_weight="0.6"
/>

       <com.example.listviewtest.LoaderImageView
   android:id="@+id/imagequality_listview_row_loaderImageView"
   android:layout_width="0dp"
   android:layout_weight="0.4"
   android:layout_height="100dp"
   image="http://developer.android.com/images/dialog_buttons.png"
   />

</LinearLayout>

以下是GetView:

List<ImageQuality_Item> items;
    Context context;
    List<CheckedTextView> checkedList;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.imagequality_listview_row, null);
            holder = new ViewHolder();
            holder.layout = (LinearLayout) convertView.findViewById(R.id.imagequality_listview_row_linearlayout);

            holder.textview = (CheckedTextView) convertView.findViewById(R.id.imagequality_listview_row_textview);
            holder.image = (LoaderImageView) convertView.findViewById(R.id.imagequality_listview_row_loaderImageView);
            checkedList.add(holder.textview);
            holder.layout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int i = 0; i < checkedList.size(); i++)
                        if (checkedList.get(i).isChecked())
                            checkedList.get(i).setChecked(false);
                    ((CheckedTextView) ((LinearLayout) v).getChildAt(0)).setChecked(true);
                }
            });

            holder.textview.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int i = 0; i < checkedList.size(); i++)
                        if (checkedList.get(i).isChecked())
                            checkedList.get(i).setChecked(false);
                    ((CheckedTextView) v).setChecked(true);
                }
            });
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.textview.setText(items.get(position).getDescription());
        if (holder.image.used == false) {
            holder.image.setImageDrawable(items.get(position).getURL());
            holder.image.used = true;
        }

        return convertView;
    }

    class ViewHolder {
        LinearLayout layout;
        LoaderImageView image;
        CheckedTextView textview;
    }

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

每次调用Drawable时都必须设置getView(),因为android正在重用列表元素。如果您不更新,可能会出现较早设置的图像。

只需删除条件即可将图像设置为可绘制,并且它可以正常工作。