2015-10-03 146 views
2

我想創建一個從服務器下載圖像的片段的RecyclerView,但我遇到了一些問題: 當滾動到快速,你可以看到以前的圖片,我真的不希望這樣。第二件事是我有一些OOM崩潰,我真的不知道該怎麼做,一些更有經驗的程序員可以幫助我嗎?用加載圖像創建RecyclerView的正確方法是什麼?

我的片段適配器代碼:

public class MyPliksAdapter extends RecyclerView.Adapter <MyPliksAdapter.MyViewHolder>{ 

private LayoutInflater inflater; 
private List<MyPlikClass> myPliksList = Collections.emptyList(); 
VolleySingleton volleySingleton; 
ImageLoader imageLoader; 

public MyPliksAdapter (Context context, List<MyPlikClass> myPliks) { 
    inflater = LayoutInflater.from(context); 
    myPliksList =myPliks; 

    volleySingleton = VolleySingleton.getsInstance(); 

    imageLoader = volleySingleton.getImageLoader(); 

} 

@Override 
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = inflater.inflate(R.layout.fragment_my_pliks_single,parent,false); 
    MyViewHolder holder = new MyViewHolder(view); 
    return holder; 
} 

@Override 
public void onBindViewHolder(final MyViewHolder holder, int position) { 
    final MyPlikClass currentItem = myPliksList.get(position); 


    //TODO: 
    if (currentItem.myPlikUserFromImgUrl != null){ 

     imageLoader.get(currentItem.myPlikUserFromImgUrl, new ImageLoader.ImageListener() { 
      @Override 
      public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) { 

       holder.myPlikUserFromImg.setImageBitmap(imageContainer.getBitmap()); 
      } 

      @Override 
      public void onErrorResponse(VolleyError volleyError) { 

      } 
     }); 
    } 

    if (currentItem.myPlikPhotoUrl != null){ 

     holder.myPlikPhoto.setImageUrl(currentItem.myPlikPhotoUrl,imageLoader); 
     //(, "Wrong sesion, pls relogin", Toast.LENGTH_SHORT).show(); 
    } 

    //holder.myPlikGetDate.setText(currentItem.myPlikGetDate); 

    byte[] titleData = Base64.decode(currentItem.myPlikTitle, Base64.DEFAULT); 
    byte[] descriptionData = Base64.decode(currentItem.myPlikDescription, Base64.DEFAULT); 
    byte[] nameData = Base64.decode(currentItem.myPlikUserFromName, Base64.DEFAULT); 

    //byte[] data = Base64.decode(currentItem.myPlikTitle, Base64.DEFAULT); 
    String title = null; 
    String name = null; 
    String description = null; 

    try { 
     title = new String(titleData, "UTF-8"); 
     name = new String(nameData, "UTF-8"); 
     description = new String(descriptionData, "UTF-8"); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } 

    if(description != null && !description.isEmpty()) { 
     holder.myPlikDeskription.setText(description); 
     holder.descriptionLayout.setVisibility(View.VISIBLE); 
    } 

    holder.myPlikTitle.setText(title); 
    holder.myPlikUserFromName.setText(name); 
    holder.myPlikUserFromNickname.setText(currentItem.myPlikUserFromNickname); 
} 

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

class MyViewHolder extends RecyclerView.ViewHolder { 
    NetworkImageView myPlikPhoto; 
    ImageView myPlikUserFromImg; 
    TextView myPlikUserFromName; 
    ImageView myPlikDeskriptionIcon; 
    TextView myPlikDeskription; 
    TextView myPlikUserFromNickname; 
    //TextView myPlikGetDate; 

    TextView myPlikTitle; 
    RelativeLayout descriptionLayout; 
    //RecyclerView recyclerView; 
    //GalleryAdapter adapter; 

    public MyViewHolder(View itemView) { 
     super(itemView); 
     myPlikTitle = (TextView) itemView.findViewById(R.id.myPlikTitle); 
     myPlikUserFromName = (TextView) itemView.findViewById(R.id.myPlikUserFromName); 
     myPlikUserFromNickname = (TextView) itemView.findViewById(R.id.myPlikUserFromNickname); 
     myPlikUserFromImg = (ImageView) itemView.findViewById(R.id.myPlikUserFromImg); 
     myPlikPhoto = (NetworkImageView) itemView.findViewById(R.id.profile_plick_content_img); 
     myPlikDeskription = (TextView) itemView.findViewById(R.id.profile_plick_content_description); 
     myPlikDeskriptionIcon = (ImageView) itemView.findViewById(R.id.profile_plik_content_comment_icon); 
     descriptionLayout = (RelativeLayout) itemView.findViewById(R.id.descripdion_block); 

     //adapter = new GalleryAdapter(inflater.getContext(), getImgIds()); 
     //recyclerView = (RecyclerView) itemView.findViewById(R.id.galleryList); 

    } 

} 
} 

回答

3

這個問題很複雜,所以我建議使用Picassohttp://square.github.io/picasso/)來處理圖像的問題。它處理ImageView回收並在適配器中下載取消。

另外還有一個官方指南,介紹如何在列表中加載圖像時處理併發問題:http://developer.android.com/training/displaying-bitmaps/process-bitmap.html。該解決方案基於AsyncTask,也適用。

對於OOM問題,嘗試在圖像加載到內存之前對圖像進行縮減採樣。選中此項:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

+0

謝謝,真的。 ) –

相關問題