其他分享
首页 > 其他分享> > android-如何将Picasso与RecyclerView的自定义适配器一起使用

android-如何将Picasso与RecyclerView的自定义适配器一起使用

作者:互联网

我正在用从Web加载的图像填充RecyclerView.我可以在适配器内使用AsyncTask加载图像.但是,由于我需要用毕加索实现它,因此需要帮助.这是到目前为止的代码:

    public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MovieViewHolder>
{
    Bitmap mBitmap;
    int pos;
    public static class MovieViewHolder extends RecyclerView.ViewHolder
    {
         CardView cv;
        TextView MovieName;
         ImageView MoviePhoto;

        MovieViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.cv);
            MovieName = (TextView)itemView.findViewById(R.id.movie_name);
            MoviePhoto = (ImageView)itemView.findViewById(R.id.movie_photo);
        }
    }

    List<Post> mpost;

    CustomAdapter(List<Post> mpost){
        this.mpost = mpost;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public MovieViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        MovieViewHolder pvh = new MovieViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(MovieViewHolder movieViewHolder, int i)
    {
        pos=i;
        movieViewHolder.MovieName.setText(mpost.get(i).getTitle());

        if(mpost.get(pos).getPoster_path()!=null)
        {
            new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {
                    try {
                        URL url = new URL("http://image.tmdb.org/t/p/w154"+mpost.get(pos).getPoster_path());
                        mBitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());

                    } catch (MalformedURLException e) {

                    } catch (IOException e) {

                    }
                    return null;
                }
            }.execute();




            movieViewHolder.MoviePhoto.setImageBitmap(mBitmap);

        }




    }

    @Override
    public int getItemCount()
    {
        if(mpost!=null)
        {
            return mpost.size();
        }
        else
        {
            return 0;
        }
    }

}

我需要替换为:

 new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    URL url = new URL("http://image.tmdb.org/t/p/w154"+mpost.get(pos).getPoster_path());
                    mBitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());

                } catch (MalformedURLException e) {

                } catch (IOException e) {

                }
                return null;
            }
        }.execute();




        movieViewHolder.MoviePhoto.setImageBitmap(mBitmap);

与毕加索:

Picasso.with(this)
                .load("http://image.tmdb.org/t/p/w154" + mpost.get(pos).getPoster_path())
                .into(MoviePhoto);

但是,这样做似乎有错误,最好的解决方法是什么?

解决方法:

您可以在onBindViewHolder方法RecyclerView.Adapter中使用以下毕加索代码

@Override
public void onBindViewHolder(MovieViewHolder movieViewHolder, int position){


    Post model = mpost.get(position);    

    movieViewHolder.MovieName.setText(model.getTitle());
    Picasso.with(getContext()).load("http://image.tmdb.org/t/p/w154" + mpost.get(pos).getPoster_path()).into(movieViewHolder.MoviePhoto)

}

标签:picasso,android
来源: https://codeday.me/bug/20191119/2034874.html