Android简易音乐重构MVVM Java版-新增我喜欢的歌单展示+底部bar点击跳转播放功能(十九)

Posted 雪の星空朝酱

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android简易音乐重构MVVM Java版-新增我喜欢的歌单展示+底部bar点击跳转播放功能(十九)相关的知识,希望对你有一定的参考价值。

android简易音乐重构MVVM Java版-新增我喜欢的歌单展示+底部bar点击跳转播放功能(十九)

关于

效果

修改MainActivity

  新增底部bar初始化

private void initBottomBar() 
        viewModel.getRecentSong().observe(this,recentSongInfoEntityApiResponse -> 
            if (recentSongInfoEntityApiResponse.getStatus() == Status.SUCCESS && recentSongInfoEntityApiResponse.getData().getData().getList().size() >0)
                RecentSongInfoEntity.RecentDataEntity.ListEntity.DataEntity data =recentSongInfoEntityApiResponse.getData().getData().getList()
                        .get(0).getData();
                viewModel.currentSongName.set(data.getName());
                viewModel.currentSongUrl.set(data.getAl().getPicUrl());
                MusicInfo musicInfo = new MusicInfo();
                musicInfo.setArtist(data.getAr().get(0).getName());
                musicInfo.setSongId(data.getId()+"");
                musicInfo.setSongName(data.getName());
                musicInfo.setSongCover(data.getAl().getPicUrl());
                musicInfo.setSongUrl(SONG_URL+data.getId());
                viewModel.currentMusicInfo = musicInfo;
            
        );

        MusicPlay.onPlayStateListener(this, new OnMusicPlayStateListener() 
            @Override
            public void onPlayState(@NonNull PlayManger playManger) 
                viewModel.currentSongUrl.set(playManger.getSongInfo().getSongCover());
                viewModel.currentSongName.set(playManger.getSongInfo().getSongName());
                switch (playManger.getStage())
                    case PlayManger.PAUSE:
                    case PlayManger.IDLE:
                        binding.songBar.ivBottomPlay.setImageResource(R.drawable.shape_play);
                        break;
                    case PlayManger.PLAYING:
                        binding.songBar.ivBottomPlay.setImageResource(R.drawable.shape_pause_black);
                        viewModel.currentMusicInfo = playManger.getSongInfo();
                        break;
                    case PlayManger.BUFFERING:
                        ViewExtensionKt.printLog("缓冲");
                        break;
                    case PlayManger.SWITCH:
                        viewModel.currentMusicInfo = playManger.getSongInfo();
                        break;

                
            
        );

        binding.songBar.rootBottomBar.setOnClickListener(view -> 
            if (ClickUtil.enableClick())
                MusicPlay.playMusicByInfo(viewModel.currentMusicInfo);
                startActivity(new Intent(this, CurrentSongPlayActivity.class)
                        .putExtra(MUSIC_INFO, viewModel.currentMusicInfo));
            
        );
        binding.songBar.ivBottomPlay.setOnClickListener(view -> 
            if (MusicPlay.isPlaying())
                MusicPlay.pauseMusic();
            else if (MusicPlay.isPaused())
                MusicPlay.restoreMusic();
            else 
                MusicPlay.playMusicByInfo(viewModel.currentMusicInfo);
            
        );
    

修改fragment_mine.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <variable
            name="vm"
            type="com.tobery.personalmusic.ui.home.MainViewModel" />
        <variable
            name="my"
            type="com.tobery.personalmusic.ui.home.mine.MineFragmentViewModel" />
    </data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/grays_10">


    <View
        android:id="@+id/view_nike"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_0"
        android:layout_marginTop="@dimen/dp_40"
        android:background="@drawable/bg_view_white_full"
        android:paddingBottom="@dimen/dp_24"
        app:layout_constraintTop_toTopOf="@id/img_head"
        app:layout_constraintBottom_toBottomOf="@id/tv_follow"
        android:padding="@dimen/dp_24"
        android:layout_marginStart="@dimen/dp_16"
        android:layout_marginEnd="@dimen/dp_16"
        />

    <ImageView
        android:id="@+id/img_head"
        android:layout_width="@dimen/dp_80"
        android:layout_height="@dimen/dp_80"
        imSrc="@vm.ui.imageUrl"
        error="@@drawable/ic_banner_loading"
        android:layout_margin="@dimen/dp_16"
        android:elevation="@dimen/dp_8"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <WebView
        android:id="@+id/web_vip"
        android:layout_width="@dimen/dp_40"
        android:layout_height="@dimen/dp_20"
        app:layout_constraintBottom_toBottomOf="@id/tv_nickname"
        app:layout_constraintStart_toEndOf="@id/tv_nickname"
        android:layout_marginStart="@dimen/dp_5"
        />

    <TextView
        android:id="@+id/tv_nickname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@vm.ui.nickname"
        android:textColor="@color/black"
        android:textSize="@dimen/sp_18"
        android:textStyle="bold"
        app:layout_constraintTop_toBottomOf="@id/img_head"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="@dimen/dp_8"
        />

    <TextView
        android:id="@+id/tv_follow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@vm.ui.follows"
        android:textSize="@dimen/sp_14"
        android:textColor="@color/grays_66"
        android:layout_marginEnd="@dimen/dp_16"
        android:paddingBottom="@dimen/dp_8"
        app:layout_constraintTop_toBottomOf="@id/tv_nickname"
        app:layout_constraintEnd_toStartOf="@id/tv_fans"
        />
    <TextView
        android:id="@+id/tv_fans"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@vm.ui.followeds"
        android:textSize="@dimen/sp_14"
        android:textColor="@color/grays_66"
        app:layout_constraintTop_toBottomOf="@id/tv_nickname"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        />

    <TextView
        android:id="@+id/tv_level"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@my.level"
        android:textSize="@dimen/sp_14"
        android:textColor="@color/grays_66"
        android:layout_marginStart="@dimen/dp_16"
        app:layout_constraintTop_toBottomOf="@id/tv_nickname"
        app:layout_constraintStart_toEndOf="@id/tv_fans"
        />

    <View
        android:id="@+id/view_like_item"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_80"
        app:layout_constraintTop_toBottomOf="@id/view_nike"
        android:background="@drawable/bg_view_white_full"
        android:layout_margin="@dimen/dp_16"
        />

    <ImageView
        android:id="@+id/img_cover"
        android:layout_width="@dimen/dp_60"
        android:layout_height="@dimen/dp_60"
        app:layout_constraintTop_toTopOf="@id/view_like_item"
        app:layout_constraintBottom_toBottomOf="@id/view_like_item"
        app:layout_constraintStart_toStartOf="@id/view_like_item"
        android:layout_marginStart="@dimen/dp_16"
        rectangleSrc="@my.mineLikeCover"
        />

    <TextView
        android:id="@+id/tv_introduction"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:text="music"
        android:textSize="@dimen/sp_16"
        android:layout_marginStart="@dimen/dp_8"
        app:layout_constraintTop_toTopOf="@id/view_like_item"
        app:layout_constraintBottom_toTopOf="@id/tv_count"
        app:layout_constraintStart_toEndOf="@id/img_cover"
        />

    <TextView
        android:id="@+id/tv_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/grays_66"
        android:text="@my.trackCount"
        android:textSize="@dimen/sp_12"
        android:layout_marginStart="@dimen/dp_8"
        app:layout_constraintTop_toBottomOf="@id/tv_introduction"
        app:layout_constraintBottom_toBottomOf="@id/view_like_item"
        app:layout_constraintStart_toEndOf="@id/img_cover"
        />



</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

修改MainViewModel

public class MainViewModel extends ViewModel 

    private SavedStateHandle state;

    public UserInfoUi ui;

    private String userInfo;

    public ObservableField<String> currentSongUrl = new ObservableField<>("");
    public ObservableField<String> currentSongName = new ObservableField<>("");
    public MusicInfo currentMusicInfo;

    public MainViewModel(SavedStateHandle savedStateHandle) 
        this.state = savedStateHandle;
        ui = state.get(KEY_MAIN_UI) == null ? new UserInfoUi(new ObservableField<>(""), new ObservableField<>(""), new ObservableInt(0), new ObservableField<>(""), new ObservableField<>(""), new ObservableField<>(""), new ObservableField<>("")) : state.get(KEY_MAIN_UI);
    

    public void initUi() 
        userInfo = SharePreferencesUtil.getInstance(ContextProvider.get().getContext())
                .getUserInfo();
        LoginEntity data = new Gson().fromJson(userInfo, LoginEntity.class);
        if (null != data.getProfile()) 
            ui.nickname.set(data.getProfile().getNickname());
            ui.imageUrl.set(data.getProfile().getAvatarUrl(以上是关于Android简易音乐重构MVVM Java版-新增我喜欢的歌单展示+底部bar点击跳转播放功能(十九)的主要内容,如果未能解决你的问题,请参考以下文章

Android简易音乐重构MVVM Java版-新增启动动画

Android简易音乐重构MVVM Java版-新增推荐菜单及侧边栏展示

Android简易音乐重构MVVM Java版-BottomNavigationView+viewpager主界面结构

Android简易音乐重构MVVM Java版-新增推荐雷达歌单详情列表界面(十八)

Android简易音乐重构MVVM Java版-新增推荐雷达歌单详情列表界面(十八)

Android简易音乐重构MVVM Java版-新增歌曲播放界面+状态栏黑科技(十七)