歌单布局适配器

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值