滚动时重复列表视图项目

时间:2015-12-07 07:45:50

标签: android listview

我在listview上使用带有convertview的ArrayAdapter。但是当它滚动时,有一些项目重复。你能帮我解决一下这个问题吗?谢谢 getView函数的主要代码:

        View view;
        if (null != convertView) {
            view = convertView;
        } else {
            view = getActivity().getLayoutInflater().inflate(R.layout.item_fans_task, null);
            taskImg = (ImageView) view.findViewById(R.id.task_img);
            taskTitleText = (TextView) view.findViewById(R.id.task_title_text);
            taskContentText = (TextView) view.findViewById(R.id.task_content_text);
            taskProcessImg = (ImageView) view.findViewById(R.id.task_process_img);
            shareText = (TextView) view.findViewById(R.id.share_text);
            taskType = (TextView) view.findViewById(R.id.task_type);
        }
        Tasks task = getItem(position);

        taskTitleText.setText(task.getTitle());
        StringBuilder builder = new StringBuilder();
        builder.append("已将文章分享到朋友圈,并获得" + task.getReaders() +"次阅读\n");
        builder.append("已分享" + task.getTsum() + "次" + "  剩余" + task.getSynum() + "次" );
        taskContentText.setText(builder);

...

        return view;

2 个答案:

答案 0 :(得分:1)

只有在没有要回收的视图时才会分配“UI” - 变量(view == null)。 也许应该使用ViewHolder模式。目前,以下情况应该没问题:

    View view;
    if (null != convertView) {
        view = convertView;
    } else {
        view = getActivity().getLayoutInflater().inflate(R.layout.item_fans_task, null);
    }
    taskImg = (ImageView) view.findViewById(R.id.task_img);
    taskTitleText = (TextView) view.findViewById(R.id.task_title_text);
    taskContentText = (TextView) view.findViewById(R.id.task_content_text);
    taskProcessImg = (ImageView) view.findViewById(R.id.task_process_img);
    shareText = (TextView) view.findViewById(R.id.share_text);
    taskType = (TextView) view.findViewById(R.id.task_type);

答案 1 :(得分:0)

你的行

if (null != convertView) {
    view = convertView;
}

导致该错误。只需修复,删除该代码。如果您希望使用ViewHolder进行ListView滚动平滑。查看本教程了解更多详细信息 http://www.vogella.com/tutorials/AndroidListView/article.html