How to load the Listview “smoothly” in android

前端 未结 5 2039
盖世英雄少女心
盖世英雄少女心 2020-11-30 20:09

I load data from Cursor to listview, but my Listview not really display \"smooth\". The data change when I drag up and down on the scollbar in my ListView. And some items lo

5条回答
  •  猫巷女王i
    2020-11-30 20:32

    I'm facing this problem as well, but in my case I used threads to fetch the external images. It is important that the current executing thread do not change the imageView if it is reused!

    public View getView(int position, View vi, ViewGroup parent) {
    ViewHolder holder;
    String imageUrl = ...;
    
    if (vi == null) {
        vi = inflater.inflate(R.layout.tweet, null);
        holder = new ViewHolder();
        holder.image = (ImageView) vi.findViewById(R.id.row_img);
        ...
        vi.setTag(holder);
    } else {
        holder = (ViewHolder) vi.getTag();      
    }
    holder.image.setTag(imageUrl);
    ...
    DRAW_MANAGER.fetchDrawableOnThread(imageUrl, holder.image);
    }
    

    And then on the fetching thread I'm doing the important check:

    final Handler handler = new Handler() {
    @Override
    public void handleMessage(Message message) {
         // VERY IMPORTANT CHECK
        if (urlString.equals(url))
            imageView.setImageDrawable((Drawable) message.obj);
    };
    
    Thread thread = new Thread() {  
    
    @Override
    public void run() {
        Drawable drawable = fetchDrawable(urlString);
        if (drawable != null) {
            Message message = handler.obtainMessage(1, drawable);
            handler.sendMessage(message);
        }
    }};
    thread.start();
    

    One could also cancel the current thread if their view is reused (like it is described here), but I decided against this because I want to fill my cache for later reuse.

提交回复
热议问题