第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在電影數(shù)據(jù)庫應(yīng)用程序的 RecyclerView 中實(shí)現(xiàn) getItemViewType()

如何在電影數(shù)據(jù)庫應(yīng)用程序的 RecyclerView 中實(shí)現(xiàn) getItemViewType()

牧羊人nacy 2024-01-17 17:02:12
我正在使用 TMDB api、Retrofit、Gson 和 Glide 創(chuàng)建一個(gè)電影應(yīng)用程序。我有兩個(gè) recyclerView 和兩個(gè)要充氣的布局。但我無法在 recyclerView 適配器中膨脹 2 個(gè)布局。我已經(jīng)在 2 個(gè)不同的 recyclerView 中實(shí)現(xiàn)了流行和即將上映的電影列表。但他們使用 1 個(gè)單一布局進(jìn)行展示。我想在一種布局中夸大流行電影,在另一種布局中夸大即將上映的電影。我無法設(shè)置 getItemViewType() 方法的條件。如何在 getItemViewType() 方法中檢查熱門和即將上映的電影列表,并在 recyclerView 的 onCreateViewHolder() 方法上實(shí)現(xiàn)它。電影適配器類: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 個(gè)不同的布局“movie_list_item.xml”和“upcoming_movie_list_item.xml”。
查看完整描述

2 回答

?
MMMHUHU

TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊

在你的適配器類(MovieAdapter)中創(chuàng)建一個(gè)新的構(gòu)造函數(shù)并添加 Int 或 enum 的額外參數(shù),無論對(duì)你來說多么簡(jiǎn)單,我只是給你簡(jiǎn)單的例子:-


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);

                    }

                }

            });

        }

    }

并且僅在您的活動(dòng)中更改此方法


    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();

}


查看完整回答
反對(duì) 回復(fù) 2024-01-17
?
HUX布斯

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個(gè)贊

為 movie_empty_item 創(chuàng)建布局


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);


            }

        }


    }


查看完整回答
反對(duì) 回復(fù) 2024-01-17
  • 2 回答
  • 0 關(guān)注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)