我正在尝试使用RecyclerView
和自定义GridLayoutManager
创建一个Adapter
,但是图像没有出现。我被困住了,不知道怎么了
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.Mo`viesHolder> {
Context context;
ArrayList<String> mImages;
ArrayList<String> mTitle ;
ArrayList<String> mReleaseDate;
ArrayList<String> mSynopsis;
ArrayList<Double> mAverageRating;
public MoviesAdapter(Context context, ArrayList<String> mImages, ArrayList<String> mTitle, ArrayList<String> mReleaseDate, ArrayList<String> mSynopsis, ArrayList<Double> mAverageRating) {
this.context = context;
this.mImages = mImages;
this.mTitle = mTitle;
this.mReleaseDate = mReleaseDate;
this.mSynopsis = mSynopsis;
this.mAverageRating = mAverageRating;
}
@NonNull
@Override
public MoviesHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater myInflater = LayoutInflater.from(context);
View movieView = myInflater.inflate(R.layout.movies_layout,parent,false);
return new MoviesHolder(movieView);
}
@Override
public void onBindViewHolder(@NonNull MoviesHolder holder, final int position) {
Glide.with(context)
.asBitmap()
.load(mImages.get(position))
.into(holder.movieImage);
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),MovieDetailsActivity.class);
intent.putExtra("movie_poster",mImages);
intent.putExtra("movie_title",mTitle);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return mImages.size();
}
public class MoviesHolder extends RecyclerView.ViewHolder {
ImageView movieImage;
RelativeLayout parentLayout;
String movieTitle;
String movieReleaseDate;
String movieSynopsis;
double movieAverageRating;
public MoviesHolder(@NonNull View itemView) {
super(itemView);
movieImage = itemView.findViewById(R.id.movie_img);
parentLayout = itemView.findViewById(R.id.parent_layout);
}
}
}
这是我的MainActivity
public class MainActivity extends AppCompatActivity {
RecyclerView moviesRecycler;
private ArrayList<String> mMoviePoster = new ArrayList<>();
private ArrayList<String> mMovieTitle = new ArrayList<>();
private ArrayList<String> mMovieReleaseDate = new ArrayList<>();
private ArrayList<String> mMovieSynopsis = new ArrayList<>();
private ArrayList<Double> mMovieAverageRating = new ArrayList<>();
MoviesAdapter myMoviesAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initMovieDetails();
moviesRecycler = findViewById(R.id.moviesRecycler);
myMoviesAdapter = new MoviesAdapter(this,mMoviePoster,mMovieTitle,mMovieReleaseDate,mMovieSynopsis,mMovieAverageRating);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2);
moviesRecycler.setAdapter(myMoviesAdapter);
moviesRecycler.setLayoutManager(gridLayoutManager);
}
private void initMovieDetails() {
mMoviePoster.add("https://unsplash.com/photos/NhUJuPUeRvc");
mMovieTitle.add("Parasite");
mMovieReleaseDate.add("2019");
mMovieAverageRating.add(8.6);
mMovieSynopsis.add("A poor family, the Kims, con their way into becoming the servants of a rich family, the Parks. But their easy life gets complicated when their deception is threatened with exposure.\n");
mMoviePoster.add("https://www.imdb.com/title/tt8946378/mediaviewer/rm2569376769");
mMovieTitle.add("Knives Out");
mMovieReleaseDate.add("2019");
mMovieAverageRating.add(8.0);
mMovieSynopsis.add("A detective investigates the death of a patriarch of an eccentric, combative family.\n");
mMoviePoster.add("https://www.imdb.com/title/tt1051906/mediaviewer/rm1847823873");
mMovieTitle.add("The Invisible Man");
mMovieReleaseDate.add("2019");
mMovieAverageRating.add(7.6);
mMovieSynopsis.add("When Cecilia's abusive ex takes his own life and leaves her his fortune, she suspects his death was a hoax. As a series of coincidences turn lethal, Cecilia works to prove that she is being hunted by someone nobody can see.\n");
mMoviePoster.add("https://www.imdb.com/title/tt7286456/mediaviewer/rm3353122305");
mMovieTitle.add("Joker");
mMovieReleaseDate.add("2019");
mMovieAverageRating.add(8.6);
mMovieSynopsis.add("In Gotham City, mentally troubled comedian Arthur Fleck is disregarded and mistreated by society. He then embarks on a downward spiral of revolution and bloody crime. This path brings him face-to-face with his alter-ego: the Joker.\n");
mMoviePoster.add("https://www.imdb.com/title/tt2584384/mediaviewer/rm3972044545");
mMovieTitle.add("JoJo Rabbit");
mMovieReleaseDate.add("2019");
mMovieAverageRating.add(8.0);
mMovieSynopsis.add("A young boy in Hitler's army finds out his mother is hiding a Jewish girl in their home.\n");
mMoviePoster.add("https://www.imdb.com/title/tt8579674/mediaviewer/rm144738817");
mMovieTitle.add("1917");
mMovieReleaseDate.add("2019");
mMovieAverageRating.add(8.4);
mMovieSynopsis.add("April 6th, 1917. As a regiment assembles to wage war deep in enemy territory, two soldiers are assigned to race against time and deliver a message that will stop 1,600 men from walking straight into a deadly trap.\n");
}
}
答案 0 :(得分:0)
我认为您要分配给Glide的网址有问题。这些链接是HTML / CSS等等,您应该为Glide分配图像本身。 例如,尝试用以下URL替换所有URL: https://m.media-amazon.com/images/M/MV5BZjU0Yzk2MzEtMjAzYy00MzY0LTg2YmItM2RkNzdkY2ZhN2JkXkEyXkFqcGdeQXVyNDg4NjY5OTQ@._V1_SY1000_SX667_AL_.jpg
此URL为.jpg扩展名。我相信它应该可以工作,请尝试一下。
答案 1 :(得分:0)
我认为您应该移动这行 filmsRecycler.setLayoutManager(gridLayoutManager);在之上 filmsRecycler.setAdapter(myMoviesAdapter);
最好使用模型类