在Android中,RecyclerView在重新创建后没有显示任何内容

时间:2015-07-15 09:39:14

标签: android android-recyclerview

所以我有这个详情活动,我在底部有RecyclerView并加载“相关电影”。

adapter = new CustomRecycleAdapter(getRelatedMovies());
adapter.notifyDataSetChanged();

我正在使用Retrofit API加载数据。这是我的getRelatedMovies()函数:

ArrayList<RelatedMovies> items;
private ArrayList<RelatedMovies> getRelatedMovies() {
    items = new ArrayList<>();
    Plugin.api.GetPackageVideos(publisher_id, package_id, getIntent().getStringExtra("movieCategory"), 1 + "", "asc", new Callback<MovieResults>() {
        @Override
        public void success(MovieResults movieResults, Response response) {
            Videos[] vids = movieResults.getResponse().getResult().getVideos();
            for (int i = 0; i < vids.length; i++) {
                items.add(new RelatedMovies(vids[i].getTitle(), vids[i].getCustom_attributes().getGenres_en().split(",")[0], vids[i].getCustom_attributes().getPromoImage()));
            }
        }

        @Override
        public void failure(RetrofitError error) {
            Log.e("ERROR", error.toString());
        }
    });
    return items;
}

所以这就是问题所在:当我回到主屏幕搜索另一部电影,并使用该电影的ID输入详细信息活动时,RecyclerView没有显示任何内容,只是我第一次打开看电影。这是我RecyclerAdapter的代码:

public class CustomRecycleAdapter extends RecyclerView.Adapter<CustomRecycleAdapter.ViewHolder> {

    ArrayList<RelatedMovies> relatedMovies;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tvRelatedMovieTitle, tvRelatedMoviePrice;
        public ImageView ivRelatedMoviePoster;
        public CardView cv;

        public ViewHolder(View itemView) {
            super(itemView);
            this.tvRelatedMovieTitle = (TextView) itemView.findViewById(R.id.tvRelatedMovieTitle);
            this.tvRelatedMoviePrice = (TextView) itemView.findViewById(R.id.tvRelatedMoviePrice);
            this.ivRelatedMoviePoster = (ImageView) itemView.findViewById(R.id.ivRelatedMoviePoster);
            this.cv = (CardView) itemView.findViewById(R.id.cv);
        }
    }

    public CustomRecycleAdapter(ArrayList<RelatedMovies> relatedMovies) {
        this.relatedMovies = new ArrayList<RelatedMovies>();
        this.relatedMovies = relatedMovies;
    }

    @Override
    public CustomRecycleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.recycle_view_cards, parent, false);
        return new CustomRecycleAdapter.ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final CustomRecycleAdapter.ViewHolder holder, int position) {
        RelatedMovies movie = relatedMovies.get(position);
        holder.tvRelatedMovieTitle.setText(movie.movieTitle);
        holder.tvRelatedMoviePrice.setText(movie.moviePrice);
        Glide.with(holder.ivRelatedMoviePoster.getContext())
                .load(movie.moviePosterUrl)
                .asBitmap()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .fitCenter()
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        holder.ivRelatedMoviePoster.setImageBitmap(resource);
                    }
                });
    }

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

知道为什么会这样吗?

0 个答案:

没有答案