Android开发 - Movie 类存储电影信息与使用详解

基本结构

  • Movie类应该包含电影的基本信息。假设我们需要存储以下信息

    • 电影标题(title)

    • 电影描述(description)

    • 发布年份(releaseYear)

    • 评分(rating)

    • 海报URL(posterUrl)

  • 我们将为这些属性创建一个

创建Movie类

  • 首先定义Movie类并添加相应的属性构造方法getter/setter方法

    public class Movie {
        // 电影标题
        private String title;
        // 电影描述
        private String description;
        // 发布年份
        private int releaseYear;
        // 评分
        private double rating;
        // 海报URL
        private String posterUrl;
    
        // 构造方法,用于初始化Movie对象
        public Movie(String title, String description, int releaseYear, double rating, String posterUrl) {
            this.title = title; // 设置电影标题
            this.description = description; // 设置电影描述
            this.releaseYear = releaseYear; // 设置发布年份
            this.rating = rating; // 设置评分
            this.posterUrl = posterUrl; // 设置海报URL
        }
    
        // 获取电影标题
        public String getTitle() {
            return title;
        }
    
        // 设置电影标题
        public void setTitle(String title) {
            this.title = title;
        }
    
        // 获取电影描述
        public String getDescription() {
            return description;
        }
    
        // 设置电影描述
        public void setDescription(String description) {
            this.description = description;
        }
    
        // 获取发布年份
        public int getReleaseYear() {
            return releaseYear;
        }
    
        // 设置发布年份
        public void setReleaseYear(int releaseYear) {
            this.releaseYear = releaseYear;
        }
    
        // 获取评分
        public double getRating() {
            return rating;
        }
    
        // 设置评分
        public void setRating(double rating) {
            this.rating = rating;
        }
    
        // 获取海报URL
        public String getPosterUrl() {
            return posterUrl;
        }
    
        // 设置海报URL
        public void setPosterUrl(String posterUrl) {
            this.posterUrl = posterUrl;
        }
    }
    

Movie类的使用

  • 创建 Movie 对象的实例以及设置其属性

    // 创建一个Movie对象实例
    Movie inception = new Movie(
        "Inception", // 电影标题
        "A thief who steals corporate secrets through the use of dream-sharing technology is given the inverse task of planting an idea into the mind of a C.E.O.", // 电影描述
        2010, // 发布年份
        8.8, // 评分
        "https://example.com/inception_poster.jpg" // 海报URL
    );
    
  • 显示电影信息

    • 在实际的 Android 应用中,我们通常会从 API 获取电影信息,并将这些信息显示在 UI 上。以下是一个简单的示例,演示如何在 RecyclerView 中显示电影列表

      • 创建主布局文件定义一个 RecyclerView 用于显示电影列表res/layout/activity_main.xml

        <!-- res/layout/activity_main.xml -->
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
        
            <!-- 定义一个RecyclerView,用于显示电影列表 -->
            <RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="16dp"
                android:scrollbars="vertical" />
        </LinearLayout>
        
      • 创建电影项布局文件定义每个电影项在 RecyclerView 中的显示方式res/layout/item_movie.xml

        <!-- res/layout/item_movie.xml -->
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">
        
            <ImageView
                android:id="@+id/posterImageView"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:scaleType="centerCrop" />
        
            <TextView
                android:id="@+id/titleTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:textStyle="bold"
                android:paddingTop="8dp" />
        
            <TextView
                android:id="@+id/descriptionTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:paddingTop="4dp" />
        
            <TextView
                android:id="@+id/releaseYearTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="12sp"
                android:paddingTop="4dp" />
        
            <TextView
                android:id="@+id/ratingTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="12sp"
                android:paddingTop="4dp" />
        </LinearLayout>
        
      • 创建 RecyclerView.Adapter 类来绑定 Movie 数据MovieAdapter.java

        public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MovieViewHolder> {
            // 电影列表
            private List<Movie> movieList;
        
            // 构造方法,传入电影列表
            public MovieAdapter(List<Movie> movieList) {
                this.movieList = movieList; // 初始化电影列表
            }
        
            // 创建 ViewHolder
            @Override
            public MovieViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                // 加载电影项布局
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_movie, parent, false);
                return new MovieViewHolder(view); // 创建并返回 ViewHolder
            }
        
            // 绑定 ViewHolder
            @Override
            public void onBindViewHolder(MovieViewHolder holder, int position) {
                Movie movie = movieList.get(position); // 获取当前电影
                holder.bind(movie); // 绑定电影数据到 ViewHolder
            }
        
            // 获取电影列表大小
            @Override
            public int getItemCount() {
                return movieList.size();
            }
            // 以上设定了形参的传参规则
        
            // 以下自定义 ViewHolder 类:包含具体的实现方法
            public static class MovieViewHolder extends RecyclerView.ViewHolder {
                // 定义 UI 元素
                private TextView titleTextView;
                private TextView descriptionTextView;
                private TextView releaseYearTextView;
                private TextView ratingTextView;
                private ImageView posterImageView;
        
                // 构造方法,初始化 UI 元素
                public MovieViewHolder(View itemView) {
                    super(itemView);
                    titleTextView = itemView.findViewById(R.id.titleTextView); // 电影标题 TextView
                    descriptionTextView = itemView.findViewById(R.id.descriptionTextView); // 电影描述 TextView
                    releaseYearTextView = itemView.findViewById(R.id.releaseYearTextView); // 发布年份 TextView
                    ratingTextView = itemView.findViewById(R.id.ratingTextView); // 评分 TextView
                    posterImageView = itemView.findViewById(R.id.posterImageView); // 海报 ImageView
                }
        
                // 绑定电影数据到 UI 元素
                public void bind(Movie movie) {
                    titleTextView.setText(movie.getTitle()); // 设置电影标题
                    descriptionTextView.setText(movie.getDescription()); // 设置电影描述
                    releaseYearTextView.setText(String.valueOf(movie.getReleaseYear())); // 设置发布年份
                    ratingTextView.setText(String.valueOf(movie.getRating())); // 设置评分
                    // 加载海报图片,可以使用 Glide 或 Picasso 库
                    Glide.with(itemView.getContext()).load(movie.getPosterUrl()).into(posterImageView); // 加载海报图片
                }
            }
        }
        
      • 启动类中设置 RecyclerView:在 ActivityFragment设置 RecyclerView,并绑定 MovieAdapter

        public class MainActivity extends AppCompatActivity {
            // RecyclerView 对象
            private RecyclerView recyclerView;
            // MovieAdapter 对象
            private MovieAdapter movieAdapter;
            // 电影列表
            private List<Movie> movieList;
        
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main); // 设置布局文件
        
                recyclerView = findViewById(R.id.recyclerView); // 获取 RecyclerView 对象
                recyclerView.setLayoutManager(new LinearLayoutManager(this)); // 设置布局管理器
        
                movieList = new ArrayList<Movie>(); // 初始化电影列表
                // 添加一些示例电影数据
                movieList.add(new Movie("Inception", "A thief who steals corporate secrets...", 2010, 8.8, "https://example.com/inception_poster.jpg"));
                movieList.add(new Movie("The Dark Knight", "When the menace known as the Joker emerges...", 2008, 9.0, "https://example.com/dark_knight_poster.jpg"));
                
                movieAdapter = new MovieAdapter(movieList); // 初始化 MovieAdapter
                recyclerView.setAdapter(movieAdapter); // 设置 Adapter
            }
        }
        
    • 以上是一个基本的示例,实际应用中可以根据需求进行扩展优化,例如从 API 获取数据实现搜索功能

posted @ 2024-08-05 11:23  阿俊学JAVA  阅读(5)  评论(0编辑  收藏  举报