[android]Picasso 只加载单个图像,但大小是 20

标签: Android
发布时间: 2016/10/10 22:21:29
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试图使 service 与改造和 RxJava 的调用。我使用的 Picasso 2.5.2 上传图像从 API 调用。我得到的图像显示,但我正在仅单个图像。当我 debug ,我得到大小为 20,但所显示的是单身。任何帮助的将是巨大的。

Adapter类︰

public class PopularMoviesAdapter extends RecyclerView.Adapter<PopularMoviesAdapter.MoviesViewHolder> {

private final String TAG = PopularMoviesAdapter.class.getSimpleName();

private Context context;
private List<MoviesResponse> movieItems = new ArrayList<>();

public PopularMoviesAdapter(Context context, List<MoviesResponse> movieItems) {
    this.context = context;
    this.movieItems = movieItems;
}

@Override
public MoviesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movies_grid_item, parent, false);
    return new MoviesViewHolder(view);
}

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

    List<Movies> movies = movieItems.get(position).getResults();
    String imageUrl = IntentKeys.MOVIES_POSTER_ENDPOINT + movies.get(position).getPosterPath();
    Log.d(TAG, "Poster URL from the API call: " + imageUrl);
    Picasso.with(context).load(imageUrl).into(holder.imageView);
    Glide.with(context).load(imageUrl).into(holder.imageView);

}

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

class MoviesViewHolder extends RecyclerView.ViewHolder {

    ImageView imageView;

    public MoviesViewHolder(View itemView) {
        super(itemView);

        imageView = (ImageView) itemView.findViewById(R.id.movies_grid_item_image);
    }
}}

fragment类︰

    public class PopularMoviesFragment extends Fragment {

    private static final int COLUMN_COUNT = 2;
    private List<MoviesResponse> responses;
    private RecyclerView recyclerView;
    private PopularMoviesAdapter popularMoviesAdapter;
    private Subscription subscription;
    private MoviesService service = RetrofitManager.getMoviesClient().create(MoviesService.class);

    public PopularMoviesFragment() {
        responses = new ArrayList<>();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_popular_movies, container, false);

        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(
                getActivity(),
                COLUMN_COUNT,
                LinearLayoutManager.VERTICAL,
                false);

        recyclerView = (RecyclerView) view.findViewById(R.id.popular_movies_recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(layoutManager);

        recyclerView.addItemDecoration(new GridSpacingItemDecoration(COLUMN_COUNT, dpToPx(10), true));
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        return view;
    }

    private int dpToPx(int dp) {
        Resources r = getResources();
        return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onStart() {
        super.onStart();
        getMoviesSubscription();
    }

    private void getMoviesSubscription() {
        subscription = service.getPopularMovies(IntentKeys.POPULAR_MOVIES_API_KEY)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        new Action1<MoviesResponse>() {
                            @Override
                            public void call(MoviesResponse movies) {
                                responses.add(movies);
                            }
                        }, new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {

                            }
                        }, new Action0() {
                            @Override
                            public void call() {
                                displayPosters();
                            }
                        }
                );
    }

    private void displayPosters() {
        popularMoviesAdapter = new PopularMoviesAdapter(getContext(), responses);
        recyclerView.setAdapter(popularMoviesAdapter);
        recyclerView.invalidate();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (subscription != null && subscription.isUnsubscribed()) {
            subscription.unsubscribe();
        }
    }

}

最后 layout :

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/movies_grid_item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/movies_grid_item_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"/>
</LinearLayout>

POJO:

  public class Movies implements Serializable {

    private String posterPath;
    private Boolean adult;
    private String overview;
    private String releaseDate;
    private List<Integer> genreIds = new ArrayList<Integer>();
    private Integer id;
    private String originalTitle;
    private String originalLanguage;
    private String title;
    private String backdropPath;
    private Float popularity;
    private Integer voteCount;
    private Boolean video;
    private Float voteAverage;

    // getters and setters ommitted and constructor
}


   public class MoviesResponse implements Serializable {
    private Integer page;
    private List<Movies> results = new ArrayList<Movies>();
    private Integer totalResults;
    private Integer totalPages;

    // getters and setters ommitted and constructor
}

enter image description here

enter image description here

enter image description here

谢谢你 !

解决方法 1:

尝试

Context context = holder.itemView.getContext();

在你的 onBindViewHolder,这样你不会 context 周围。

也尝试添加 popularMoviesAdapter.notifyDataSetChanged() 在你 fragment 。上传您的 POJO,你介意吗?

在你adapter

public PopularMoviesAdapter(List<Movie> movies) {
    this.movies = movies;
}

在你fragment

.subscribe(
                        new Action1<MoviesResponse>() {
                            @Override
                            public void call(MoviesResponse movies) {
                                responses.add(movies.getResults());
                                popularMoviesAdapter.notifyDataSetChanged()
                            }
                        }

变化

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

    List<Movies> movies = movieItems.get(position);
    String imageUrl = IntentKeys.MOVIES_POSTER_ENDPOINT + movies.get(position).getPosterPath();
    Log.d(TAG, "Poster URL from the API call: " + imageUrl);
    Picasso.with(context).load(imageUrl).into(holder.imageView);
    Glide.with(context).load(imageUrl).into(holder.imageView);

}
官方微信
官方QQ群
31647020