毕加索只加载单张图片但尺寸为20

时间:2016-09-17 02:14:49

标签: android web-services picasso retrofit2 image-loading

我正在尝试使用Retrofit和RxJava进行服务调用。我正在使用Picasso 2.5.2从API调用上传图像。我正在显示图像,但我只获得单张图像。当我调试时,我的大小为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);
    }
}}

片段类:

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

}

最后布局:

    <?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

谢谢!

2 个答案:

答案 0 :(得分:1)

尝试

Context context = holder.itemView.getContext();

在你的onBindViewHolder中

,这样你就不会传递上下文。

还可以尝试在片段中添加popularMoviesAdapter.notifyDataSetChanged()。你介意上传你的POJO吗?

适配器中的

public PopularMoviesAdapter(List<Movie> movies) {
    this.movies = movies;
}
片段中的

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

}

答案 1 :(得分:0)

这是设置适配器的代码

popularMoviesAdapter = new PopularMoviesAdapter(getContext(), responses); recyclerView.setAdapter(popularMoviesAdapter);

你的 ArrayList的响应=新的ArrayList() //这是你提供给适配器的arraylist

调试并检查响应的大小,我认为它将在一个对象上返回多少条记录 我认为你的数组有点像这样

responses [{
    0,{

     } 
}];

所以传递

 popularMoviesAdapter = new PopularMoviesAdapter(getContext(), responses.get(0).getResults
); recyclerView.setAdapter(popularMoviesAdapter);