滚动时正在复制ListView值

时间:2013-09-04 00:42:13

标签: performance android-layout android-listview android-adapter flowlayout

我正在使用listview st每一行都有一个imageview和一个flowlayout,我扩展了baseadapter并且我发送了一个hashraps的arraylisy每个都有图像路径和一个单词列表我的问题是每次我向上滚动条目“离开”屏幕然后向下并且条目“返回”到屏幕,条目流程图中被回收的单词被复制(意味着如果磁盘上的单词是“dok”,那么之后我向下滚动然后再次向上流动布局中的单词现在是“dok dok”)...我无法弄清楚为什么...... =(

我从这里把flowlayout + bubbles带到了它 - http://www.superliminal.com/sources/FlowLayout.java.html http://nishantvnair.wordpress.com/2010/09/28/android-create-bubble-like-facebook/

和解码异步任务将图像从@ MCeley的答案加载到列表中 - Large ListView containing images in Android

那是我的getView代码 -

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = null;
    FlowLayout flowLayout = null;
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.list_item, null);
        imageView = (ImageView) convertView.findViewById(R.id.list_image);
        flowLayout = (FlowLayout) convertView.findViewById(R.id.flow_tags);
    } else {
        imageView = (ImageView) convertView.findViewById(R.id.list_image);
        flowLayout = (FlowLayout) convertView.findViewById(R.id.flow_tags);
        DecodeTask task = (DecodeTask) imageView.getTag(R.id.list_image);
        if (task != null) {
            task.cancel(true);
        }
    }
    HashMap<String, List<String>> photos = new HashMap<String, List<String>>();
    photos = data.get(position);
    imageView.setImageBitmap(null);
    DecodeTask task = new DecodeTask(imageView);
    task.execute(photos.get(DatabaseHandler.KEY_PATH).get(0));
    imageView.setTag(R.id.list_image, task);

    ArrayList<String> subjects = new ArrayList<String>();
    int size = photos.get(DatabaseHandler.KEY_TAGS).size();
    for (int i = 0; i < size; i++) {
        String name = String.format("name - %s ",
                photos.get(DatabaseHandler.KEY_TAGS).get(i));
        Bubble.getBubble(name, flowLayout, subjects, activity,
                photos.get(DatabaseHandler.KEY_PATH).get(0), false, false);
    }

    return convertView;
}

TNX提前..!

2 个答案:

答案 0 :(得分:0)

尝试使用holder作为您的视图,即ImageView和flowLayout。

   ViewHolder holder;            
       // When convertView is not null, we can reuse it directly, there is no need            
       // to reinflate it. We only inflate a new View when the convertView supplied            
       // by ListView is null.            

       if (convertView == null) {                

        convertView = mInflater.inflate(R.layout.sample, null);   
        // Creates a ViewHolder and store references to the two children views                
        // we want to bind data to.               
        holder = new ViewHolder();                
        holder.imageview= (ImageView) convertView.findViewById(R.id.list_image);               
        holder.flowLayout= (FlowLayout) convertView.findViewById(R.id.flow_tags);                
        convertView.setTag(holder);            
       } else {                
        // Get the ViewHolder back to get fast access to the FlowLayout
        // and the ImageView.


        holder = (ViewHolder) convertView.getTag();

       } 

创建 viewHolder

static class viewHolder(){

        ImageView imageview;
        FlowLayout flowlayout;

}

答案 1 :(得分:0)

在您的Adapter中使用此代码。如果不为null,则应重新使用该行并删除重复的行。

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

    // A ViewHolder keeps references to children views to avoid unneccessary
    // calls
    // to findViewById() on each row.
    ViewHolder holder;
    // When convertView is not null, we can reuse it directly, there is no
    // need
    // to reinflate it. We only inflate a new View when the convertView
    // supplied
    // by ListView is null.

    if (convertView == null) {

        convertView = mInflater.inflate(R.layout.sample, null);
        // Creates a ViewHolder and store references to the two children
        // views
        // we want to bind data to.
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.text);
        holder.icon = (ImageView) convertView.findViewById(R.id.icon);
        convertView.setTag(holder);
    } else {
        // Get the ViewHolder back to get fast access to the TextView
        // and the ImageView.

        holder = (ViewHolder) convertView.getTag();

    }

    // Bind the data efficiently with the holder.
    holder.name.setText(myElements.get(id));
    holder.icon.setImageBitmap(mIcon1);

    return convertView;
}

static class ViewHolder {
    TextView  name;
    ImageView icon;
}
相关问题