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

标签 android picasso

我正在使用从网络加载的图像填充 RecyclerView。我可以在我的适配器中使用 AsyncTask 加载图像。但是,由于我需要用 Picasso 来实现它,所以我需要帮助。这是到目前为止的代码:

    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 :

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

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

最佳答案

@Override
public void onBindViewHolder(MovieViewHolder movieViewHolder, int i){
    pos=i;
    movieViewHolder.MovieName.setText(mpost.get(i).getTitle());
    if(mpost.get(pos).getPoster_path()!=null){
        Picasso.with(getContext()).load("http://image.tmdb.org/t/p/w154" + mpost.get(pos).getPoster_path()).into(movieViewHolder.MoviePhoto)
    }
}

关于android - 如何将 Picasso 与 RecyclerView 的自定义适配器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34395777/

相关文章:

android - 在自定义复合控件中使用时,充气机会默默地失败

android - 在 picasso 中使用回调获取图像?

android - RecyclerView 适配器没有膨胀 xml 布局

java - Picasso 在 Android 10 中不工作。为什么?

java - ImageView 未根据 onActivityResult 中的更改进行更新

android - 为什么在 Picasso.with(context) 中,Picasso 要求提供上下文?

android - TabLayout 上方的页眉布局

Android 菜单项在 'swiping' 之后不可点击

java - Android 通知无法访问接收到的字符串

Android Mapbox 地理编码