以下是一个基本的Android ListView电影列表的示例:
1. 创建一个新的Android项目,并在布局文件中添加一个ListView组件:
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/movie_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:dividerHeight="0dp" />
</RelativeLayout>
```
2. 创建一个Movie类,该类将存储电影的名称,导演和海报图像的资源ID:
```
public class Movie {
private String name;
private String director;
private int poster;
public Movie(String name, String director, int poster) {
this.name = name;
this.director = director;
this.poster = poster;
}
public String getName() {
return name;
}
public String getDirector() {
return director;
}
public int getPoster() {
return poster;
}
}
```
3. 在MainActivity中创建Movie对象列表,并将其添加到ListView中:
```
public class MainActivity extends AppCompatActivity {
private ListView movieList;
private List<Movie> movies;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
movieList = (ListView) findViewById(R.id.movie_list);
movies = new ArrayList<>();
movies.add(new Movie("The Shawshank Redemption", "Frank Darabont", R.drawable.shawshank_redemption));
movies.add(new Movie("The Godfather", "Francis Ford Coppola", R.drawable.godfather));
movies.add(new Movie("The Dark Knight", "Christopher Nolan", R.drawable.dark_knight));
movies.add(new Movie("The Lord of the Rings: The Fellowship of the Ring", "Peter Jackson", R.drawable.lord_of_the_rings));
movies.add(new Movie("Forrest Gump", "Robert Zemeckis", R.drawable.forrest_gump));
movies.add(new Movie("Star Wars: Episode IV - A New Hope", "George Lucas", R.drawable.star_wars));
movies.add(new Movie("The Matrix", "The Wachowski Brothers", R.drawable.matrix));
movies.add(new Movie("Goodfellas", "Martin Scorsese", R.drawable.goodfellas));
movies.add(new Movie("Pulp Fiction", "Quentin Tarantino", R.drawable.pulp_fiction));
movies.add(new Movie("Schindler's List", "Steven Spielberg", R.drawable.schindlers_list));
MovieAdapter adapter = new MovieAdapter(this, movies);
movieList.setAdapter(adapter);
}
}
```
4. 创建一个MovieAdapter类,该类继承自BaseAdapter,并将Movie对象列表中的每个电影显示在ListView中:
```
public class MovieAdapter extends BaseAdapter {
private Context context;
private List<Movie> movies;
public MovieAdapter(Context context, List<Movie> movies) {
this.context = context;
this.movies = movies;
}
@Override
public int getCount() {
return movies.size();
}
@Override
public Object getItem(int position) {
return movies.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.movie_item, null);
}
ImageView poster = (ImageView) view.findViewById(R.id.poster);
TextView name = (TextView) view.findViewById(R.id.name);
TextView director = (TextView) view.findViewById(R.id.director);
Movie movie = movies.get(position);
poster.setImageResource(movie.getPoster());
name.setText(movie.getName());
director.setText(movie.getDirector());
return view;
}
}
```
5. 创建一个movie_item.xml布局文件,该布局将在ListView中每个电影项的视图中重复使用:
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/poster"
android:layout_width="100dp"
android:layout_height="150dp"
android:padding="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/director"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
```
6. 运行应用程序,您将看到一个基本的电影列表,其中包含电影的名称,导演和海报图像。