1

我的应用程序有一个列表视图,列表视图的行由 imageView 和 Textview 组成。该应用程序从 Web 下载图像以在行布局中设置 imageview。当我在列表视图中上下滑动时,我遇到了关于重新下载图像的问题。出于这个原因,我使用ViewHolder模式来保存列表项。问题已解决,但这次我遇到了新问题。

Listview 中有 15 行。因此列表底部的项目不会出现。当我向下滚动列表以查看项目时,列表顶部的项目即将到达 lisview 的末尾。此外,当我浏览列表时,列表末尾的项目将出现在列表顶部。

所以它不会显示需要显示的值或行。

我的 getView() 方法的最后状态是..

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


        ViewHolder viewHolder;

            if (convertView == null) {

                convertView = mInflater.inflate(R.layout.row_layout, null);

                TextView programName = (TextView)convertView.findViewById(R.id.programName);
                TextView programTime = (TextView)convertView.findViewById(R.id.programTime);
                TextView programState = (TextView)convertView.findViewById(R.id.programState);
                ImageView programImage = (ImageView)convertView.findViewById(R.id.programImage);


                programName.setText(values.programNames.get(position));
                programTime.setText(values.programTimes.get(position));
                new imageDownload(programImage).execute(values.programImageUrls.get(position)); 

                viewHolder = new ViewHolder();
                viewHolder.p_Name = programName;
                viewHolder.p_Time = programTime;
                viewHolder.p_State = programState;
                viewHolder.p_Image = programImage;

                convertView.setTag(viewHolder);

            }else{

                viewHolder = (ViewHolder) convertView.getTag();
            }

            return convertView;
        }

我的观点持有人...

public static class ViewHolder {
     TextView p_Name;
     TextView p_Time;
     TextView p_State;
     ImageView p_Image;
}

十分感谢

4

3 回答 3

2

将此移到您设置转换视图的位置之外

programName.setText(values.programNames.get(position));
programTime.setText(values.programTimes.get(position));
new imageDownload(programImage).execute(values.programImageUrls.get(position));

所以它看起来像这样

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


    ViewHolder viewHolder;

        if (convertView == null) {

            convertView = mInflater.inflate(R.layout.row_layout, null);

            TextView programName = (TextView)convertView.findViewById(R.id.programName);
            TextView programTime = (TextView)convertView.findViewById(R.id.programTime);
            TextView programState = (TextView)convertView.findViewById(R.id.programState);
            ImageView programImage =(ImageView)convertView.findViewById(R.id.programImage);



            viewHolder = new ViewHolder();
            viewHolder.p_Name = programName;
            viewHolder.p_Time = programTime;
            viewHolder.p_State = programState;
            viewHolder.p_Image = programImage;

            convertView.setTag(viewHolder);

        }else{

            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.p_Name.setText(values.programNames.get(position));
        viewHolder.p_Time.setText(values.programTimes.get(position));
        new imageDownload(programImage).execute(values.programImageUrls.get(position)); 


        return convertView;
    }

此外,我强烈建议您使用 Picasso 下载图片,因为它会自动为您处理缓存。我还会考虑用 Butterknife 注入您的 ViewHolder,这也会减少您的代码

于 2014-10-16T15:40:53.690 回答
0

谢谢您的回答。我决定将毕加索用于图像。我只是更改了如下代码

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

 ViewHolder viewHolder;

    if (convertView == null) {

        convertView = mInflater.inflate(R.layout.row_layout, null);

        TextView programName = (TextView)convertView.findViewById(R.id.programName);
        TextView programTime = (TextView)convertView.findViewById(R.id.programTime);
        TextView programState = (TextView)convertView.findViewById(R.id.programState);
        ImageView programImage =(ImageView)convertView.findViewById(R.id.programImage);

        viewHolder = new ViewHolder();
        viewHolder.p_Name = programName;
        viewHolder.p_Time = programTime;
        viewHolder.p_State = programState;
        viewHolder.p_Image = programImage;

        convertView.setTag(viewHolder);

    }else{

        viewHolder = (ViewHolder) convertView.getTag();
    }
            viewHolder.p_Ad.setText(values.programIsimleri.get(position));
            viewHolder.p_Saat.setText(values.programSaatleri.get(position));
  Picasso.with(getApplicationContext()).load(values.programImageUrls.get(position)).into(viewHolder.p_Image);


    return convertView;
}

该代码现在可以正常工作。

于 2014-10-16T16:51:51.247 回答
-1

您应该在 ViewHolder 类上有一个位置值,以便您可以比较从 convertView.getTag() 检索到的 ViewHolder 的位置。如果 convertView 不为空,您需要确保视图中的信息正确。

if (viewHolder.position != position) {
    //retrieves the correct position of the information
    viewHolder.p_Name.setText(values.programNames.get(position)); 

    //etc
    viewHolder.position = position;
}
于 2014-10-16T15:46:22.810 回答