2015-07-02 29 views
5

Tôi có RecyclerView với GridLinearLayout và bộ điều hợp tùy chỉnh. Nội dung của mỗi mục là một hình ảnh được tải xuống bằng cách sử dụng một json và phân tích nó.Ngăn RecyclerView hiển thị nội dung trước đó khi cuộn

Về cơ bản là một mạng lưới ảnh.

Mọi thứ hoạt động gần như tốt, tuy nhiên, khi cuộn xuống nội dung và đi lên lần nữa, nó sẽ hiển thị các chế độ xem trước đó trong mỗi mục trong ít hơn một giây và sau đó hiển thị lại ảnh thích hợp.

Tôi có thể làm gì để ngăn chặn hoặc khắc phục sự cố này? Cảm ơn trước vì bất kỳ trợ giúp và/hoặc hướng dẫn nào bạn có thể cung cấp.

Đây là mã adapter:

package jahirfiquitiva.project.adapters; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.support.v7.graphics.Palette; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.ProgressBar; 
import android.widget.TextView; 

import jahirfiquitiva.project.activities.WallpapersActivity; 
import com.koushikdutta.async.future.FutureCallback; 
import com.koushikdutta.ion.Ion; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.WeakHashMap; 

import jahirfiquitiva.project.R; 

public class WallpapersAdapter extends RecyclerView.Adapter<WallpapersAdapter.WallsHolder> { 

    public interface ClickListener { 
     void onClick(WallsHolder view, int index, boolean longClick); 
    } 

    private ArrayList<HashMap<String, String>> data; 
    private final Context context; 
    private boolean usePalette = true; 
    private final ClickListener mCallback; 
    private final Map<String, Palette> mPaletteCache = new WeakHashMap<>(); 

    public WallpapersAdapter(Context context, ClickListener callback) { 
     this.context = context; 
     this.data = new ArrayList<>(); 
     this.mCallback = callback; 
    } 

    public void setData(ArrayList<HashMap<String, String>> data) { 
     this.data = data; 
     notifyDataSetChanged(); 
    } 

    @Override 
    public WallsHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     LayoutInflater inflater = LayoutInflater.from(context); 
     return new WallsHolder(inflater.inflate(R.layout.wallpaper_item, parent, false)); 
    } 

    @Override 
    public void onBindViewHolder(final WallsHolder holder, int position) { 
     Animation anim = AnimationUtils.loadAnimation(context, android.R.anim.fade_in); 
     HashMap<String, String> jsondata = data.get(position); 

     holder.name.setText(jsondata.get(WallpapersActivity.NAME)); 
     final String wallurl = jsondata.get(WallpapersActivity.WALL); 
     holder.wall.startAnimation(anim); 
     holder.wall.setTag(wallurl); 

     Ion.with(context) 
       .load(wallurl) 
       .asBitmap() 
       .setCallback(new FutureCallback<Bitmap>() { 
        @Override 
        public void onCompleted(Exception e, Bitmap result) { 
         holder.progressBar.setVisibility(View.GONE); 
         if (e != null) { 
          e.printStackTrace(); 
         } else if (holder.wall.getTag() != null && holder.wall.getTag().equals(wallurl)) { 
          holder.wall.setImageBitmap(result); 
          if (usePalette) { 
           Palette p; 
           if (mPaletteCache.containsKey(wallurl)) { 
            p = mPaletteCache.get(wallurl); 
           } else { 
            p = new Palette.Builder(result).generate(); 
            mPaletteCache.put(wallurl, p); 
           } 
           if (p != null) { 
            Palette.Swatch wallSwatch = p.getVibrantSwatch(); 
            if (wallSwatch != null) { 
             holder.titleBg.setBackgroundColor(wallSwatch.getRgb()); 
             holder.titleBg.setAlpha(1); 
             holder.name.setTextColor(wallSwatch.getTitleTextColor()); 
             holder.name.setAlpha(1); 
            } 
           } 
          } 
         } 
        } 
       }); 
    } 

    @Override 
    public int getItemCount() { 
     return data.size(); 
    } 

    public class WallsHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { 

     public final View view; 
     public final ImageView wall; 
     public final TextView name; 
     public final ProgressBar progressBar; 
     public final LinearLayout titleBg; 

     WallsHolder(View v) { 
      super(v); 
      view = v; 
      wall = (ImageView) v.findViewById(R.id.wall); 
      name = (TextView) v.findViewById(R.id.name); 
      progressBar = (ProgressBar) v.findViewById(R.id.progress); 
      titleBg = (LinearLayout) v.findViewById(R.id.titlebg); 

      view.setOnClickListener(this); 
      view.setOnLongClickListener(this); 
     } 

     @Override 
     public void onClick(View v) { 
      int index = getLayoutPosition(); 
      if (mCallback != null) 
       mCallback.onClick(this, index, false); 
     } 

     @Override 
     public boolean onLongClick(View v) { 
      int index = getLayoutPosition(); 
      if (mCallback != null) 
       mCallback.onClick(this, index, true); 
      return false; 
     } 
    } 
} 

Trả lời

3

Như tên cho thấy RecyclerView tái chế các quan điểm để tối ưu hóa bộ nhớ để nó sẽ hiển thị nội dung của giao diện trước đó. Vì bạn đang tải hình ảnh từ internet nên mất ít thời gian để tải hình ảnh để nội dung của hình ảnh trước đó có thể được quan sát. Bạn có thể làm một trong những điều sau đây.

1) Thiết lập một hình ảnh mặc định từ nguồn địa phương trước khi tải hình ảnh thực tế, tốt nhất là một hình ảnh kích thước nhỏ để giữ gìn memory.something như thế này

//load default image first     
holder.wall.setImageResource(R.id.your_default_image_resource); 
//load actual image 
Ion.with(context) 
      .load(wallurl) 
    .asBitmap() 
    .setCallback(new FutureCallback<Bitmap>() { 
     @Override 
     public void onCompleted(Exception e, Bitmap result) { 
      holder.progressBar.setVisibility(View.GONE); 
      if (e != null) { 
       e.printStackTrace(); 
      } else if (holder.wall.getTag() != null && holder.wall.getTag().equals(wallurl)) { 
       holder.wall.setImageBitmap(result); 
       if (usePalette) { 
        Palette p; 
        if (mPaletteCache.containsKey(wallurl)) { 
         p = mPaletteCache.get(wallurl); 
        } else { 
         p = new Palette.Builder(result).generate(); 
         mPaletteCache.put(wallurl, p); 
        } 
        if (p != null) { 
         Palette.Swatch wallSwatch = p.getVibrantSwatch(); 
         if (wallSwatch != null) { 
          holder.titleBg.setBackgroundColor(wallSwatch.getRgb()); 
          holder.titleBg.setAlpha(1); 
          holder.name.setTextColor(wallSwatch.getTitleTextColor()); 
          holder.name.setAlpha(1); 
         } 
        } 
       } 
      } 
     } 
    }); 

2) Thiết lập khả năng hiển thị ImageView để đi/INVISIBLE trước khi tải hình ảnh và làm cho nó VISIBLE một lần nữa sau khi hình ảnh được tải.

//hide the imageview 
holder.wall.setVisibility(View.INVISIBLE); 
Ion.with(context) 
      .load(wallurl) 
    .asBitmap() 
    .setCallback(new FutureCallback<Bitmap>() { 
     @Override 
     public void onCompleted(Exception e, Bitmap result) { 
      holder.progressBar.setVisibility(View.GONE); 
      if (e != null) { 
       e.printStackTrace(); 
      } else if (holder.wall.getTag() != null && holder.wall.getTag().equals(wallurl)) { 
       //show the imageview and set bitmap 
       holder.wall.setVisibility(View.VISIBLE); 
       holder.wall.setImageBitmap(result); 
       if (usePalette) { 
        Palette p; 
        if (mPaletteCache.containsKey(wallurl)) { 
         p = mPaletteCache.get(wallurl); 
        } else { 
         p = new Palette.Builder(result).generate(); 
         mPaletteCache.put(wallurl, p); 
        } 
        if (p != null) { 
         Palette.Swatch wallSwatch = p.getVibrantSwatch(); 
         if (wallSwatch != null) { 
          holder.titleBg.setBackgroundColor(wallSwatch.getRgb()); 
          holder.titleBg.setAlpha(1); 
          holder.name.setTextColor(wallSwatch.getTitleTextColor()); 
          holder.name.setAlpha(1); 
         } 
        } 
       } 
      } 
     } 
    }); 
+0

Tùy chọn đầu tiên làm việc. Cái thứ hai vẫn cho tôi cùng một vấn đề. Cảm ơn đã giúp đỡ. –

0

Tôi nghĩ bạn nên thêm giữ chỗ như thế này:

Ion.with(context).load("http://example.com/image.png") 
    .withBitmap() 
    .placeholder(R.drawable.placeholder_image) 
    .error(R.drawable.error_image) 
    .intoImageView(imageView); 

hoặc thiết lập hình ảnh mặc định lúc đầu.

holder.wall.setImageResource(R.drawable.placeholder_image); 
2

Ghi đè onViewRecycled (VH holder) để đặt hình ảnh thành không.

Như thế này:

public void onViewRecycled (VH holder) { 

    holder.wall.setImageBitmap(null); 
} 
Các vấn đề liên quan