Android简易音乐重构MVVM Java版-新增推荐雷达歌单及重构首页(十三)

Posted 雪の星空朝酱

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android简易音乐重构MVVM Java版-新增推荐雷达歌单及重构首页(十三)相关的知识,希望对你有一定的参考价值。

android简易音乐重构MVVM Java版-新增推荐雷达歌单及重构首页(十三)

关于

  本篇主要介绍引用首页发现api来替换首页轮播何推荐歌单以及新增雷达歌单(根据登陆用户推荐)、实现discoverfragment上下滑动效果。

效果图

修改ApiService

  添加首页发现api

@GET("homepage/block/page")//首页发现
    LiveData<ApiResponse<HomeDiscoverEntity>> requireHomeDiscover(@Query("refresh") Boolean start);

  项目结构:

添加HomeDiscoverEntity实体类

  注意,首页发现的json解析实体类最好直接粘贴我的,因为用工具解析的在这个json是必然出错的,因为它涉及到了同一个层级同一个名称不同属性的问题,还有就是部分字段类型解析错误的问题

@NoArgsConstructor
@Data
public class HomeDiscoverEntity 

    private int code;
    private DataEntity data;
    private String message;

    @NoArgsConstructor
    @Data
    public static class DataEntity 
        private String cursor;
        private List<BlocksEntity> blocks;
        private boolean hasMore;
        private Object blockUUIDs;
        private PageConfigEntity pageConfig;
        private GuideToastEntity guideToast;
        private Object internalTest;
        private List<?> titles;
        private Object blockCodeOrderList;

        @NoArgsConstructor
        @Data
        public static class PageConfigEntity 
            private String refreshToast;
            private String nodataToast;
            private int refreshInterval;
            private Object title;
            private boolean fullscreen;
            private List<String> abtest;
            private List<String> songLabelMarkPriority;
            private int songLabelMarkLimit;
            private String homepageMode;
            private boolean showModeEntry;
            private String orderInfo;
        

        @NoArgsConstructor
        @Data
        public static class GuideToastEntity 
            private boolean hasGuideToast;
            private List<?> toastList;
        

        @NoArgsConstructor
        @Data
        public static class BlocksEntity 
            private String blockCode;
            private String showType;
            private int dislikeShowType;
            private Object extInfo;
            private boolean canClose;
            private int blockStyle;
            private String action;
            private String actionType;
            private UiElementEntity uiElement;
            private List<CreativesEntity> creatives;
            private String alg;
            private String logInfo;

            @NoArgsConstructor
            @Data
            public static class UiElementEntity 
                private SubTitleEntity subTitle;
                private ButtonEntity button;
                private String rcmdShowType;

                @NoArgsConstructor
                @Data
                public static class SubTitleEntity 
                    private String title;
                

                @NoArgsConstructor
                @Data
                public static class ButtonEntity 
                    private String action;
                    private String actionType;
                    private String text;
                    private Object iconUrl;
                
            

            @NoArgsConstructor
            @Data
            public static class CreativesEntity 
                private String creativeType;
                private String creativeId;
                private String action;
                private String actionType;
                private UiElementEntity uiElement;
                private List<ResourcesEntity> resources;
                private String alg;
                private String logInfo;
                private int position;

                @NoArgsConstructor
                @Data
                public static class UiElementEntity 
                    private MainTitleEntity mainTitle;
                    private ImageEntity image;
                    private List<String> labelTexts;
                    private String rcmdShowType;

                    @NoArgsConstructor
                    @Data
                    public static class MainTitleEntity 
                        private String title;
                    

                    @NoArgsConstructor
                    @Data
                    public static class ImageEntity 
                        private String imageUrl;
                    
                

                @NoArgsConstructor
                @Data
                public static class ResourcesEntity 
                    private UiElementEntity uiElement;
                    private String resourceType;
                    private String resourceId;
                    private Object resourceUrl;
                    private ResourceExtInfoEntity resourceExtInfo;
                    private String action;
                    private String actionType;
                    private boolean valid;
                    private String alg;
                    private String logInfo;

                    @NoArgsConstructor
                    @Data
                    public static class UiElementEntity 
                        private MainTitleEntity mainTitle;
                        private ImageEntity image;
                        private List<String> labelTexts;
                        private String rcmdShowType;

                        @NoArgsConstructor
                        @Data
                        public static class MainTitleEntity 
                            private String title;
                        

                        @NoArgsConstructor
                        @Data
                        public static class ImageEntity 
                            private String imageUrl;
                        
                    

                    @NoArgsConstructor
                    @Data
                    public static class ResourceExtInfoEntity 
                        private long playCount;
                        private boolean highQuality;
                        private int specialType;
                    
                
            
        
    

添加BannerExtInfoEntity(新banner的实体类)

@NoArgsConstructor
@Data
public class BannerExtInfoEntity 
      private List<BannersEntity> banners;

                @NoArgsConstructor
                @Data
                public static class BannersEntity 
                    private Object adLocation;
                    private Object monitorImpress;
                    private String bannerId;
                    private Object extMonitor;
                    private Object pid;
                    private String pic;
                    private Object program;
                    private Object video;
                    private Object adurlV2;
                    private Object adDispatchJson;
                    private Object dynamicVideoData;
                    private Object monitorType;
                    private Object adid;
                    private String titleColor;
                    private String requestId;
                    private boolean exclusive;
                    private String scm;
                    private Object event;
                    private String alg;
                    private String s_ctrp;
                    private SongEntity song;
                    private long targetId;
                    private boolean showAdTag;
                    private Object adSource;
                    private Object showContext;
                    private int targetType;
                    private String typeTitle;
                    private Object url;
                    private String encodeId;
                    private Object extMonitorInfo;
                    private Object monitorClick;
                    private Object monitorImpressList;
                    private Object monitorBlackList;
                    private Object monitorClickList;

                    @NoArgsConstructor
                    @Data
                    public static class SongEntity 
                        private String name;
                        private int id;
                        private int pst;
                        private int t;
                        private List<ArEntity> ar;
                        private List<?> alia;
                        private int pop;
                        private int st;
                        private String rt;
                        private int fee;
                        private int v;
                        private Object crbt;
                        private String cf;
                        private AlEntity al;
                        private int dt;
                        private HEntity h;
                        private MEntity m;
                        private LEntity l;
                        private SqEntity sq;
                        private HrEntity hr;
                        private Object a;
                        private String cd;
                        private int no;
                        private Object rtUrl;
                        private int ftype;
                        private List<?> rtUrls;
                        private int djId;
                        private int copyright;
                        private int s_id;
                        private int mark;
                        private int originCoverType;
                        private Object originSongSimpleData;
                        private Object tagPicList;
                        private boolean resourceState;
                        private int version;
                        private Object songJumpInfo;
                        private Object entertainmentTags;
                        private int single;
                        private Object noCopyrightRcmd;
                        private int mst;
                        private int cp;
                        private int mv;
                        private int rtype;
                        private Object rurl;
                        private long publishTime;
                        private VideoInfoEntity videoInfo;
                        private List<String> tns;
                        private String alg;

                        @NoArgsConstructor
                        @Data
                        public static class AlEntity 
                            private int id;
                            private String name;
                            private String picUrl;
                            private List<String> tns;
                            private String pic_str;
                            private long pic;
                        

                        @NoArgsConstructor
                        @Data
                        public static class HEntity 
                            private int br;
                            private int fid;
                            private int size;
                            private int vd;
                            private int sr;
                        

                        @NoArgsConstructor
                        @Data
                        public static class MEntity 
                            private int br;
                            private int fid;
                            private int size;
                            private int vd;
                            private int sr;
                        

                        @NoArgsConstructor
                        @Data
                        public static class LEntity 
                            private int br;
                            private int fid;
                            private int size;
                            private int vd;
                            private int sr;
                        

                        @NoArgsConstructor
                        @Data
                        public static class SqEntity 
                            private int br;
                            private int fid;
                            private int size;
                            private int vd;
                            private int sr;
                        

                        @NoArgsConstructor
                        @Data
                        public static class HrEntity 
                            private int br;
                            private int fid;
                            private int size;
                            private int vd;
                            private int sr;
                        

                        @NoArgsConstructor
                        @Data
                        public static class VideoInfoEntity 
                            private boolean moreThanOne;
                            private Object video;
                        

                        @NoArgsConstructor
                        @Data
                        public static class ArEntity 
                            private int id;
                            private String name;
                            private List<?> tns;
                            private List<?> alias;
                        
                    
                

添加ScrollView实现上下滑动

  修改activity_main.xml:

 <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toBottomOf="@id/view_top"
            app:layout_constraintBottom_toTopOf="@id/bottom_nav"
            android:fillViewport="true">
        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/home_viewpager"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_0"
            app:layout_constraintTop_toBottomOf="@id/view_top"
            app:layout_constraintBottom_toTopOf="@id/bottom_nav" />
        </ScrollView>

新增DiscoverFragmentViewModel

public class DiscoverFragmentViewModel extends ViewModel 

    public String date = Calendar.getInstance().get(Calendar.DAY_OF_MONTH) + "";

    public BannerExtInfoEntity bannerList;

    public List<HomeDiscoverEntity.DataEntity.BlocksEntity.CreativesEntity> recommendList;

    public List<HomeDiscoverEntity.DataEntity.BlocksEntity.CreativesEntity> selfMgcList;


    public LiveData<ApiResponse<HomeDiscoverEntity>> requireDiscover(Boolean refresh)
        return RetrofitUtils.getmApiUrl().requireHomeDiscover(refresh);
    


修改DiscoverFragment布局

<?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.discover.DiscoverFragmentViewModel" />
    </data>
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white80">

    <View
        android:id="@+id/view_title_bg"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_45"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@color/colorPrimary"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_95"
        app:layout_constraintTop_toBottomOf="@id/view_title_bg"
        android:background="@color/colorPrimary"
        />
    
    <com.youth.banner.Banner
        android:id="@+id/banner_img"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_160"
        app:layout_constraintTop_toBottomOf="@id/view_title_bg"
        android:layout_margin="@dimen/dp_16"
        />
    
    <ImageView
        android:idAndroid简易音乐重构MVVM Java版-新增启动动画

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

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

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

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

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