为了账号安全,请及时绑定邮箱和手机立即绑定

如何在电影数据库应用程序的 RecyclerView 中实现 getItemViewType()

如何在电影数据库应用程序的 RecyclerView 中实现 getItemViewType()

牧羊人nacy 2024-01-17 17:02:12
我正在使用 TMDB api、Retrofit、Gson 和 Glide 创建一个电影应用程序。我有两个 recyclerView 和两个要充气的布局。但我无法在 recyclerView 适配器中膨胀 2 个布局。我已经在 2 个不同的 recyclerView 中实现了流行和即将上映的电影列表。但他们使用 1 个单一布局进行展示。我想在一种布局中夸大流行电影,在另一种布局中夸大即将上映的电影。我无法设置 getItemViewType() 方法的条件。如何在 getItemViewType() 方法中检查热门和即将上映的电影列表,并在 recyclerView 的 onCreateViewHolder() 方法上实现它。电影适配器类:public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MovieViewHolder> {    private Context context;    private ArrayList<Movie> movieArrayList;    public MovieAdapter(Context context, ArrayList<Movie> movieArrayList) {        this.context = context;        this.movieArrayList = movieArrayList;    }    @NonNull    @Override    public MovieViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_list_item, parent, false);        return new MovieViewHolder(view);    }    @Override    public int getItemViewType(int position) {        return super.getItemViewType(position);    }    @Override    // Set values to the list item components    public void onBindViewHolder(@NonNull MovieViewHolder holder, int position) {        holder.movieTitle.setText(movieArrayList.get(position).getOriginalTitle());        holder.rating.setText(String.valueOf(movieArrayList.get(position).getVoteAverage()));        String imagePath = "https://image.tmdb.org/t/p/w500" + movieArrayList.get(position).getPosterPath();        Glide.with(context)                .load(imagePath)                .placeholder(R.drawable.loading)    }}我想在 onCreateViewHolder() 方法中膨胀 2 个不同的布局“movie_list_item.xml”和“upcoming_movie_list_item.xml”。
查看完整描述

2 回答

?
MMMHUHU

TA贡献1834条经验 获得超8个赞

在你的适配器类(MovieAdapter)中创建一个新的构造函数并添加 Int 或 enum 的额外参数,无论对你来说多么简单,我只是给你简单的例子:-


public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MovieViewHolder> {

    private Context context;

    private ArrayList<Movie> movieArrayList;

    private int viewType;


        public MovieAdapter(Context context, ArrayList < Movie > movieArrayList, int viewType) {

        this.context = context;

        this.movieArrayList = movieArrayList;

        this.viewType=viewType;

    }


    @NonNull

    @Override

    public MovieViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view;

        if (viewType == 1) {

            //Popular movie layout

            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_list_item, parent, false); 

        } else {

            //upcoming movie layout

            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_list_item, parent, false);

        }


        return new MovieViewHolder(view);

    }


    @Override

    public int getItemViewType(int position) {

        if (viewType == 1)

            return 1; //Popular Movie Layout

        else

            return 2; //Upcoming Movie Layout

        //    return super.getItemViewType(position);

    }


    @Override

    // Set values to the list item components

    public void onBindViewHolder(@NonNull MovieViewHolder holder, int position) {

        holder.movieTitle.setText(movieArrayList.get(position).getOriginalTitle());

        holder.rating.setText(String.valueOf(movieArrayList.get(position).getVoteAverage()));

        String imagePath = "https://image.tmdb.org/t/p/w500" + movieArrayList.get(position).getPosterPath();


        Glide.with(context)

            .load(imagePath)

            .placeholder(R.drawable.loading)

            .into(holder.movieImage);

    }


    @Override

    public int getItemCount() {

        return movieArrayList == null ? 0 : movieArrayList.size();

    }


    public class MovieViewHolder extends RecyclerView.ViewHolder {


        TextView movieTitle, rating;

        ImageView movieImage;


        public MovieViewHolder(@NonNull View itemView) {

            super(itemView);


            movieImage = itemView.findViewById(R.id.ivMovieImage);

            movieTitle = itemView.findViewById(R.id.tvTitle);

            rating = itemView.findViewById(R.id.tvRating);


            itemView.setOnClickListener(new View.OnClickListener() {

                @Override

                public void onClick(View view) {

                    int position = getAdapterPosition();


                    if (position != RecyclerView.NO_POSITION) {

                        Movie selectedMovie = movieArrayList.get(position);


                        Intent intent = new Intent(context, MovieActivity.class);

                        intent.putExtra("movie", selectedMovie);

                        context.startActivity(intent);

                    }

                }

            });

        }

    }

并且仅在您的活动中更改此方法


    private void showOnRecyclerView() {

    recyclerViewPopular = findViewById(R.id.rvMovies);

    recyclerViewUpcoming = findViewById(R.id.rvTopMovies);


    RecyclerView.LayoutManager popularLayoutManager = new LinearLayoutManager(this);

    RecyclerView.LayoutManager upcomingLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);


    recyclerViewUpcoming.setLayoutManager(upcomingLayoutManager);

    recyclerViewPopular.setLayoutManager(popularLayoutManager);



    movieAdapter = new MovieAdapter(this, popularMovie,1);

    upcomingAdapter = new MovieAdapter(this, topRatedMovie,2);



    if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {

        recyclerViewPopular.setLayoutManager(new GridLayoutManager(this, 2));


    } else {

        recyclerViewPopular.setLayoutManager(new GridLayoutManager(this, 4));

    }


    recyclerViewPopular.setItemAnimator(new DefaultItemAnimator());

    recyclerViewUpcoming.setItemAnimator(new DefaultItemAnimator());


    recyclerViewPopular.setAdapter(movieAdapter);

    recyclerViewUpcoming.setAdapter(upcomingAdapter);


    movieAdapter.notifyDataSetChanged();

    upcomingAdapter.notifyDataSetChanged();

}


查看完整回答
反对 回复 2024-01-17
?
HUX布斯

TA贡献1876条经验 获得超6个赞

为 movie_empty_item 创建布局


public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MovieViewHolder> {

        private Context context;

        private ArrayList<Movie> movieArrayList;


        //add this two line

        private static final int EMPTY_VIEW_TYPE = 0;

        private static final int NORMAL_VIEW_TYPE = 1;


        public MovieAdapter(Context context, ArrayList<Movie> movieArrayList) {

            this.context = context;

            this.movieArrayList = movieArrayList;

        }


        @NonNull

        @Override

        public MovieViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {


            //return viewholder replace like this

            if(viewType == NORMAL_VIEW_TYPE) {

                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_list_item, parent, false);

                return new MovieViewHolder(view);

            }else {

                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_empty_item, parent, false);

                return new EmptyViewHolder(view); 

            };

        }


       //getItemViewType return replace like this

        @Override

        public int getItemViewType(int position) {

            return movieArrayList.size()>0?NORMAL_VIEW_TYPE:EMPTY_VIEW_TYPE;

        }


        @Override

        // Set values to the list item components

        public void onBindViewHolder(@NonNull MovieViewHolder holder, int position) {

            holder.movieTitle.setText(movieArrayList.get(position).getOriginalTitle());

            holder.rating.setText(String.valueOf(movieArrayList.get(position).getVoteAverage()));

            String imagePath = "https://image.tmdb.org/t/p/w500" + movieArrayList.get(position).getPosterPath();


            Glide.with(context)

                    .load(imagePath)

                    .placeholder(R.drawable.loading)

                    .into(holder.movieImage);

        }


        @Override

        public int getItemCount() {

            return movieArrayList.size() ? movieArrayList.size():1 ;

        }


        public class MovieViewHolder extends RecyclerView.ViewHolder {


            TextView movieTitle, rating;

            ImageView movieImage;


            public MovieViewHolder(@NonNull View itemView) {

                super(itemView);


                movieImage = itemView.findViewById(R.id.ivMovieImage);

                movieTitle = itemView.findViewById(R.id.tvTitle);

                rating = itemView.findViewById(R.id.tvRating);


                itemView.setOnClickListener(new View.OnClickListener() {

                    @Override

                    public void onClick(View view) {

                        int position = getAdapterPosition();


                        if (position != RecyclerView.NO_POSITION) {

                            Movie selectedMovie = movieArrayList.get(position);


                            Intent intent = new Intent(context, MovieActivity.class);

                            intent.putExtra("movie", selectedMovie);

                            context.startActivity(intent);

                        }

                    }

                });

            }

        }



        public class EmptyViewHolder extends RecyclerView.ViewHolder {



            public EmptyViewHolder(@NonNull View itemView) {

                super(itemView);


            }

        }


    }


查看完整回答
反对 回复 2024-01-17
  • 2 回答
  • 0 关注
  • 114 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信