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

Posted 雪の星空朝酱

tags:

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

android简易音乐重构MVVM Java版-新增推荐菜单及侧边栏展示(十二)

关于

  本篇主要实现主界面新增推荐歌单展示及侧滑栏菜单。

效果图

添加侧边栏

  修改activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/home_drawer_menu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/boundary_gray"
    tools:context=".ui.home.MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

        <ImageView
            android:id="@+id/home_top_left_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_home_top_menu"
            app:layout_constraintTop_toTopOf="@id/view_top"
            app:layout_constraintBottom_toBottomOf="@id/view_top"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="@dimen/dp_12" />

        <ImageView
            android:id="@+id/search"
            android:layout_width="@dimen/dp_22"
            android:layout_height="@dimen/dp_25"
            android:layout_marginEnd="@dimen/dp_16"
            app:layout_constraintEnd_toEndOf="parent"
            android:src="@drawable/music_mike"
            app:layout_constraintTop_toTopOf="@id/view_top"
            app:layout_constraintBottom_toBottomOf="@id/view_top" />

        <EditText
            android:id="@+id/ed_search"
            android:layout_width="@dimen/dp_0"
            android:layout_height="@dimen/dp_30"
            android:layout_marginStart="@dimen/dp_16"
            android:layout_marginEnd="@dimen/dp_16"
            app:layout_constraintStart_toEndOf="@id/home_top_left_btn"
            app:layout_constraintEnd_toStartOf="@id/search"
            app:layout_constraintTop_toTopOf="@id/view_top"
            android:alpha="0.5"
            android:textColor="@color/white"
            android:paddingStart="@dimen/dp_8"
            android:paddingEnd="@dimen/dp_8"
            android:paddingTop="@dimen/dp_5"
            android:paddingBottom="@dimen/dp_5"
            app:layout_constraintBottom_toBottomOf="@id/view_top"
            android:background="@drawable/bg_edit_search_gray" />

        <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" />

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_nav"
            android:layout_width="match_parent"
            android:background="?android:attr/windowBackground"
            android:layout_height="@dimen/dp_60"
            app:menu="@menu/bottom_nav"
            app:itemRippleColor="@color/white"
            app:labelVisibilityMode="labeled"
            app:layout_constraintBottom_toBottomOf="parent"
            />

    </androidx.constraintlayout.widget.ConstraintLayout>

       <!--添加DrawerMenuFragment作为侧滑栏页面-->
    <fragment
        android:name="com.tobery.personalmusic.ui.home.menu.DrawerMenuFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        tools:ignore="RtlHardcoded" />
</androidx.drawerlayout.widget.DrawerLayout>

  新增DrawerMenuFragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary">

        <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" />


        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/view_title_bg"
            android:fillViewport="true">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_0"
                android:paddingTop="@dimen/dp_45">

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

</androidx.constraintlayout.widget.ConstraintLayout>

  新增DrawerMenuFragment.java:

public class DrawerMenuFragment extends Fragment 

    private FragmentDrawberMenuBinding binding;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        binding = FragmentDrawberMenuBinding.inflate(inflater);
        viewModel = new ViewModelProvider(this).get(MainViewModel.class);
        binding.setLifecycleOwner(this);
        return binding.getRoot();
    

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) 
        super.onViewCreated(view, savedInstanceState);
    

添加推荐歌单

  修改DiscoverFragmentViewModel,添加请求推荐歌单:

public LiveData<ApiResponse<MainRecommendListBean>> getRecommendList()
        return RetrofitUtils.getmApiUrl().getRecommendPlayList();
    

  修改ApiService,添加推荐歌单api:

 @GET("recommend/resource")
        //推荐歌单
    LiveData<ApiResponse<MainRecommendListBean>> getRecommendPlayList();

  添加MainRecommendListBean实体类(后续轮播图和推荐歌单都要重构,暂时先用以前的代码):

/**
 * 首页推荐歌单的ban
 * Created By Tobey on 2020/9/10
 */
public class MainRecommendListBean 

    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 int 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(int 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 int getUserId() 
            return userId;
        

        public void setUserId(int 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 int 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 int getUserId() 
                return userId;
            

            public void setUserId(int 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;
            

        

以上是关于Android简易音乐重构MVVM Java版-新增推荐菜单及侧边栏展示的主要内容,如果未能解决你的问题,请参考以下文章

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

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

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

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

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

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