我正在使用retrofit2来加入多个recyclerviews。它在一个recyclerview上效果很好,但是当我去排队另一个具有不同类别的视图时,它不起作用。
我试图做第二种onresponse方法,我想不出其他方法
Call<MovieResponse> call = apiInterface.getTopRatedMovies(API_KEY);//This one is working!
Call<MovieResponse> call1 = apiInterface.getNowPlayingMovies(API_KEY); //This is what i what to implement
这是代码
Call<MovieResponse> call = apiInterface.getTopRatedMovies(API_KEY);
call.enqueue(new Callback<MovieResponse>() {
@Override
public void onResponse(Call<MovieResponse> call, Response<MovieResponse> response) {
int statuscode =response.code();
List<Movie> movies = response.body().getResults();
recyclerView.setAdapter(new MoviesAdapter(movies, R.layout.movie_item, getContext()));
recyclerView1.setAdapter(new MoviesAdapter(movies, R.layout.movie_item, getContext())); //this works but not with the NowPlayingMovies, tested to see if it was the other recyclerview
}
@Override
public void onFailure(Call<MovieResponse> call, Throwable t) {
//Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
最佳答案
如果您不喜欢使用RxJava,则可以使用以下解决方案:
Call<MovieResponse> call = apiInterface.getTopRatedMovies(API_KEY);
call.enqueue(new Callback<MovieResponse>() {
@Override
public void onResponse(Call<MovieResponse> call, Response<MovieResponse> response) {
int statuscode =response.code();
List<Movie> movies = response.body().getResults();
recyclerView.setAdapter(new MoviesAdapter(movies, R.layout.movie_item, getContext()));
secondAPICall();
}
@Override
public void onFailure(Call<MovieResponse> call, Throwable t) {
//Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
public void secondAPICall(){
call1.enqueue(new Callback<MovieResponse>() {
@Override
public void onResponse(Call<MovieResponse> call, Response<MovieResponse> response) {
int statuscode =response.code();
List<Movie> movies = response.body().getResults();
recyclerView1.setAdapter(new MoviesAdapter(movies, R.layout.movie_item, getContext()));
}
@Override
public void onFailure(Call<MovieResponse> call, Throwable t) {
//Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}