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:id="@+id/img_recommend"
android:layout_width="@dimen/dp_50"
android:layout_height="@dimen/dp_50"
android:src="@drawable/ic_daily_recommend"
android:layout_margin="@dimen/dp_16"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/banner_img"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/sp_12"
android:text="@{vm.date}"
app:layout_constraintTop_toTopOf="@id/img_recommend"
app:layout_constraintBottom_toBottomOf="@id/img_recommend"
app:layout_constraintStart_toStartOf="@id/img_recommend"
app:layout_constraintEnd_toEndOf="@id/img_recommend"
/>
<TextView
android:id="@+id/tv_daily"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dp_4"
android:text="@string/day_recommend"
android:textColor="@color/dark_grey"
app:layout_constraintTop_toBottomOf="@id/img_recommend"
app:layout_constraintStart_toStartOf="@id/img_recommend"
app:layout_constraintEnd_toEndOf="@id/img_recommend"
android:textSize="@dimen/sp_12" />
<ImageView
android:id="@+id/img_mine_fm"
android:layout_width="@dimen/dp_50"
android:layout_height="@dimen/dp_50"
android:src="@drawable/ic_mine_fm"
android:layout_margin="@dimen/dp_16"
app:layout_constraintStart_toEndOf="@id/img_recommend"
app:layout_constraintTop_toBottomOf="@id/banner_img"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dp_4"
android:text="@string/fm"
android:textColor="@color/dark_grey"
app:layout_constraintTop_toBottomOf="@id/img_mine_fm"
app:layout_constraintStart_toStartOf="@id/img_mine_fm"
app:layout_constraintEnd_toEndOf="@id/img_mine_fm"
android:textSize="@dimen/sp_12" />
<ImageView
android:id="@+id/img_playlist"
android:layout_width="@dimen/dp_50"
android:layout_height="@dimen/dp_50"
android:src="@drawable/ic_playlist"
android:layout_margin="@dimen/dp_16"
app:layout_constraintStart_toEndOf="@id/img_mine_fm"
app:layout_constraintTop_toBottomOf="@id/banner_img"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dp_4"
android:text="@string/playlist"
android:textColor="@color/dark_grey"
app:layout_constraintTop_toBottomOf="@id/img_playlist"
app:layout_constraintStart_toStartOf="@id/img_playlist"
app:layout_constraintEnd_toEndOf="@id/img_playlist"
android:textSize="@dimen/sp_12" />
<ImageView
android:id="@+id/img_rank"
android:layout_width="@dimen/dp_50"
android:layout_height="@dimen/dp_50"
android:src="@drawable/ic_playlist"
android:layout_margin="@dimen/dp_16"
app:layout_constraintStart_toEndOf="@id/img_playlist"
app:layout_constraintTop_toBottomOf="@id/banner_img"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dp_4"
android:text="@string/rank"
android:textColor="@color/dark_grey"
app:layout_constraintTop_toBottomOf="@id/img_rank"
app:layout_constraintStart_toStartOf="@id/img_rank"
app:layout_constraintEnd_toEndOf="@id/img_rank"
android:textSize="@dimen/sp_12" />
<ImageView
android:id="@+id/img_live"
android:layout_width="@dimen/dp_50"
android:layout_height="@dimen/dp_50"
android:src="@drawable/ic_live"
android:layout_margin="@dimen/dp_16"
app:layout_constraintStart_toEndOf="@id/img_rank"
app:layout_constraintTop_toBottomOf="@id/banner_img"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dp_4"
android:text="@string/live"
android:textColor="@color/dark_grey"
app:layout_constraintTop_toBottomOf="@id/img_live"
app:layout_constraintStart_toStartOf="@id/img_live"
app:layout_constraintEnd_toEndOf="@id/img_live"
android:textSize="@dimen/sp_12" />
<ImageView
android:id="@+id/img_radio"
android:layout_width="@dimen/dp_50"
android:layout_height="@dimen/dp_50"
android:src="@drawable/ic_radio"
android:layout_margin="@dimen/dp_16"
app:layout_constraintStart_toEndOf="@id/img_live"
app:layout_constraintTop_toBottomOf="@id/banner_img"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dp_4"
android:text="@string/radio"
android:textColor="@color/dark_grey"
app:layout_constraintTop_toBottomOf="@id/img_radio"
app:layout_constraintStart_toStartOf="@id/img_radio"
app:layout_constraintEnd_toEndOf="@id/img_radio"
android:textSize="@dimen/sp_12" />
<View
android:id="@+id/view_recommend"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_1"
android:layout_marginTop="@dimen/dp_12"
android:background="@color/boundary_gray"
app:layout_constraintTop_toBottomOf="@id/tv_daily"/>
<TextView
android:id="@+id/tv_recommend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dp_16"
android:text="@string/recommend_playlist"
android:textColor="@color/tv_black"
android:textSize="@dimen/sp_14"
android:textStyle="bold"
app:layout_constraintTop_toBottomOf="@id/view_recommend"
app:layout_constraintStart_toStartOf="parent"
/>
<TextView
android:id="@+id/tv_playlist_playground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/dp_16"
android:layout_marginStart="@dimen/dp_15"
android:background="@drawable/bg_playlist_playground"
android:paddingLeft="@dimen/dp_15"
android:paddingTop="@dimen/dp_8"
android:paddingRight="@dimen/dp_15"
android:paddingBottom="@dimen/dp_8"
android:text="@string/playlist_playground"
android:textColor="@color/tv_gray_01"
android:textSize="@dimen/sp_11"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/tv_recommend"
app:layout_constraintBottom_toBottomOf="@id/tv_recommend"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recommend_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_12"
android:layout_marginStart="@dimen/dp_8"
app:layout_constraintTop_toBottomOf="@id/tv_recommend"/>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dp_0"
android:layout_marginTop="@dimen/dp_8"
android:background="@drawable/bg_view_white_full"
android:paddingBottom="@dimen/dp_24"
app:layout_constraintTop_toBottomOf="@id/recommend_recycle"
app:layout_constraintBottom_toBottomOf="@id/like_recycle"
/>
<TextView
android:id="@+id/tv_like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dp_16"
android:textColor="@color/tv_black"
android:textSize="@dimen/sp_14"
android:textStyle="bold"
app:layout_constraintTop_toBottomOf="@id/recommend_recycle"
app:layout_constraintStart_toStartOf="parent"
/>
<TextView
android:id="@+id/tv_like_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/dp_16"
android:layout_marginStart="@dimen/dp_15"
android:background="@drawable/bg_playlist_playground"
android:paddingLeft="@dimen/dp_15"
android:paddingTop="@dimen/dp_8"
android:paddingRight="@dimen/dp_15"
android:paddingBottom="@dimen/dp_8"
android:textColor="@color/tv_gray_01"
android:textSize="@dimen/sp_11"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/tv_like"
app:layout_constraintBottom_toBottomOf="@id/tv_like"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/like_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_12"
android:layout_marginStart="@dimen/dp_8"
app:layout_constraintTop_toBottomOf="@id/tv_like"/>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dp_0"
android:layout_marginTop="@dimen/dp_8"
android:background="@drawable/bg_view_white_full"
android:paddingBottom="@dimen/dp_24"
app:layout_constraintTop_toBottomOf="@id/like_recycle"
app:layout_constraintBottom_toBottomOf="@id/mgc_recycle"
/>
<TextView
android:id="@+id/tv_mgc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dp_16"
android:textColor="@color/tv_black"
android:textSize="@dimen/sp_14"
android:textStyle="bold"
app:layout_constraintTop_toBottomOf="@id/like_recycle"
app:layout_constraintStart_toStartOf="parent"
/>
<TextView
android:id="@+id/tv_mgc_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/dp_16"
android:layout_marginStart="@dimen/dp_15"
android:background="@drawable/bg_playlist_playground"
android:paddingLeft="@dimen/dp_15"
android:paddingTop="@dimen/dp_8"
android:paddingRight="@dimen/dp_15"
android:paddingBottom="@dimen/dp_8"
android:text="@string/playlist_playground"
android:textColor="@color/tv_gray_01"
android:textSize="@dimen/sp_11"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/tv_mgc"
app:layout_constraintBottom_toBottomOf="@id/tv_mgc"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/mgc_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_12"
android:layout_marginStart="@dimen/dp_8"
app:layout_constraintTop_toBottomOf="@id/tv_mgc"/>
<TextView
android:id="@+id/tv_look"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dp_16"
android:textColor="@color/tv_black"
android:textSize="@dimen/sp_14"
android:textStyle="bold"
app:layout_constraintTop_toBottomOf="@id/mgc_recycle"
app:layout_constraintStart_toStartOf="parent"
/>
<TextView
android:id="@+id/tv_look_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/dp_16"
android:layout_marginStart="@dimen/dp_15"
android:background="@drawable/bg_playlist_playground"
android:paddingLeft="@dimen/dp_15"
android:paddingTop="@dimen/dp_8"
android:paddingRight="@dimen/dp_15"
android:paddingBottom="@dimen/dp_8"
android:textColor="@color/tv_gray_01"
android:textSize="@dimen/sp_11"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/tv_look"
app:layout_constraintBottom_toBottomOf="@id/tv_look"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/look_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_12"
android:layout_marginStart="@dimen/dp_8"
app:layout_constraintTop_toBottomOf="@id/tv_look"/>
<TextView
android:id="@+id/tv_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/look_recycle"
android:layout_margin="@dimen/dp_24"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
<?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/white">
<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:id="@+id/img_recommend"
android:layout_width="@dimen/dp_50"
android:layout_height="@dimen/dp_50"
android:src="@drawable/ic_daily_recommend"
android:layout_margin="@dimen/dp_16"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/banner_img"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/sp_12"
android:text="@{vm.date}"
app:layout_constraintTop_toTopOf="@id/img_recommend"
app:layout_constraintBottom_toBottomOf="@id/img_recommend"
app:layout_constraintStart_toStartOf="@id/img_recommend"
app:layout_constraintEnd_toEndOf="@id/img_recommend"
/>
<TextView
android:id="@+id/tv_daily"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dp_4"
android:text="@string/day_recommend"
android:textColor="@color/dark_grey"
app:layout_constraintTop_toBottomOf="@id/img_recommend"
app:layout_constraintStart_toStartOf="@id/img_recommend"
app:layout_constraintEnd_toEndOf="@id/img_recommend"
android:textSize="@dimen/sp_12" />
<ImageView
android:id="@+id/img_mine_fm"
android:layout_width="@dimen/dp_50"
android:layout_height="@dimen/dp_50"
android:src="@drawable/ic_mine_fm"
android:layout_margin="@dimen/dp_16"
app:layout_constraintStart_toEndOf="@id/img_recommend"
app:layout_constraintTop_toBottomOf="@id/banner_img"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dp_4"
android:text="@string/fm"
android:textColor="@color/dark_grey"
app:layout_constraintTop_toBottomOf="@id/img_mine_fm"
app:layout_constraintStart_toStartOf="@id/img_mine_fm"
app:layout_constraintEnd_toEndOf="@id/img_mine_fm"
android:textSize="@dimen/sp_12" />
<ImageView
android:id="@+id/img_playlist"
android:layout_width="@dimen/dp_50"
android:layout_height="@dimen/dp_50"
android:src="@drawable/ic_playlist"
android:layout_margin="@dimen/dp_16"
app:layout_constraintStart_toEndOf="@id/img_mine_fm"
app:layout_constraintTop_toBottomOf="@id/banner_img"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dp_4"
android:text="@string/playlist"
android:textColor="@color/dark_grey"
app:layout_constraintTop_toBottomOf="@id/img_playlist"
app:layout_constraintStart_toStartOf="@id/img_playlist"
app:layout_constraintEnd_toEndOf="@id/img_playlist"
android:textSize="@dimen/sp_12" />
<ImageView
android:id="@+id/img_rank"
android:layout_width="@dimen/dp_50"
android:layout_height="@dimen/dp_50"
android:src="@drawable/ic_playlist"
android:layout_margin="@dimen/dp_16"
app:layout_constraintStart_toEndOf="@id/img_playlist"
app:layout_constraintTop_toBottomOf="@id/banner_img"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dp_4"
android:text="@string/rank"
android:textColor="@color/dark_grey"
app:layout_constraintTop_toBottomOf="@id/img_rank"
app:layout_constraintStart_toStartOf="@id/img_rank"
app:layout_constraintEnd_toEndOf="@id/img_rank"
android:textSize="@dimen/sp_12" />
<ImageView
android:id="@+id/img_live"
android:layout_width="@dimen/dp_50"
android:layout_height="@dimen/dp_50"
android:src="@drawable/ic_live"
android:layout_margin="@dimen/dp_16"
app:layout_constraintStart_toEndOf="@id/img_rank"
app:layout_constraintTop_toBottomOf="@id/banner_img"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dp_4"
android:text="@string/live"
android:textColor="@color/dark_grey"
app:layout_constraintTop_toBottomOf="@id/img_live"
app:layout_constraintStart_toStartOf="@id/img_live"
app:layout_constraintEnd_toEndOf="@id/img_live"
android:textSize="@dimen/sp_12" />
<ImageView
android:id="@+id/img_radio"
android:layout_width="@dimen/dp_50"
android:layout_height="@dimen/dp_50"
android:src="@drawable/ic_radio"
android:layout_margin="@dimen/dp_16"
app:layout_constraintStart_toEndOf="@id/img_live"
app:layout_constraintTop_toBottomOf="@id/banner_img"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dp_4"
android:text="@string/radio"
android:textColor="@color/dark_grey"
app:layout_constraintTop_toBottomOf="@id/img_radio"
app:layout_constraintStart_toStartOf="@id/img_radio"
app:layout_constraintEnd_toEndOf="@id/img_radio"
android:textSize="@dimen/sp_12" />
<View
android:id="@+id/view_recommend"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_1"
android:layout_marginTop="@dimen/dp_12"
android:background="@color/boundary_gray"
app:layout_constraintTop_toBottomOf="@id/tv_daily"/>
<TextView
android:id="@+id/tv_recommend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dp_16"
android:text="@string/recommend_playlist"
android:textColor="@color/tv_black"
android:textSize="@dimen/sp_14"
android:textStyle="bold"
app:layout_constraintTop_toBottomOf="@id/view_recommend"
app:layout_constraintStart_toStartOf="parent"
/>
<TextView
android:id="@+id/tv_playlist_playground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/dp_16"
android:layout_marginStart="@dimen/dp_15"
android:background="@drawable/bg_playlist_playground"
android:paddingLeft="@dimen/dp_15"
android:paddingTop="@dimen/dp_8"
android:paddingRight="@dimen/dp_15"
android:paddingBottom="@dimen/dp_8"
android:text="@string/playlist_playground"
android:textColor="@color/tv_gray_01"
android:textSize="@dimen/sp_11"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/tv_recommend"
app:layout_constraintBottom_toBottomOf="@id/tv_recommend"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recommend_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_12"
android:layout_marginStart="@dimen/dp_16"
app:layout_constraintTop_toBottomOf="@id/tv_recommend"/>
<TextView
android:id="@+id/tv_mgc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dp_16"
android:textColor="@color/tv_black"
android:textSize="@dimen/sp_14"
android:textStyle="bold"
app:layout_constraintTop_toBottomOf="@id/recommend_recycle"
app:layout_constraintStart_toStartOf="parent"
/>
<TextView
android:id="@+id/tv_mgc_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/dp_16"
android:layout_marginStart="@dimen/dp_15"
android:background="@drawable/bg_playlist_playground"
android:paddingLeft="@dimen/dp_15"
android:paddingTop="@dimen/dp_8"
android:paddingRight="@dimen/dp_15"
android:paddingBottom="@dimen/dp_8"
android:text="@string/playlist_playground"
android:textColor="@color/tv_gray_01"
android:textSize="@dimen/sp_11"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/tv_mgc"
app:layout_constraintBottom_toBottomOf="@id/tv_mgc"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/mgc_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_12"
android:layout_marginStart="@dimen/dp_16"
app:layout_constraintTop_toBottomOf="@id/tv_mgc"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
修改DiscoverFragment.java
public class DiscoverFragment extends Fragment {
private FragmentDiscoverBinding binding;
private DiscoverFragmentViewModel viewModel;
private RecommendAdapter adapter;
private MgcAdapter mgcAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = FragmentDiscoverBinding.inflate(inflater,container,false);
viewModel = new ViewModelProvider(this).get(DiscoverFragmentViewModel.class);
binding.setLifecycleOwner(this);
binding.setVm(viewModel);
return binding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initRecycle();
initObserver();
}
private void initRecycle() {
adapter = new RecommendAdapter(getContext());
LinearLayoutManager manager = new LinearLayoutManager(getContext());
manager.setOrientation(LinearLayoutManager.HORIZONTAL);
binding.recommendRecycle.setLayoutManager(manager);
binding.recommendRecycle.setHasFixedSize(true);
binding.recommendRecycle.setAdapter(adapter);
mgcAdapter = new MgcAdapter(getContext());
LinearLayoutManager managerMgc = new LinearLayoutManager(getContext());
managerMgc.setOrientation(LinearLayoutManager.HORIZONTAL);
binding.mgcRecycle.setLayoutManager(managerMgc);
binding.mgcRecycle.setHasFixedSize(true);
binding.mgcRecycle.setAdapter(mgcAdapter);
}
private void initObserver() {
viewModel.requireDiscover(false).observe(getViewLifecycleOwner(), homeDiscoverEntityApiResponse -> {
if (homeDiscoverEntityApiResponse.getStatus() == Status.SUCCESS){
for (HomeDiscoverEntity.DataEntity.BlocksEntity block : homeDiscoverEntityApiResponse.getData().getData().getBlocks()){
switch (block.getBlockCode()){
case "HOMEPAGE_BANNER": //banner
String bannerJson = new Gson().toJson(block.getExtInfo());
viewModel.bannerList = new Gson().fromJson(bannerJson,BannerExtInfoEntity.class);
break;
case "HOMEPAGE_BLOCK_PLAYLIST_RCMD": //推荐歌单
viewModel.recommendList = block.getCreatives();
break;
case "HOMEPAGE_BLOCK_MGC_PLAYLIST"://雷达歌单
viewModel.selfMgcList = block.getCreatives();
binding.tvMgc.setText(block.getUiElement().getSubTitle().getTitle());
binding.tvMgcMore.setText(block.getUiElement().getButton().getText());
break;
}
}
initData(viewModel.bannerList.getBanners());
adapter.setDataList(viewModel.recommendList);
mgcAdapter.setDataList(viewModel.selfMgcList);
}else {
Log.e("报错",homeDiscoverEntityApiResponse.getMessage());
}
});
}
private void initData(List<BannerExtInfoEntity.BannersEntity> banners) {
binding.bannerImg.setAdapter(new bannerAdapter(banners))
.addBannerLifecycleObserver(getViewLifecycleOwner())
.setIntercept(false) //不拦截事件
.setBannerRound(10f)//圆角
.setIndicator(new RectangleIndicator(getContext())) //线条指示器
.setIndicatorHeight(5)
.setIndicatorWidth(6,6)//选中下宽度一致
.setIndicatorGravity(IndicatorConfig.Direction.CENTER)
.setOnBannerListener(new OnBannerListener() {
@Override
public void OnBannerClick(Object data, int position) {
}
});
}
}
修改RecommendAdapter适配器
public class RecommendAdapter extends RecyclerView.Adapter<ViewHolder> {
private final List<HomeDiscoverEntity.DataEntity.BlocksEntity.CreativesEntity> dataList = new ArrayList<>();
private final Context mContext;
public RecommendAdapter(Context context) {
this.mContext = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ItemRecommendDiscoverBinding binding = ItemRecommendDiscoverBinding
.inflate(LayoutInflater.from(parent.getContext()), parent, false);
return new ViewHolder(binding);
}
@SuppressLint("NotifyDataSetChanged")
public void setDataList(List<HomeDiscoverEntity.DataEntity.BlocksEntity.CreativesEntity> data) {
dataList.clear();
dataList.addAll(data);
notifyDataSetChanged();
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
HomeDiscoverEntity.DataEntity.BlocksEntity.CreativesEntity bean = dataList.get(position);
holder.tvTitle.setText(bean.getUiElement().getMainTitle().getTitle());
//holder.tvCount.setText(bean.getResources().get(0).getResourceExtInfo().getPlayCount());
RequestOptions options = new RequestOptions()
.placeholder(R.drawable.ic_banner_loading)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.transform(new CenterCrop(),new RoundedCorners(10))
.error(R.mipmap.ic_launcher);
Glide.with(mContext)
.load(bean.getUiElement().getImage().getImageUrl())
.transition(new DrawableTransitionOptions().crossFade())
.apply(options)
.into(holder.imRecommend);
}
@Override
public int getItemCount() {
return dataList.size();
}
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView tvTitle, tvCount;
ImageView imRecommend;
public ViewHolder(ItemRecommendDiscoverBinding binding) {
super(binding.getRoot());
tvTitle = binding.recommendTitle;
imRecommend = binding.imgRecommend;
tvCount = binding.playCount;
}
}
新增雷达歌单适配器MgcAdapter
public class MgcAdapter extends RecyclerView.Adapter<MgcViewHolder> {
private final List<HomeDiscoverEntity.DataEntity.BlocksEntity.CreativesEntity> dataList = new ArrayList<>();
private final Context mContext;
private OnItemClick onItemClick;
public void setOnItemClick(OnItemClick onItemClick) {
this.onItemClick = onItemClick;
}
public MgcAdapter(Context context) {
this.mContext = context;
}
@NonNull
@Override
public MgcViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ItemRecommendDiscoverBinding binding = ItemRecommendDiscoverBinding
.inflate(LayoutInflater.from(parent.getContext()), parent, false);
return new MgcViewHolder(binding);
}
@SuppressLint("NotifyDataSetChanged")
public void setDataList(List<HomeDiscoverEntity.DataEntity.BlocksEntity.CreativesEntity> data) {
dataList.clear();
dataList.addAll(data);
notifyDataSetChanged();
}
@Override
public void onBindViewHolder(@NonNull MgcViewHolder holder, int position) {
HomeDiscoverEntity.DataEntity.BlocksEntity.CreativesEntity bean = dataList.get(position);
holder.tvTitle.setText(bean.getUiElement().getMainTitle().getTitle());
//holder.tvCount.setText(bean.getResources().get(0).getResourceExtInfo().getPlayCount());
RequestOptions options = new RequestOptions()
.placeholder(R.drawable.ic_banner_loading)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.transform(new CenterCrop(),new RoundedCorners(10))
.error(R.mipmap.ic_launcher);
Glide.with(mContext)
.load(bean.getUiElement().getImage().getImageUrl())
.transition(new DrawableTransitionOptions().crossFade())
.apply(options)
.into(holder.imRecommend);
}
@Override
public int getItemCount() {
return dataList.size();
}
}
class MgcViewHolder extends RecyclerView.ViewHolder {
TextView tvTitle, tvCount;
ImageView imRecommend;
public MgcViewHolder(ItemRecommendDiscoverBinding binding) {
super(binding.getRoot());
tvTitle = binding.recommendTitle;
imRecommend = binding.imgRecommend;
tvCount = binding.playCount;
}
}
修改轮播图适配器bannerAdapter
public class bannerAdapter extends BannerAdapter<BannerExtInfoEntity.BannersEntity, TitleHolder> {
public bannerAdapter(List<BannerExtInfoEntity.BannersEntity> datas) {
super(datas);
}
@Override
public TitleHolder onCreateHolder(ViewGroup parent, int viewType) {
return new TitleHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.banner_title_image,parent,false));
}
@Override
public void onBindView(TitleHolder holder, BannerExtInfoEntity.BannersEntity data, int position, int size) {
Glide.with(holder.imageView)
.load(data.getPic())
.thumbnail(Glide.with(holder.itemView)
.load(R.drawable.ic_banner_loading))
.into(holder.imageView);
holder.textView.setText(data.getTypeTitle());
}
}
class TitleHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView textView;
public TitleHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.bannerImage);
textView = itemView.findViewById(R.id.bannerTitle);
}
}
本篇到此就结束了,有问题欢迎批评指正,觉得不错的也请点个赞 谢谢。