2016-04-18 5 views
0

新しいスレッドでロードロジックを移動しようとしましたが、RecyclerImageViewHolderのImageViewが非常に速く変化します。エラーがRecyclerViewという方法で行うことです、それはRecyclerImageViewHolder新しいスレッドでアイテムをロード

demonstration

//One view - one thread 
    private Thread imageLoadThread = null; 

    public AuthorRecyclerViewHolder(LinearLayout itemView, Activity activity) { 
     super(itemView); 
     this.view = itemView; 
     itemView.findViewById(R.id.card_view).setOnClickListener(this); 
     this.activity = activity; 
    } 

    public AuthorRecyclerViewHolder setItem(AuthorObject ao) { 
     this.ao = ao; 

     //Find image 
     iv = (ImageView) view.findViewById(R.id.imageView); 

     //Clear 
     if (imageLoadThread != null) 
      imageLoadThread.interrupt(); 
     iv.clearAnimation(); 
     iv.setImageResource(R.drawable.notfoundmusic); 

     //Set content 
     setImageOnItemView(activity, iv, false); 
     ((TextView) view.findViewById(R.id.description)).setText(ao.description); 
     ((TextView) view.findViewById(R.id.head_author)).setText(ao.name); 
     return this; 
    } 


    public void setImageOnItemView(final Activity activity, final ImageView iv, boolean isBigPicture) { 
     final Animation vis = AnimationUtils.loadAnimation(activity, R.anim.alphavisible); 
     final ImageResource ir = isBigPicture ? ao.bigImage == null ? ao.smallImage : ao.bigImage : ao.smallImage == null ? ao.bigImage : ao.smallImage; 
     if (ir != null) 
      this.imageLoadThread = ir.getImage(this); 
     else { 
      iv.setImageResource(R.drawable.notfoundmusic); 
      iv.startAnimation(vis); 
      iv.setAlpha(1.0F); 
     } 
    } 


    @Override 
    public void recieveResource(final @Nullable Bitmap bitmap, final String id) { 
     final Thread thisThread = Thread.currentThread(); 

     activity.runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 

       final Animation vis = AnimationUtils.loadAnimation(activity, R.anim.alphavisible); 
       final Animation unvis = AnimationUtils.loadAnimation(activity, R.anim.alphaunvisible); 
       iv.clearAnimation(); 

       if (thisThread.isInterrupted()) 
        return; 
       if (id.substring(id.lastIndexOf("_") + 1).equals(String.valueOf(ao.authorId))) 
        if (bitmap != null) { 
         new Handler().postDelayed(new Runnable() { 
          public void run() { 
           iv.setImageBitmap(bitmap); 
           iv.setAnimation(vis); 
          } 
         }, unvis.getDuration()); 
         iv.setAlpha(1.0F); 
         iv.startAnimation(unvis); 
        } else { 
         iv.startAnimation(vis); 
         iv.setAlpha(1.0F); 
        } 
      } 
     }); 
    } 
+1

https://github.com/square/picasso – Ufkoku

答えて

2

を動作しない理由を私は理解していない...私は、割り込みスレッドを試してみて、私は、設定された画像の前にチェックしてみてください、それが動作しませんViewHolderを再利用しています。

基本的に、ここでは、任意の1つに対して何が起こっているのかを見ています。ViewHolderデータが移入

  • を作成し

    1. データセットから移入
    2. 新しいデータダウン
    3. スクロール(画像スレッドが1イメージ番号をダウンロードするために始めた)
    4. スクロール(画像スレッドがイメージ#2をダウンロードするために始めました)下の
    5. 新しいデータがデータセットから読み込まれました(イメージスレッドがイメージ#3をダウンロードし始めました)
    6. スクロールを停止します
    7. 画像#1あなたがスクロールした後のロード複数の画像を参照してください理由ですダウンロードして

    を示す

  • 画像#3をダウンロードして示す
  • 画像#2をダウンロードして示します。

    これは簡単には解決できません。 ViewHolderの基になるコンテンツが変更された場合は、画像をダウンロードするスレッドに停止を指示する必要があります。

  • +0

    私はそれがどのように機能するか理解しています。私はそれがどのように修正するか分からない –

    関連する問題