技术开发技术科普贴。it技术

你拥有它,让你Android开发更简单

2016-11-21  本文已影响13519人  诸葛不摇扇
image.png image.png 576d518130720.png
更多: https://github.com/meikoz/Basic
欢迎Follow我的GitHub: https://github.com/meikoz

搭建一个新的Android项目,你会怎么做?

每个人对应用框架的理解不相同,但是最终达到的效果应该是一样:
①降低项目的复杂性
②易扩展、易修改、可重用性强、可维护性强
③职责单一,功能清晰

在android开发项目中,我们首先要考虑每个项目的共同点,比如说:Mvp、网络请求层、Base存放View的基类、Log日志、App crash、刷新加载更多、Loading、广告图、支持ListView,RecyclerView的BaseAdater、通知栏沉浸式、图片加载缓存、底部导航功能...
那么这些功能是每个项目都必须需要的功能,那么可不可以以把这些通用的东西抽取呢?

项目工程搭建

App工程结构搭建:几种常见Android代码架构分析

App工程包结构.png

包名的作用一目了然,在别人接手这个项目的时候就会相对简单

common libraries 开源库的选型

//squareup
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'io.reactivex:rxjava:1.1.5'
compile 'io.reactivex:rxandroid:1.2.0'
compile 'com.jcodecraeer:xrecyclerview:1.2.7'

项目新建重构

对Android一些常用功能做一些整理封装成basic框架,方便Android初始项目快速开发
https://github.com/meikoz/Basic

仿开眼Demo示例:Basic+Retrofit+Okhttp+Rxjava+Glide
请大家多多关注star和提意见给予支持,从自己的一些实践经验来总结这部分通用的东西作为一份善意的分享。

How Usage

Step 1:

Setup 'Basic' dependencies in build.gradle file:

dependencies {
    compile 'com.meikoz.basic:core:1.0.5'
}

Step 2:

Create subclass of Application extends MainApplication,initialize basic BuildConfig.debug is true in super.onCreate() method before for debug print log.

public class BaseApp extends MainApplication {
    @Override
    public void onCreate() {
        RestApi.getInstance().deBug(true);
        super.onCreate();
    }
}

Step 3:

app usage mvp pattern, View and Presenter need things.
View extends BaseView. Presenter extends BasePresenter<View>.

@Implement(MainLogicImpl.class)
public interface MainLogicI {
    void onLoadData2Remote();

    interface MainView extends BaseView {
        void onLoadSuccessHandler(String responce);
    }
}

public class MainLogicImpl extends BasePresenter<MainLogicI.MainView>
    implements MainLogicI {
    @Override
    public void onLoadData2Remote() {
        getView().onLoadSuccessHandler("请求成功,返回的数据");
    }
}

How to initialize use the Activity.

class MainActivity extends Activity implements MainLogicI.MainView {

   @Override
    protected Class getLogicClazz() {
        return your interface MainLogicI class;// MainLogic是Presenter要实现的接口
    }

    @Override
    protected void onInitData2Remote() {
        super.onInitData2Remote();
       ((MainLogicImpl) mPresenter).onLoadData2Remote();
    }

    @Override
    public void onLoadSuccessHandler(String response) {
    //response是Presenter中返回的数据
   }
}

Step 4:

Network: Retrofit + okhttp
根据不同业务生成不同Service

public class ApiManager {
    public static APIService createApi() {
        return RestApi.getInstance().create(APIService.class);
    }
    public static UserService createUserApi() {
        return RestApi.getInstance().create(UserService.class);
    }
}

public interface APIService {
    String BASE_URL ="https://github.com/";
    @GET("api/v1/user")
    Call<Response> getUserInfo(@Query("uid") int uid);
}

cool feature

feature 1:实现 Tab +fragment 功能

QQ图片20161121164914.png
public class MainAty extends AppCompatActivity {
    private TabStripView navigateTabBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        navigateTabBar = (TabStripView) findViewById(R.id.navigateTabBar);
        navigateTabBar.onRestoreInstanceState(savedInstanceState);

        navigateTabBar.addTab(HomeFragment.class,
            new TabStripView.TabParam(R.drawable.ic_tab_strip_icon_feed,
                R.drawable.ic_tab_strip_icon_feed_selected,
                R.string.tab_bar_text_feed));

        navigateTabBar.addTab(DiscoveryFragment.class,
            new TabStripView.TabParam(R.drawable.ic_tab_strip_icon_category,
                R.drawable.ic_tab_strip_icon_category_selected,
                R.string.tab_bar_text_category));

        navigateTabBar.addTab(AutoFragment.class,
            new TabStripView.TabParam(R.drawable.ic_tab_strip_icon_pgc,
                R.drawable.ic_tab_strip_icon_pgc_selected,
                R.string.tab_bar_text_pgc));

        navigateTabBar.addTab(ProfileFragment.class,
            new TabStripView.TabParam(R.drawable.ic_tab_strip_icon_profile,
                R.drawable.ic_tab_strip_icon_profile_selected,
                R.string.tab_bar_text_profile));
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        navigateTabBar.onSaveInstanceState(outState);
    }
}

feature 2:实现仿ios弹窗效果

默认取消确定按钮.png
new SweetAlertDialog.Builder(MainActivity.this)
             .setTitle("标题")
             .setMessage("描述详细内容?")
             .setCancelable(true)
             .setPositiveButton("确认", new SweetAlertDialog.OnDialogClickListener() {
                  @Override
                   public void onClick(Dialog dialog, int which) {
                      Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_SHORT).show();
                   }
              })
             .show();
只有一个确定按钮.png

![Uploading QQ图片20161121171109_005140.png . . .]

new SweetAlertDialog.Builder(MainActivity.this)
             .setTitle("标题")
             .setMessage("描述详细内容?")
             .setCancelable(false)
             .setPositiveButton("确认", new SweetAlertDialog.OnDialogClickListener() {
                  @Override
                   public void onClick(Dialog dialog, int which) {
                      Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_SHORT).show();
                   }
              })
             .show();
左右边都设置按钮.png
new SweetAlertDialog.Builder(MainActivity.this)
             .setTitle("标题")
             .setMessage("描述详细内容?")
             .setCancelable(false)
             .setNegativeButton("左边", new SweetAlertDialog.OnDialogClickListener() {    
              @Override    
              public void onClick(Dialog dialog, int which) {        
                      Toast.makeText(MainActivity.this, "左边" + which, Toast.LENGTH_SHORT).show();    
              }})
             .setPositiveButton("确认", new SweetAlertDialog.OnDialogClickListener() {
                  @Override
                   public void onClick(Dialog dialog, int which) {
                      Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_SHORT).show();
                   }
              })
             .show();

feature 3:CommonAdapter for ListView, RecyclerAdapter for RecyclerView

CommonAdapter for ListView, RecyclerAdapter for RecyclerView
实现 void convert(ViewHolder helper, T item); Replace getView();

feature 4:Logger 漂亮的日志系统

Logger.d(message);
Logger.i(message);
Logger.e(message);
Logger.v(message);
Logger.json(message);


漂亮的日志.png

To do something v2.0.0

Email

zhoujinlongdev@163.com

上一篇下一篇

猜你喜欢

热点阅读