Set horizontal pagination in ArtistPageFragment top songs

This commit is contained in:
CappielloAntonio 2021-08-02 09:41:53 +02:00
parent f321a5520f
commit c46c41db20
4 changed files with 25 additions and 12 deletions

View file

@ -14,7 +14,9 @@ import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import com.cappielloantonio.play.App;
import com.cappielloantonio.play.R;
@ -29,6 +31,7 @@ import com.cappielloantonio.play.repository.ArtistRepository;
import com.cappielloantonio.play.repository.QueueRepository;
import com.cappielloantonio.play.service.MusicPlayerRemote;
import com.cappielloantonio.play.ui.activity.MainActivity;
import com.cappielloantonio.play.util.UIUtil;
import com.cappielloantonio.play.viewmodel.ArtistPageViewModel;
import java.util.ArrayList;
@ -163,11 +166,18 @@ public class ArtistPageFragment extends Fragment {
}
private void initTopSongsView() {
bind.mostStreamedSongRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
bind.mostStreamedSongRecyclerView.setHasFixedSize(true);
songHorizontalAdapter = new SongHorizontalAdapter(activity, requireContext(), getChildFragmentManager());
bind.mostStreamedSongRecyclerView.setAdapter(songHorizontalAdapter);
artistPageViewModel.getArtistTopSongList().observe(requireActivity(), songs -> songHorizontalAdapter.setItems(songs));
artistPageViewModel.getArtistTopSongList().observe(requireActivity(), songs -> {
if (bind != null) bind.artistPageTopSongsSector.setVisibility(!songs.isEmpty() ? View.VISIBLE : View.GONE);
bind.mostStreamedSongRecyclerView.setLayoutManager(new GridLayoutManager(requireContext(), UIUtil.getSpanCount(songs.size(), 5), GridLayoutManager.HORIZONTAL, false));
songHorizontalAdapter.setItems(songs);
});
PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
pagerSnapHelper.attachToRecyclerView(bind.mostStreamedSongRecyclerView);
}
private void initAlbumsView() {

View file

@ -173,8 +173,7 @@ public class HomeFragment extends Fragment {
recentlyPlayedAlbumAdapter = new AlbumAdapter(requireContext());
bind.recentlyPlayedAlbumsRecyclerView.setAdapter(recentlyPlayedAlbumAdapter);
homeViewModel.getRecentlyPlayedAlbumList().observe(requireActivity(), albums -> {
if (bind != null)
bind.homeRecentlyPlayedAlbumsSector.setVisibility(!albums.isEmpty() ? View.VISIBLE : View.GONE);
if (bind != null) bind.homeRecentlyPlayedAlbumsSector.setVisibility(!albums.isEmpty() ? View.VISIBLE : View.GONE);
recentlyPlayedAlbumAdapter.setItems(albums);
});
}
@ -202,8 +201,7 @@ public class HomeFragment extends Fragment {
starredSongAdapter = new SongHorizontalAdapter(activity, requireContext(), getChildFragmentManager());
bind.starredTracksRecyclerView.setAdapter(starredSongAdapter);
homeViewModel.getStarredTracks().observe(requireActivity(), songs -> {
if (bind != null)
bind.homeStarredTracksSector.setVisibility(!songs.isEmpty() ? View.VISIBLE : View.GONE);
if (bind != null) bind.homeStarredTracksSector.setVisibility(!songs.isEmpty() ? View.VISIBLE : View.GONE);
bind.starredTracksRecyclerView.setLayoutManager(new GridLayoutManager(requireContext(), UIUtil.getSpanCount(songs.size(), 5), GridLayoutManager.HORIZONTAL, false));
starredSongAdapter.setItems(songs);
});
@ -218,8 +216,7 @@ public class HomeFragment extends Fragment {
starredAlbumAdapter = new AlbumHorizontalAdapter(activity, requireContext(), getChildFragmentManager());
bind.starredAlbumsRecyclerView.setAdapter(starredAlbumAdapter);
homeViewModel.getStarredAlbums().observe(requireActivity(), albums -> {
if (bind != null)
bind.homeStarredAlbumsSector.setVisibility(!albums.isEmpty() ? View.VISIBLE : View.GONE);
if (bind != null) bind.homeStarredAlbumsSector.setVisibility(!albums.isEmpty() ? View.VISIBLE : View.GONE);
bind.starredAlbumsRecyclerView.setLayoutManager(new GridLayoutManager(requireContext(), UIUtil.getSpanCount(albums.size(), 5), GridLayoutManager.HORIZONTAL, false));
starredAlbumAdapter.setItems(albums);
});
@ -234,8 +231,7 @@ public class HomeFragment extends Fragment {
starredArtistAdapter = new ArtistHorizontalAdapter(activity, requireContext(), getChildFragmentManager());
bind.starredArtistsRecyclerView.setAdapter(starredArtistAdapter);
homeViewModel.getStarredArtists().observe(requireActivity(), artists -> {
if (bind != null)
bind.homeStarredArtistsSector.setVisibility(!artists.isEmpty() ? View.VISIBLE : View.GONE);
if (bind != null) bind.homeStarredArtistsSector.setVisibility(!artists.isEmpty() ? View.VISIBLE : View.GONE);
bind.starredArtistsRecyclerView.setLayoutManager(new GridLayoutManager(requireContext(), UIUtil.getSpanCount(artists.size(), 5), GridLayoutManager.HORIZONTAL, false));
starredArtistAdapter.setItems(artists);
});
@ -262,8 +258,7 @@ public class HomeFragment extends Fragment {
dowanloadedMusicAdapter = new RecentMusicAdapter(activity, requireContext(), getChildFragmentManager());
bind.downloadedTracksRecyclerView.setAdapter(dowanloadedMusicAdapter);
homeViewModel.getDownloaded().observe(requireActivity(), downloads -> {
if (bind != null)
bind.homeDownloadedTracksSector.setVisibility(!downloads.isEmpty() ? View.VISIBLE : View.GONE);
if (bind != null) bind.homeDownloadedTracksSector.setVisibility(!downloads.isEmpty() ? View.VISIBLE : View.GONE);
dowanloadedMusicAdapter.setItems(MappingUtil.mapDownload(downloads));
});
}

View file

@ -117,6 +117,7 @@
<!-- Label and button -->
<LinearLayout
android:id="@+id/artist_page_top_songs_sector"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">