2017-12-27 94 views
0
public class Adapter1 extends RecyclerView.Adapter<Adapter1.ViewHolder> { 
ArrayList<CategoryModels> categoryList; 
Context context; 

public Adapter1(ArrayList<CategoryModels> categoryList, Context context){ 
    this.categoryList =categoryList; 
    this.context = context; 

} 


@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_scategory_single_layout,null); 
    final ViewHolder viewHolder=new ViewHolder(view); 

    return viewHolder; 
} 



@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 

     CategoryModels categoryModels = categoryList.get(position); 

    holder.name.setText(categoryModels.getName()); 

    int pos = getItemViewType(position); 
    if(categoryList.get(pos).getImage() == null) { 

     holder.image.setVisibility(View.GONE); 
    } else 
     new DownloadImageTask(holder.image).execute(baseurl+categoryModels.getImage()); 
     //Picasso.with(categoryList.ge).load("http://www.zawallpaper.com/includes/images/"+categoryModels.getImage()).resize(120, 60).into(holder.image); 
} 

@Override 
public int getItemViewType(int position) { 
    return super.getItemViewType(position); 
} 

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

public class ViewHolder extends RecyclerView.ViewHolder{ 
    TextView name; 
    ImageView image; 
    public ViewHolder(View itemView) { 
     super(itemView); 
     name = (TextView) itemView.findViewById(R.id.name); 
     image = (ImageView) itemView.findViewById(R.id.imgcategory); 
    } 
} 
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
    ImageView bmImage; 

    public DownloadImageTask(ImageView bmImage) { 
     this.bmImage = bmImage; 
    } 


    protected Bitmap doInBackground(String... urls) { 
     String urldisplay = urls[0]; 
     Bitmap mIcon = null; 
     try { 
      InputStream in = new java.net.URL(urldisplay).openStream(); 
      mIcon = BitmapFactory.decodeStream(in); 
     } catch (Exception e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 
     return mIcon; 
    } 

    protected void onPostExecute(Bitmap result) { 
     bmImage.setImageBitmap(result); 
     //ProgressDialogue.hideProgressDialogue(); 
    } 


} 
public Bitmap StringToBitMap(String encodedString) { 
    try { 
     byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT); 
     Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length); 
     return bitmap; 
    } catch (Exception e) { 
     e.getMessage(); 
     return null; 
    } 
} 

}RecyclerView形象不斷刷新,當上下滾動,我們

我知道這個問題已經被問過。但我無法解決我的問題。我以json的形式獲取數據。圖像通過異步任務加載。但是當我向上滾動或向下滾動時,互相替換,然後幾秒鐘後,圖像顯示在原始位置。我認爲問題出在OnBind方法中。

回答

0

不要讓API調用onBind
在活動
2. onPostExecute {}設置適配器& 1.進行API調用

recyclerview.setAdapter(new YourAdapter(dataList))<br> 

如果你想上的滾動API調用列表中,在onScroll方法中編寫邏輯

recyclerView.addOnScrollListener 

如果要更改圖像wrt position of recyclerview使用Y圖像加載庫:例如:方
http://square.github.io/picasso/

Picasso.with(context).load(url).into(view); 
+0

我已經調用活動API,其中IAM讓所有的數據,在剛剛加載與URL的IMAG aync任務IM。問題是,當我向上或向下滾動時,圖像被替換爲另一個。無論如何,當我向上或向下滾動時,無論如何都要在他們的職位上嚴格地綁定圖像? – dani

+0

我用piccasso。但它會給出錯誤。上下文不能爲空 – dani

+0

那麼,我已經解決了關於畢加索的問題,它解決了滾動問題。謝謝你的幫助。 :) – dani