每个人对应用框架的理解不相同,但是最终达到的效果应该是一样: ①降低项目的复杂性 ②易扩展、易修改、可重用性强、可维护性强 ③职责单一,功能清晰
在android开发项目中,我们首先要考虑每个项目的共同点,比如说:Mvp、网络请求层、Base存放View的基类、Log日志、App crash、刷新加载更多、Loading、广告图、支持ListView,RecyclerView的BaseAdater、通知栏沉浸式、图片加载缓存、底部导航功能... 那么这些功能是每个项目都必须需要的功能,那么可不可以以把这些通用的东西抽取呢?
App工程结构搭建:几种常见Android代码架构分析
包名的作用一目了然,在别人接手这个项目的时候就会相对简单
//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初始项目快速开发 Basic框架 仿开眼Demo示例:Basic+Retrofit+Okhttp+Rxjava+Glide 请大家多多关注star和提意见给予支持,从自己的一些实践经验来总结这部分通用的东西作为一份善意的分享。
@User:码农小阿新 @GitHub: https://github.com/meikoz
Setup 'Basic' dependencies in build.gradle file:
dependencies { compile 'com.meikoz.basic:core:1.0.5' }
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(); } }
app usage mvp pattern, View and Presenter need things. View extends BaseView. Presenter extends BasePresenter.
@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中返回的数据 } }
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); }
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); } }
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();
![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();
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();
CommonAdapter for ListView, RecyclerAdapter for RecyclerView 实现 void convert(ViewHolder helper, T item); Replace getView();
Logger.d(message); Logger.i(message); Logger.e(message); Logger.v(message); Logger.json(message);
Application onreate方法中初始化 异常保存本地,可以进行服务上传 AndroidCrash.getInstance().init(this);
CustomViewPageAdapter adapter = new CustomViewPageAdapter(getActivity(), getAdData()); mViewpage.updateIndicatorView(getAdData().size(), 0); mViewpage.setAdapter(adapter); mViewpage.startScorll();
zhoujinlongdev@163.com
Copyright 2016 meikoz Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.