/
AlbumsAdapter.java
101 lines (86 loc) · 3.45 KB
/
AlbumsAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.material.catalog.musicplayer;
import io.material.catalog.R;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import io.material.catalog.musicplayer.AlbumsAdapter.AlbumViewHolder;
import io.material.catalog.musicplayer.MusicData.Album;
/** An adapter which displays a list of albums in either a list or grid layout. */
class AlbumsAdapter extends ListAdapter<Album, AlbumViewHolder> {
public interface AlbumAdapterListener {
void onAlbumClicked(View view, Album album);
}
private final AlbumAdapterListener listener;
private final int itemLayout;
AlbumsAdapter(AlbumAdapterListener listener, boolean asGrid) {
super(Album.DIFF_CALLBACK);
this.listener = listener;
this.itemLayout =
asGrid
? R.layout.cat_music_player_album_grid_item
: R.layout.cat_music_player_album_list_item;
}
@NonNull
@Override
public AlbumViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new AlbumViewHolder(
LayoutInflater.from(viewGroup.getContext()).inflate(itemLayout, viewGroup, false),
listener);
}
@Override
public void onBindViewHolder(@NonNull AlbumViewHolder albumViewHolder, int i) {
albumViewHolder.bind(getItem(i));
}
static class AlbumViewHolder extends RecyclerView.ViewHolder {
private final AlbumAdapterListener listener;
private final View container;
private final ImageView albumImage;
private final TextView albumTitle;
private final TextView albumArtist;
// Optional field available only in the list item layout.
@Nullable private final TextView albumDuration;
AlbumViewHolder(View view, AlbumAdapterListener listener) {
super(view);
this.listener = listener;
container = view.findViewById(R.id.album_item_container);
albumImage = view.findViewById(R.id.album_image);
albumTitle = view.findViewById(R.id.album_title);
albumArtist = view.findViewById(R.id.album_artist);
albumDuration = view.findViewById(R.id.album_duration);
}
public void bind(Album album) {
// Use a unique transition name so this item can be used as a shared element when
// transitioning to the album details screen.
ViewCompat.setTransitionName(container, album.title);
container.setOnClickListener(v -> listener.onAlbumClicked(container, album));
albumImage.setImageResource(album.cover);
albumTitle.setText(album.title);
albumArtist.setText(album.artist);
if (albumDuration != null) {
albumDuration.setText(album.duration);
}
}
}
}