使用加载图像创建RecyclerView的正确方法是什么?

时间:2015-10-03 15:32:51

标签: android json android-recyclerview recycler-adapter

我正在尝试使用从服务器下载图像的片段创建RecyclerView,但我遇到了一些问题: 当滚动到快速,你可以看到以前的图片,我真的不想要这个。第二件事是我有一些OOM崩溃,我真的不知道该怎么做,一些更有经验的程序员可以帮助我吗?

我的片段适配器代码:

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);

    }

}
}

1 个答案:

答案 0 :(得分: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