2014-10-22 203 views
2

我有一個問題,當我滾動我的列表視圖,圖像似乎繼續重新加載自己,它使得列表視圖滯後很多。 我能做些什麼來防止這種情況發生,我之前在我的listview中做過這件事,而且它沒有這樣做。畢加索圖像重新加載滾動在列表視圖

public class PostsAdapter extends BaseAdapter{ 
    public List<PostList> postList; 
    protected Context context; 

    public void add(PostList object,int position) { 
      postList.add(position,object); 
     } 

     public PostsAdapter(Context context) { 
      this.context = context; 
      postList = new ArrayList<PostList>(); 
     } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return postList.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return postList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     ViewHolder holder; 
     if(convertView == null){ 
      convertView = LayoutInflater.from(context).inflate(R.layout.posts_list, null); 
      holder = new ViewHolder(); 
      holder.image = (ImageView)convertView.findViewById(R.id.postImage); 
      holder.username = (TextView)convertView.findViewById(R.id.postUsername); 
      convertView.setTag(holder); 
     }else{ 
      holder = (ViewHolder)convertView.getTag(); 
     } 
     holder.username.setText(postList.get(position).user); 
     Picasso.with(context).load(postList.get(position).postPicture).into(holder.image); 

     return convertView; 
    } 
static class ViewHolder{ 
     ImageView image; 
     TextView username; 
    } 
+0

加上'Picasso.with(上下文).load(postList.get(位置).postPicture).into(架.image);'in you'if(convertView == null){...}'從其他地方移除。 – Rustam 2014-10-22 06:02:35

+0

public void add(PostList對象,int位置){ postList.add(position,object); }這就是爲什麼你使用 – 2014-10-22 06:04:26

+0

public PostsAdapter(Context context,ArrayList List){ this.context = context; postList = List; }在適配器中使用此缺點 – 2014-10-22 06:05:20

回答

0
public class PostsAdapter extends BaseAdapter 
    { 
     public List<PostList> postList; 
     protected Context context; 

    public PostsAdapter(Context context,ArrayList<PostList> List) 
    { 
     this.context = context; 
     postList = List; 
    } 

    @Override 
    public int getCount() 
    { 
    // TODO Auto-generated method stub 
    return postList.size(); 
    } 

@Override 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return postList.get(position); 
} 

@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    ViewHolder holder; 
    if(convertView == null){ 
     convertView = LayoutInflater.from(context).inflate(R.layout.posts_list, null); 
     holder = new ViewHolder(); 
     holder.image = (ImageView)convertView.findViewById(R.id.postImage); 
     holder.username = (TextView)convertView.findViewById(R.id.postUsername); 
     convertView.setTag(holder); 
    }else{ 
     holder = (ViewHolder)convertView.getTag(); 
    } 
    holder.username.setText(postList.get(position).user); 
    Picasso.with(context).load(postList.get(position).postPicture).into(holder.image); 

    return convertView; 
} 
    static class ViewHolder 
{ 
    ImageView image; 
    TextView username; 
} 
+0

這仍然不起作用。 – Rhynoboy2009 2014-10-22 06:50:59

+0

檢查yor活動代碼becouse repting問題不是來自adapetr我想你可能會使用更多的代碼在android也發佈活動代碼 – 2014-10-22 06:52:49

+0

我的意思是重複每次我滾動瀏覽列表視圖,畢加索再次加載圖像。我想要加載圖像1次,並在不加載圖像的情況下出現。 – Rhynoboy2009 2014-10-22 06:54:10

2

當用戶觸發一扔MotionEvent,應用程序下載圖像,它需要停止滾動的濃縮,然後回到儘快停止運動再次下載圖像。這樣做的效果幾乎是神奇的。

此外,谷歌給出了代碼,以顯示它是如何完成的。你可以在這裏找到它 - https://github.com/google/iosched

下面是如何添加一個滾動監聽器來停止和啓動隊列的快速片段。

listView.setOnScrollListener(new OnScrollListener() { 
    @Override 
    public void onScrollStateChanged(AbsListView listView, int scrollState) { 
     // Pause disk cache access to ensure smoother scrolling 
     if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) { 
      imageLoader.stopProcessingQueue(); 
     } else { 
      imageLoader.startProcessingQueue(); 
     } 
    } 

    @Override 
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
      // TODO Auto-generated method stub 
    } 
}); 
0

使用調整與畢加索

    Picasso.with(context) 
       .load(imageURL) 
       .tag(context) 
       .placeholder(R.drawable.kanye8080s) 
       .error(R.drawable.stadiumarcadium) 
       .into(imageView) 
       .resize(x,y); 

//這絕對有幫助