当我滚动列表视图时,列表视图中项目的位置正在改变

时间:2014-10-16 15:33:46

标签: android listview android-listview recycle getview

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

Listview中有15行。因此,列表底部的项目不会出现。当我仔细查看列表以查看项目时,列表顶部的项目即将结束。此外,当我对列表进行清理时,列表末尾的项目将成为列表的首位。

因此它不显示需要显示的值或行。

我的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;
}

非常感谢

3 个答案:

答案 0 :(得分: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,这也会减少你的代码

答案 1 :(得分:0)

感谢您的回答。我决定使用Picasso for Images。我只是改变了下面的代码

@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;
}

代码现在正常运行。

答案 2 :(得分:-1)

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

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

    //etc
    viewHolder.position = position;
}