新增我的推荐歌单通过ViewPager2+RadioGroup重构主界面( 简易音乐 六)
Posted Tobey_r1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了新增我的推荐歌单通过ViewPager2+RadioGroup重构主界面( 简易音乐 六)相关的知识,希望对你有一定的参考价值。
关于
在本篇开始之前,我发现之前以为的在fragment中使用轮播图在切换的过程中会导致重新加载banner的问题,其实是因为我使用的开启事务里面的replace方法调用了remove,导致碎片被移除,自然就banner也销毁了。上一篇请看主界面绘制、加载轮播图、减少apk体积( 简易音乐 五)
本想着主界面代码简洁一点,经过这次重构,主界面代码增加了一部分,但是还好,我们现在看下运行效果。
效果
因为我用的一个工具网站视频转GIF(免费),只支持15秒及以内转换,所以很多时候比较仓促,也懒得搞两段,,理解一下,按着教程来不会差的:
添加Api接口
/**
* 每日推荐歌单*/
@GET("recommend/resource")
Observable<RecommendListBean> getRecommendPlayList();
添加对应实体类RecommendListBean
:
/**
* 首页推荐歌单的ban
* Created By Tobey on 2021/7/01
*/
public class RecommendListBean {
private int code;
private boolean featureFirst;
private boolean haveRcmdSongs;
private List<RecommendBean> recommend;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public boolean isFeatureFirst() {
return featureFirst;
}
public void setFeatureFirst(boolean featureFirst) {
this.featureFirst = featureFirst;
}
public boolean isHaveRcmdSongs() {
return haveRcmdSongs;
}
public void setHaveRcmdSongs(boolean haveRcmdSongs) {
this.haveRcmdSongs = haveRcmdSongs;
}
public List<RecommendBean> getRecommend() {
return recommend;
}
public void setRecommend(List<RecommendBean> recommend) {
this.recommend = recommend;
}
public static class RecommendBean {
private long id;
private int type;
private String name;
private String copywriter;
private String picUrl;
private long playcount;
private long createTime;
private CreatorBean creator;
private int trackCount;
private long userId;
private String alg;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCopywriter() {
return copywriter;
}
public void setCopywriter(String copywriter) {
this.copywriter = copywriter;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
public long getPlaycount() {
return playcount;
}
public void setPlaycount(long playcount) {
this.playcount = playcount;
}
public long getCreateTime() {
return createTime;
}
public void setCreateTime(long createTime) {
this.createTime = createTime;
}
public CreatorBean getCreator() {
return creator;
}
public void setCreator(CreatorBean creator) {
this.creator = creator;
}
public int getTrackCount() {
return trackCount;
}
public void setTrackCount(int trackCount) {
this.trackCount = trackCount;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getAlg() {
return alg;
}
public void setAlg(String alg) {
this.alg = alg;
}
public static class CreatorBean {
private String backgroundImgIdStr;
private Object remarkName;
private boolean mutual;
private long userId;
private long avatarImgId;
private int djStatus;
private boolean followed;
private String backgroundUrl;
private long backgroundImgId;
private String detailDescription;
private boolean defaultAvatar;
private Object expertTags;
private int accountStatus;
private int vipType;
private int province;
private int gender;
private String avatarUrl;
private int authStatus;
private int userType;
private String nickname;
private long birthday;
private int city;
private String avatarImgIdStr;
private String description;
private String signature;
private int authority;
public String getBackgroundImgIdStr() {
return backgroundImgIdStr;
}
public void setBackgroundImgIdStr(String backgroundImgIdStr) {
this.backgroundImgIdStr = backgroundImgIdStr;
}
public Object getRemarkName() {
return remarkName;
}
public void setRemarkName(Object remarkName) {
this.remarkName = remarkName;
}
public boolean isMutual() {
return mutual;
}
public void setMutual(boolean mutual) {
this.mutual = mutual;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public long getAvatarImgId() {
return avatarImgId;
}
public void setAvatarImgId(long avatarImgId) {
this.avatarImgId = avatarImgId;
}
public int getDjStatus() {
return djStatus;
}
public void setDjStatus(int djStatus) {
this.djStatus = djStatus;
}
public boolean isFollowed() {
return followed;
}
public void setFollowed(boolean followed) {
this.followed = followed;
}
public String getBackgroundUrl() {
return backgroundUrl;
}
public void setBackgroundUrl(String backgroundUrl) {
this.backgroundUrl = backgroundUrl;
}
public long getBackgroundImgId() {
return backgroundImgId;
}
public void setBackgroundImgId(long backgroundImgId) {
this.backgroundImgId = backgroundImgId;
}
public String getDetailDescription() {
return detailDescription;
}
public void setDetailDescription(String detailDescription) {
this.detailDescription = detailDescription;
}
public boolean isDefaultAvatar() {
return defaultAvatar;
}
public void setDefaultAvatar(boolean defaultAvatar) {
this.defaultAvatar = defaultAvatar;
}
public Object getExpertTags() {
return expertTags;
}
public void setExpertTags(Object expertTags) {
this.expertTags = expertTags;
}
public int getAccountStatus() {
return accountStatus;
}
public void setAccountStatus(int accountStatus) {
this.accountStatus = accountStatus;
}
public int getVipType() {
return vipType;
}
public void setVipType(int vipType) {
this.vipType = vipType;
}
public int getProvince() {
return province;
}
public void setProvince(int province) {
this.province = province;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public String getAvatarUrl() {
return avatarUrl;
}
public void setAvatarUrl(String avatarUrl) {
this.avatarUrl = avatarUrl;
}
public int getAuthStatus() {
return authStatus;
}
public void setAuthStatus(int authStatus) {
this.authStatus = authStatus;
}
public int getUserType() {
return userType;
}
public void setUserType(int userType) {
this.userType = userType;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public long getBirthday() {
return birthday;
}
public void setBirthday(long birthday) {
this.birthday = birthday;
}
public int getCity() {
return city;
}
public void setCity(int city) {
this.city = city;
}
public String getAvatarImgIdStr() {
return avatarImgIdStr;
}
public void setAvatarImgIdStr(String avatarImgIdStr) {
this.avatarImgIdStr = avatarImgIdStr;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getSignature() {
return signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
public int getAuthority() {
return authority;
}
public void setAuthority(int authority) {
this.authority = authority;
}
}
}
}
重构主界面
修改主界面布局文件activity_main.xml
(使用viewpager2):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!--android:background="@color/whitesmoke"-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<RadioGroup
android:id="@+id/rg_tabs"
android:layout_width="match_parent"
android:layout_height="54dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="5dp">
<RadioButton
android:id="@+id/rb_main"
style="@style/tab_RadioButton_normal"
android:drawableTop="@drawable/bottom_home_style"
android:text="@string/home_page"
/>
<RadioButton
android:id="@+id/rb_contract"
style="@style/tab_RadioButton_normal"
android:drawableTop="@drawable/bottom_topic_style"
android:text="@string/cloud"
/>
<RadioButton
android:id="@+id/rb_me"
style="@style/tab_RadioButton_normal"
android:drawableTop="@drawable/bottom_me_style"
android:text="@string/me"
/>
</RadioGroup>
</LinearLayout>
</FrameLayout>
修改MainActivity.java代码:
@Route(path = Config.ROUTE_HOME)
public class MainActivity extends BaseActivity {
@BindView(R.id.rg_tabs)
RadioGroup rgTabs;
@BindView(R.id.view_pager)
ViewPager2 viewPager2;
private List<Fragment> fragmentList;
private long firstTime = 0;
//监听viewpager2页面切换监听
private ViewPager2.OnPageChangeCallback onPageChange = new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
rgTabs.setOnCheckedChangeListener(null);
switch (position){
case 0:
rgTabs.check(R.id.rb_main);
break;
case 1:
rgTabs.check(R.id.rb_contract);
break;
case 2:
rgTabs.check(R.id.rb_me);
break;
}
rgTabs.setOnCheckedChangeListener(onCheckedChange);
}
};
private RadioGroup.OnCheckedChangeListener onCheckedChange = new RadioGroup以上是关于新增我的推荐歌单通过ViewPager2+RadioGroup重构主界面( 简易音乐 六)的主要内容,如果未能解决你的问题,请参考以下文章
Android简易音乐重构MVVM Java版-新增推荐雷达歌单详情列表界面(十八)
Android简易音乐重构MVVM Java版-新增推荐雷达歌单详情列表界面(十八)
Android简易音乐重构MVVM Java版-新增推荐雷达歌单及重构首页(十三)
Android简易音乐重构MVVM Java版-新增推荐菜单及侧边栏展示