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

Posted 雪の星空朝酱

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android简易音乐重构MVVM Java版-BottomNavigationView+viewpager主界面结构相关的知识,希望对你有一定的参考价值。

android简易音乐重构MVVM Java版-BottomNavigationView+viewpager主界面结构(十一)

关于

  本篇主要记录新增discoverFragment(发现界面)及banner展示、以及使用lombok替代冗余的get、set方法等。

效果图

Android使用Lombok

  参考《Android使用GsomFormatPlus+Lombok简化定义实体类
  修改Login的实体类:

@NoArgsConstructor
@Data
public class LoginEntity 

    private int loginType;
    private int code;
    private AccountEntity account;
    private String token;
    private ProfileEntity profile;
    private List<BindingsEntity> bindings;
    private String cookie;

    @NoArgsConstructor
    @Data
    public static class AccountEntity 
        private int id;
        private String userName;
        private int type;
        private int status;
        private int whitelistAuthority;
        private long createTime;
        private String salt;
        private int tokenVersion;
        private int ban;
        private int baoyueVersion;
        private int donateVersion;
        private int vipType;
        private long viptypeVersion;
        private boolean anonimousUser;
        private boolean uninitialized;
    

    @NoArgsConstructor
    @Data
    public static class ProfileEntity 
        private String backgroundImgIdStr;
        private int userId;
        private String avatarImgIdStr;
        private boolean followed;
        private String backgroundUrl;
        private String detailDescription;
        private int userType;
        private int vipType;
        private int gender;
        private int accountStatus;
        private long avatarImgId;
        private String nickname;
        private long backgroundImgId;
        private long birthday;
        private int city;
        private String avatarUrl;
        private boolean defaultAvatar;
        private int province;
        private Object expertTags;
        private ExpertsEntity experts;
        private boolean mutual;
        private Object remarkName;
        private int authStatus;
        private int djStatus;
        private String description;
        private String signature;
        private int authority;
        private String avatarImgId_str;
        private int followeds;
        private int follows;
        private int eventCount;
        private Object avatarDetail;
        private int playlistCount;
        private int playlistBeSubscribedCount;

        @NoArgsConstructor
        @Data
        public static class ExpertsEntity 
        
    

    @NoArgsConstructor
    @Data
    public static class BindingsEntity 
        private int userId;
        private String url;
        private boolean expired;
        private String tokenJsonStr;
        private long bindingTime;
        private int expiresIn;
        private int refreshTime;
        private long id;
        private int type;
    

  然后删除对应的Login_bean实体类,替换一下其他使用到的地方即可。

添加cookie和cache引用

  修改moudle的build文件新增cookie引用:

// CookieJar
    implementation 'com.github.franmontiel:PersistentCookieJar:v1.0.1'

新增 ApplicationContextProvider

public class ApplicationContextProvider extends ContentProvider 

    @SuppressLint("StaticFieldLeak")
    public static Context context;

    @Override
    public boolean onCreate() 
        context = getContext();
        return false;
    

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) 
        return null;
    

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) 
        return null;
    

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) 
        return null;
    

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) 
        return 0;
    

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) 
        return 0;
    

  然后再androidmanifest.xml的application中添加provider:

<provider
            android:name=".ApplicationContextProvider"
            android:authorities="$applicationId.contextProvider"
            android:exported="false" />

  然后定义ContextProvider.java用来单例获取context:

public class ContextProvider 

    @SuppressLint("StaticFieldLeak")
    private static volatile ContextProvider instance;
    private final Context mContext;

    private ContextProvider(Context context) 
        mContext = context;
    

    /**
     * 获取实例
     */
    public static ContextProvider get() 
        if (instance == null) 
            synchronized (ContextProvider.class) 
                if (instance == null) 
                    Context context = ApplicationContextProvider.context;
                    if (context == null) 
                        throw new IllegalStateException("context == null");
                    
                    instance = new ContextProvider(context);
                
            
        
        return instance;
    

    /**
     * 获取上下文
     */
    public Context getContext() 
        return mContext;
    

    public Application getApplication() 
        return (Application) mContext.getApplicationContext();
    

添加cookie和cache

  修改RetrofitUtils.java

public class RetrofitUtils 
    /**
     * 单例模式
     */
    public static ApiService apiService;
    public static ApiService getmApiUrl()
        if (apiService == null)
            synchronized (RetrofitUtils.class)
                if (apiService == null)
                    apiService = new RetrofitUtils().getRetrofit();
                
            
        
        return apiService;
    

    private ApiService getRetrofit() 
        //初始化Retrofit
        ApiService apiService = initRetrofit(initOkHttp()).create(ApiService.class);
        return apiService;
    

    private OkHttpClient initOkHttp() 
        File cacheDir = ContextProvider.get().getContext().getCacheDir();
        long cacheSize = 10L * 1024L * 1024L; // 10 MiB
        ClearableCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(ContextProvider.get().getContext()));
        return new OkHttpClient().newBuilder()
                .readTimeout(Constant.DEFAULT_TIME, TimeUnit.SECONDS) //设置读取超时时间
                .connectTimeout(Constant.DEFAULT_TIME, TimeUnit.SECONDS) //设置请求超时时间
                .writeTimeout(Constant.DEFAULT_TIME, TimeUnit.SECONDS) //设置写入超时时间
                .addInterceptor(new LogInterceptor())  //添加打印拦截器
                .retryOnConnectionFailure(true) //设置出错进行重新连接
                .cache(new Cache(cacheDir, cacheSize)) //cache
                .cookieJar(cookieJar) //cookie
                .build();
    

    /**
     * 初始化Retrofit
     */
    @NonNull
    private Retrofit initRetrofit(OkHttpClient client)
        return new Retrofit.Builder()
                .client(client)
                .baseUrl(Constant.BaseUrl)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addCallAdapterFactory(LiveDataCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    

修改添加侧滑栏和底部导航栏

  修改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>

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        tools:ignore="RtlHardcoded" />
</androidx.drawerlayout.widget.DrawerLayout>

  新增侧边栏按钮ic_home_top_menu.xml

<!--
  ~ Copyright (c) 2022 Station. All rights reserved.
  -->

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="16dp"
    android:viewportWidth="24"
    android:viewportHeight="16">
    <path
        android:pathData="M0,16H24V14H0V16ZM0,2H24V0H0V2ZM0,9H24V7H0V9Z"
        android:fillColor="#ffffff"
        android:fillType="evenOdd" />
</vector>

  新增editview的背景bg_edit_search_gray.xml

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android"Android简易音乐重构MVVM Java版-新增启动动画

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

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

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

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

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