猿问

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

我正在使用 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”。



牧羊人nacy
浏览 102回答 2
2回答

MMMHUHU

在你的适配器类(MovieAdapter)中创建一个新的构造函数并添加 Int 或 enum 的额外参数,无论对你来说多么简单,我只是给你简单的例子:-public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MovieViewHolder> {&nbsp; &nbsp; private Context context;&nbsp; &nbsp; private ArrayList<Movie> movieArrayList;&nbsp; &nbsp; private int viewType;&nbsp; &nbsp; &nbsp; &nbsp; public MovieAdapter(Context context, ArrayList < Movie > movieArrayList, int viewType) {&nbsp; &nbsp; &nbsp; &nbsp; this.context = context;&nbsp; &nbsp; &nbsp; &nbsp; this.movieArrayList = movieArrayList;&nbsp; &nbsp; &nbsp; &nbsp; this.viewType=viewType;&nbsp; &nbsp; }&nbsp; &nbsp; @NonNull&nbsp; &nbsp; @Override&nbsp; &nbsp; public MovieViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {&nbsp; &nbsp; &nbsp; &nbsp; View view;&nbsp; &nbsp; &nbsp; &nbsp; if (viewType == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Popular movie layout&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_list_item, parent, false);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //upcoming movie layout&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_list_item, parent, false);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return new MovieViewHolder(view);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getItemViewType(int position) {&nbsp; &nbsp; &nbsp; &nbsp; if (viewType == 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1; //Popular Movie Layout&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 2; //Upcoming Movie Layout&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; return super.getItemViewType(position);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; // Set values to the list item components&nbsp; &nbsp; public void onBindViewHolder(@NonNull MovieViewHolder holder, int position) {&nbsp; &nbsp; &nbsp; &nbsp; holder.movieTitle.setText(movieArrayList.get(position).getOriginalTitle());&nbsp; &nbsp; &nbsp; &nbsp; holder.rating.setText(String.valueOf(movieArrayList.get(position).getVoteAverage()));&nbsp; &nbsp; &nbsp; &nbsp; String imagePath = "https://image.tmdb.org/t/p/w500" + movieArrayList.get(position).getPosterPath();&nbsp; &nbsp; &nbsp; &nbsp; Glide.with(context)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .load(imagePath)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .placeholder(R.drawable.loading)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .into(holder.movieImage);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getItemCount() {&nbsp; &nbsp; &nbsp; &nbsp; return movieArrayList == null ? 0 : movieArrayList.size();&nbsp; &nbsp; }&nbsp; &nbsp; public class MovieViewHolder extends RecyclerView.ViewHolder {&nbsp; &nbsp; &nbsp; &nbsp; TextView movieTitle, rating;&nbsp; &nbsp; &nbsp; &nbsp; ImageView movieImage;&nbsp; &nbsp; &nbsp; &nbsp; public MovieViewHolder(@NonNull View itemView) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; movieImage = itemView.findViewById(R.id.ivMovieImage);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; movieTitle = itemView.findViewById(R.id.tvTitle);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rating = itemView.findViewById(R.id.tvRating);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; itemView.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int position = getAdapterPosition();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (position != RecyclerView.NO_POSITION) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Movie selectedMovie = movieArrayList.get(position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(context, MovieActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra("movie", selectedMovie);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }并且仅在您的活动中更改此方法&nbsp; &nbsp; private void showOnRecyclerView() {&nbsp; &nbsp; recyclerViewPopular = findViewById(R.id.rvMovies);&nbsp; &nbsp; recyclerViewUpcoming = findViewById(R.id.rvTopMovies);&nbsp; &nbsp; RecyclerView.LayoutManager popularLayoutManager = new LinearLayoutManager(this);&nbsp; &nbsp; RecyclerView.LayoutManager upcomingLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);&nbsp; &nbsp; recyclerViewUpcoming.setLayoutManager(upcomingLayoutManager);&nbsp; &nbsp; recyclerViewPopular.setLayoutManager(popularLayoutManager);&nbsp; &nbsp; movieAdapter = new MovieAdapter(this, popularMovie,1);&nbsp; &nbsp; upcomingAdapter = new MovieAdapter(this, topRatedMovie,2);&nbsp; &nbsp; if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {&nbsp; &nbsp; &nbsp; &nbsp; recyclerViewPopular.setLayoutManager(new GridLayoutManager(this, 2));&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; recyclerViewPopular.setLayoutManager(new GridLayoutManager(this, 4));&nbsp; &nbsp; }&nbsp; &nbsp; recyclerViewPopular.setItemAnimator(new DefaultItemAnimator());&nbsp; &nbsp; recyclerViewUpcoming.setItemAnimator(new DefaultItemAnimator());&nbsp; &nbsp; recyclerViewPopular.setAdapter(movieAdapter);&nbsp; &nbsp; recyclerViewUpcoming.setAdapter(upcomingAdapter);&nbsp; &nbsp; movieAdapter.notifyDataSetChanged();&nbsp; &nbsp; upcomingAdapter.notifyDataSetChanged();}

HUX布斯

为 movie_empty_item 创建布局public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MovieViewHolder> {&nbsp; &nbsp; &nbsp; &nbsp; private Context context;&nbsp; &nbsp; &nbsp; &nbsp; private ArrayList<Movie> movieArrayList;&nbsp; &nbsp; &nbsp; &nbsp; //add this two line&nbsp; &nbsp; &nbsp; &nbsp; private static final int EMPTY_VIEW_TYPE = 0;&nbsp; &nbsp; &nbsp; &nbsp; private static final int NORMAL_VIEW_TYPE = 1;&nbsp; &nbsp; &nbsp; &nbsp; public MovieAdapter(Context context, ArrayList<Movie> movieArrayList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.context = context;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.movieArrayList = movieArrayList;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @NonNull&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public MovieViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //return viewholder replace like this&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(viewType == NORMAL_VIEW_TYPE) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_list_item, parent, false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new MovieViewHolder(view);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_empty_item, parent, false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new EmptyViewHolder(view);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;//getItemViewType return replace like this&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public int getItemViewType(int position) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return movieArrayList.size()>0?NORMAL_VIEW_TYPE:EMPTY_VIEW_TYPE;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; // Set values to the list item components&nbsp; &nbsp; &nbsp; &nbsp; public void onBindViewHolder(@NonNull MovieViewHolder holder, int position) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.movieTitle.setText(movieArrayList.get(position).getOriginalTitle());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.rating.setText(String.valueOf(movieArrayList.get(position).getVoteAverage()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String imagePath = "https://image.tmdb.org/t/p/w500" + movieArrayList.get(position).getPosterPath();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Glide.with(context)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .load(imagePath)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .placeholder(R.drawable.loading)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .into(holder.movieImage);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public int getItemCount() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return movieArrayList.size() ? movieArrayList.size():1 ;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public class MovieViewHolder extends RecyclerView.ViewHolder {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextView movieTitle, rating;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ImageView movieImage;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public MovieViewHolder(@NonNull View itemView) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; movieImage = itemView.findViewById(R.id.ivMovieImage);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; movieTitle = itemView.findViewById(R.id.tvTitle);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rating = itemView.findViewById(R.id.tvRating);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; itemView.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int position = getAdapterPosition();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (position != RecyclerView.NO_POSITION) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Movie selectedMovie = movieArrayList.get(position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(context, MovieActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra("movie", selectedMovie);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public class EmptyViewHolder extends RecyclerView.ViewHolder {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public EmptyViewHolder(@NonNull View itemView) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答