package com.example.helloworld;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onOpenPlaylistActivityClick();
}
public void onOpenPlaylistActivityClick() {
// Create an Intent to start PlaylistActivity
Intent intent = new Intent(this, PlaylistActivity.class);
startActivity(intent);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<ListView
android:id="@+id/playlistListView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
package com.example.helloworld;
import android.os.Bundle;
import android.widget.GridView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.helloworld.adapter.PlaylistAdapter;
import java.util.ArrayList;
import java.util.List;
public class PlaylistActivity extends AppCompatActivity {
private List<Playlist> playlistList; // Declare playlistList
private PlaylistAdapter latestAdapter;
private PlaylistAdapter recommendedAdapter;
private PlaylistAdapter popularAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playlist);
// Sample playlist data
playlistList = new ArrayList<>();
playlistList.add(new Playlist("最新推荐 1", R.drawable.text));
playlistList.add(new Playlist("推荐歌单 1", R.drawable.text));
playlistList.add(new Playlist("热门电台 1", R.drawable.text));
// Add more playlists as needed
// Initialize the adapters
latestAdapter = new PlaylistAdapter(this, filterPlaylists("最新"));
recommendedAdapter = new PlaylistAdapter(this, filterPlaylists("推荐"));
popularAdapter = new PlaylistAdapter(this, filterPlaylists("热门"));
// Get the GridViews from the layout
GridView gridViewLatest = findViewById(R.id.grid_view_latest);
GridView gridViewRecommended = findViewById(R.id.grid_view_recommended);
GridView gridViewPopular = findViewById(R.id.grid_view_popular);
// Set the adapters for the GridViews
gridViewLatest.setAdapter(latestAdapter);
gridViewRecommended.setAdapter(recommendedAdapter);
gridViewPopular.setAdapter(popularAdapter);
}
private List<Playlist> filterPlaylists(String filter) {
// Filter playlists based on the filter string
List<Playlist> filteredPlaylists = new ArrayList<>();
for (Playlist playlist : playlistList) {
if (playlist.getPlaylistName().contains(filter)) {
filteredPlaylists.add(playlist);
}
}
return filteredPlaylists;
}
}
package com.example.helloworld;
public class Playlist {
private String playlistName;
private int playlistImage;
public Playlist(String playlistName, int playlistImage) {
this.playlistName = playlistName;
this.playlistImage = playlistImage;
}
public String getPlaylistName() {
return playlistName;
}
public void setPlaylistName(String playlistName) {
this.playlistName = playlistName;
}
public int getPlaylistImage() {
return playlistImage;
}
public void setPlaylistImage(int playlistImage) {
this.playlistImage = playlistImage;
}
}
package com.example.helloworld.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.helloworld.Playlist;
import com.example.helloworld.R;
import java.util.ArrayList;
import java.util.List;
public class PlaylistAdapter extends BaseAdapter {
private Context context;
private List<Playlist> playlistList;
public PlaylistAdapter(Context context, List<Playlist> playlistList) {
this.context = context;
this.playlistList = playlistList;
}
@Override
public int getCount() {
return playlistList.size();
}
@Override
public Object getItem(int position) {
return playlistList.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.playlist_item, null);
}
Playlist playlist = playlistList.get(position);
// Set playlist name
TextView playlistNameTextView = view.findViewById(R.id.playlist_name);
playlistNameTextView.setText(playlist.getPlaylistName());
// Set playlist image
ImageView playlistImageView = view.findViewById(R.id.playlist_image);
playlistImageView.setImageResource(playlist.getPlaylistImage());
return view;
}
// Helper method to filter playlists based on their names
public List<Playlist> filterPlaylists(String filter) {
List<Playlist> filteredPlaylists = new ArrayList<>();
for (Playlist playlist : playlistList) {
if (playlist.getPlaylistName().toLowerCase().contains(filter.toLowerCase())) {
filteredPlaylists.add(playlist);
}
}
return filteredPlaylists;
}
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PlaylistActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<!-- Title 1: 最新推荐 -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="最新推荐"
android:textSize="18sp"
android:padding="16dp" />
<!-- GridView for 最新推荐 -->
<!-- Set a fixed height for 2 rows -->
<GridView
android:id="@+id/grid_view_latest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:scrollbars="none" />
<!-- Title 2: 推荐歌单 -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="推荐歌单"
android:textSize="18sp"
android:padding="16dp" />
<!-- GridView for 推荐歌单 -->
<GridView
android:id="@+id/grid_view_recommended"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:scrollbars="none" />
<!-- Title 3: 热门电台 -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="热门电台"
android:textSize="18sp"
android:padding="16dp" />
<!-- GridView for 热门电台 -->
<GridView
android:id="@+id/grid_view_popular"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:scrollbars="none" />
</LinearLayout>
</ScrollView>
<!-- playlist_item.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/playlist_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="#FFFFFF" />
<TextView
android:id="@+id/playlist_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:text="Playlist Name"
android:textSize="16sp" />
</LinearLayout>