您的位置:首页 > 移动开发 > Android开发

Android实战(橘子娱乐)-首页(第一篇 MVP框架基本搭建和实现准备)

2017-10-12 13:56 507 查看
前言:     “橘子娱乐”这个实战APP参考了大神的开源项目:    https://github.com/LRH1993/SmartRecom  ,我们将采用MVP框架以及现在比较热门的开源项目去实现,目的在于练手,实现如下功能:            1.电影展示以及播放跳转            2.音乐播放               3.视频直播            4.搞笑段子            5.小说书城            6.本地简单的登录功能     版权声明:            该APP的数据接口均来自互联网,只做学习所用,若有侵权请告知我将立即删除对应内容。首先来看一下首页效果图:一、看完动态图,我们总结一下首页需要实现的功能:        1.侧滑菜单        2.顶部toolbar(在更随滑动隐藏和现实)        3.电影推荐展示,包含功能:下拉刷新、顶部banner、底部显示提示,中间两列和一列混排        4.sharedpreferences保存登录状态,根据登录状态显示不同的头像和文字二、开源项目及技术点:    1.NavigationView+DrawerLayout    2.MVP    3.sharedpreferences    4.IRecyclerView    5.ButterKnife    6.RxJava2    7.Retrofit2    8.okhttp    9.com.youth.banner    10.SuperTextView     三、准备    我们需要实现首页的这些功能呢,我们首先需要做一些基础性的准备,比如依赖相关库、搭建MVP框架的结构以及一些基本通用的类实现。开源库的依赖我就不细讲了大概过一下,下面是首页需要的依赖(app build.gradle):
compile 'com.android.support:recyclerview-v7:25.3.1'
//视图绑定 butterknife
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
/*IRecyclerView*/
compile 'com.github.Aspsine:IRecyclerView:0.0.5'
//引入rxjava
compile 'io.reactivex.rxjava2:rxjava:2.0.4'
//引入rxandroid
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
//引入retrofit
compile 'com.squareup.retrofit2:retrofit:2.1.0'
//引入rxjava适配器,方便rxjava与retrofit的结合
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
//引入json转换器,方便将返回的数据转换为json格式
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
//引入okhttp
compile 'com.squareup.okhttp3:okhttp:3.5.0'
//引入Log拦截器,方便DEBUG模式输出log信息
compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'
//自动轮播
compile 'com.youth.banner:banner:1.4.9'
上面引入ButterKnife时还需要设置:    
apply plugin: 'com.neenbedankt.android-apt'
同时需要在project build.gradle文件中设置:
dependencies {
//这里配置 apt 供butterknife使用
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
在依赖成功以后,我们需要编写baseActivity来实现一些统一的操作封装,提供给以后的Activity继承:
package com.andy.orange.base;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import com.andy.orange.R;
import com.andy.orange.utils.SettingStatusBar;
import butterknife.ButterKnife;
import butterknife.Unbinder;
/**
* Created by Andy Lau on 2017/8/9.
* 定义一个基础的Activity抽象类,做一些通用的操作
*/
public abstract class BaseActivity extends AppCompatActivity {
public Context mContext;
private Unbinder mUnbinder;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
beforeSetContentView();
setContentView(getChildLayoutId());
//设置默认的状态栏颜色
setStatusBarColor();
mContext=this;
mUnbinder= ButterKnife.bind(this);
this.initPresenter();
this.initView();
}
/*
* 设置默认的状态栏颜色(4.4系统以上有效)
**/
protected void setStatusBarColor(){
SettingStatusBar.setColor(this,getResources().getColor(R.color.colorPrimary));
};
/*
* 用户自定义状态栏颜色(4.4系统以上有效)
**/
protected void setStatusBarColor(int color){
SettingStatusBar.setColor(this,color);
};
/*
* 在设置布局之前的配置
**/
private void beforeSetContentView(){
//设置不显示标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置竖屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
/*
*定义一个获取子类layoutId的抽象方法,在子类中实现
*/
public abstract int getChildLayoutId();
/*
* 初始化Presenter
**/
public abstract void initPresenter();
/*
* 初始化view
**/
public abstract void initView();
/*
* 通过class无返回值跳转页面,并传递Bundle
*
*/
public void startActivity(Class<?> cls,Bundle bundle){
Intent intent=new Intent();
intent.setClass(this,cls);
if (bundle!=null){
intent.putExtras(bundle);
}
startActivity(intent);
}
/*
* 通过class不带返回值跳转页面,无Bundle参数
*
  */
public void startActivity(Class cls){
startActivity(cls,null);
}
/*
* 通过class带返回值跳转页面,有Bundle参数
*
  */
public void  startActivityForResult(Class cls,Bundle bundle,int requestCode){
Intent intent=new Intent(this,cls);
if(bundle!=null){
intent.putExtras(bundle);
}
startActivityForResult(intent,requestCode);
}
/*
* 通过class带返回值跳转页面,无Bundle参数
*
  */
public void  startActivityForResult(Class cls,int requestCode){
startActivityForResult(cls,null,requestCode);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
//用来重置绑定的view
mUnbinder.unbind();
}
}
    从BaseActivity代码可以看出我们做了一些统一的操作,比如 在onCreate时绑定了ButterKnife,在onDestroy中解绑了ButterKnife  ,继承了该类的Actvity都不需要重复做绑定和解绑操作,然后我们也开放了一些抽象方法比如 initPresenter()initView() getChildLayoutId()等,提供给继承类实现,同时还封装了一些基本的启动Activity的方法,ok这就是BaseActivity全部功能,至于SettingStatusBar工具类代码由于篇幅原因这里就不贴出来讲解了,基础类封装好以后,我们来搭我们的MVP架子。下面是mvp的包结构截图:     ok三层结构都建立完毕,presenter和model层包含接口与实现类,最后实现的效果就是,在model中通过网络请求获取到数据,保存到bean(我们还需要建立一个bean包,放置我们的bean类)集合中,presenter通过获取model实例获取到保存数据的bean集合,然后通过绑定的view实例传递给view层,view层等同于Activity因为我们会让Activity去继承对应的view接口。这样我们就实现了数据从model层传递到Activity了。    这一篇我们就实现这么多,篇幅太长不利于学习,第二篇文章我们将实现,主页面的布局,同时讲解主页Activity部分的代码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐