├── .gitignore ├── .idea ├── codeStyles │ └── Project.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── baozi │ │ └── mvp │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── baozi │ │ │ └── mvp │ │ │ ├── MVPManager.java │ │ │ ├── StartFactory.java │ │ │ ├── annotation │ │ │ └── JView.java │ │ │ ├── base │ │ │ ├── BaseActivity.java │ │ │ └── BaseFragment.java │ │ │ ├── model │ │ │ ├── BaseModel.java │ │ │ └── LoadDataModel.java │ │ │ ├── presenter │ │ │ ├── BasePresenter.java │ │ │ ├── EmptyPresenter.java │ │ │ ├── LoadMorePresenter.java │ │ │ ├── PagerFragmentPresenter.java │ │ │ └── PagerPresenter.java │ │ │ ├── tempalet │ │ │ ├── TemplateActivity.java │ │ │ ├── TemplateFragment.java │ │ │ ├── helper │ │ │ │ ├── load │ │ │ │ │ └── LoadHelper.java │ │ │ │ └── toolbar │ │ │ │ │ ├── BaseToolBarHelperImpl.java │ │ │ │ │ ├── EmptyToolbarHelperImpl.java │ │ │ │ │ ├── SimpleToolbarHelperImpl.java │ │ │ │ │ └── ToolbarHelper.java │ │ │ ├── options │ │ │ │ ├── ContentOptions.java │ │ │ │ └── ToolbarOptions.java │ │ │ └── weight │ │ │ │ └── LoadingPager.java │ │ │ ├── utils │ │ │ └── StatusBarUtil.java │ │ │ └── view │ │ │ ├── BaseView.java │ │ │ ├── LoadMoreView.java │ │ │ ├── PageFragmentView.java │ │ │ ├── PageView.java │ │ │ ├── TabLayoutView.java │ │ │ ├── ToolbarView.java │ │ │ └── UIView.java │ └── res │ │ ├── drawable-xhdpi │ │ └── back.9.png │ │ ├── drawable │ │ └── progress_bar_horizontal_states.xml │ │ ├── layout │ │ ├── template_layout.xml │ │ └── toolbar_template_default.xml │ │ ├── values-v21 │ │ └── styles.xml │ │ └── values │ │ ├── attrs.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── baozi │ └── mvp │ └── ExampleUnitTest.java ├── build.gradle ├── demo ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── baozi │ │ └── demo │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── assets │ │ └── a.gif │ ├── java │ │ └── com │ │ │ └── baozi │ │ │ └── demo │ │ │ ├── App.java │ │ │ ├── model │ │ │ └── MainModel.java │ │ │ ├── persenter │ │ │ ├── DemoPresenter.java │ │ │ └── MainPresenter.java │ │ │ └── ui │ │ │ ├── at │ │ │ ├── BaseAt.java │ │ │ ├── LoadingAt.java │ │ │ ├── MainAt.java │ │ │ ├── PageAt.java │ │ │ ├── TemplateAt.java │ │ │ └── TestAt.java │ │ │ └── fg │ │ │ ├── DemoChildFg.java │ │ │ └── DemoFg.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── bg_toolbar.xml │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── at_load.xml │ │ ├── at_main.xml │ │ ├── at_template.xml │ │ ├── at_test.xml │ │ ├── at_viewpage.xml │ │ ├── empty_layout.xml │ │ ├── fg_demo.xml │ │ ├── fg_demo_child.xml │ │ ├── item_test.xml │ │ └── load_layout.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── baozi │ └── demo │ └── ExampleUnitTest.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | xmlns:android 14 | 15 | ^$ 16 | 17 | 18 | 19 |
20 |
21 | 22 | 23 | 24 | xmlns:.* 25 | 26 | ^$ 27 | 28 | 29 | BY_NAME 30 | 31 |
32 |
33 | 34 | 35 | 36 | .*:id 37 | 38 | http://schemas.android.com/apk/res/android 39 | 40 | 41 | 42 |
43 |
44 | 45 | 46 | 47 | .*:name 48 | 49 | http://schemas.android.com/apk/res/android 50 | 51 | 52 | 53 |
54 |
55 | 56 | 57 | 58 | name 59 | 60 | ^$ 61 | 62 | 63 | 64 |
65 |
66 | 67 | 68 | 69 | style 70 | 71 | ^$ 72 | 73 | 74 | 75 |
76 |
77 | 78 | 79 | 80 | .* 81 | 82 | ^$ 83 | 84 | 85 | BY_NAME 86 | 87 |
88 |
89 | 90 | 91 | 92 | .* 93 | 94 | http://schemas.android.com/apk/res/android 95 | 96 | 97 | ANDROID_ATTRIBUTE_ORDER 98 | 99 |
100 |
101 | 102 | 103 | 104 | .* 105 | 106 | .* 107 | 108 | 109 | BY_NAME 110 | 111 |
112 |
113 |
114 |
115 |
116 |
-------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 15 | 16 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 24 | 41 | 42 | 43 | 44 | 45 | 46 | 48 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JMVP 2 | 一个高效,快速的Android MVP框架 3 | 节省你的时间,不同于千遍一律的mvp框架. 4 | 该框架不会使用Cantract契约类 5 | 6 | 上手简单 7 | 8 | 依赖 9 | ``` 10 | implementation 'com.github.Jlanglang:JMVP:1.0.4' 11 | ``` 12 | 13 | 14 | 15 | #### 1.不断优化更新 16 | 17 | 18 | QQ交流群:573676189 493180098 19 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | minSdkVersion 14 7 | versionCode 1 8 | versionName "1.0" 9 | } 10 | } 11 | dependencies { 12 | implementation 'com.android.support:appcompat-v7:28.0.0' 13 | implementation 'com.android.support:design:28.0.0' 14 | implementation 'com.android.support:support-v4:28.0.0' 15 | } 16 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in J:\Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/baozi/mvp/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/MVPManager.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp; 2 | 3 | import android.support.annotation.AnimRes; 4 | import android.support.annotation.AnimatorRes; 5 | import android.support.annotation.StyleRes; 6 | 7 | import com.baozi.mvp.tempalet.options.ContentOptions; 8 | import com.baozi.mvp.tempalet.options.ToolbarOptions; 9 | 10 | /** 11 | * Created by Administrator on 2017/8/15 0015. 12 | */ 13 | 14 | public class MVPManager { 15 | private static ToolbarOptions mToolbarOptions; 16 | private static ContentOptions mContentOptions; 17 | @AnimRes 18 | @AnimatorRes 19 | private static int enterAnim; 20 | @AnimRes 21 | @AnimatorRes 22 | private static int exitAnim; 23 | @AnimRes 24 | @AnimatorRes 25 | private static int enterPopAnim; 26 | @AnimRes 27 | @AnimatorRes 28 | private static int exitPopAnim; 29 | private static int transactionStyle; 30 | 31 | public static void setFragmentAnim(@AnimatorRes @AnimRes int enter, 32 | @AnimatorRes @AnimRes int exit, @AnimatorRes @AnimRes int popEnter, 33 | @AnimatorRes @AnimRes int popExit) { 34 | enterAnim = enter; 35 | exitAnim = exit; 36 | enterPopAnim = popEnter; 37 | exitPopAnim = popExit; 38 | } 39 | 40 | public static void setFragmentTransaction(@StyleRes int styleRes) { 41 | transactionStyle = styleRes; 42 | } 43 | 44 | public static int getEnterAnim() { 45 | return enterAnim; 46 | } 47 | 48 | public static int getExitAnim() { 49 | return exitAnim; 50 | } 51 | 52 | public static int getEnterPopAnim() { 53 | return enterPopAnim; 54 | } 55 | 56 | public static int getExitPopAnim() { 57 | return exitPopAnim; 58 | } 59 | 60 | public static int getTransactionStyle() { 61 | return transactionStyle; 62 | } 63 | 64 | 65 | public static ToolbarOptions getToolbarOptions() { 66 | if (mToolbarOptions == null) { 67 | return ToolbarOptions.Create(); 68 | } 69 | return mToolbarOptions.clone(); 70 | } 71 | 72 | public static void setToolbarOptions(ToolbarOptions toolbarOptions) { 73 | mToolbarOptions = toolbarOptions; 74 | } 75 | 76 | /** 77 | * 防止影响全局,第二次获取只能获取clone的副本; 78 | * 79 | * @return 80 | */ 81 | public static ContentOptions getContentOptions() { 82 | if (mContentOptions == null) { 83 | mContentOptions = ContentOptions.create(); 84 | } 85 | return mContentOptions.clone(); 86 | } 87 | 88 | public static void setContentOptions(ContentOptions templetContentOptions) { 89 | mContentOptions = templetContentOptions; 90 | } 91 | 92 | public static int getStatusDrawable() { 93 | return MVPManager.getToolbarOptions().getStatusDrawable(); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/StartFactory.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.support.v4.app.Fragment; 7 | import android.support.v4.app.FragmentTransaction; 8 | import android.support.v7.app.AppCompatActivity; 9 | 10 | public class StartFactory { 11 | /** 12 | * 跳转fragment 13 | * 14 | * @param tofragment 15 | */ 16 | 17 | public static void startFragment(AppCompatActivity activity, Fragment tofragment) { 18 | startFragment(activity, tofragment, null); 19 | } 20 | 21 | /** 22 | * @param activity 23 | * @param fragment 跳转的fragment 24 | * @param tag fragment的标签 25 | */ 26 | 27 | public static void startFragment(AppCompatActivity activity, Fragment fragment, String tag) { 28 | startFragment(activity, fragment, tag, true); 29 | } 30 | 31 | /** 32 | * @param activity 33 | * @param fragment 跳转的fragment 34 | * @param tag fragment的标签 35 | */ 36 | public static void startFragment(AppCompatActivity activity, Fragment fragment, String tag, boolean isAdd) { 37 | startFragment(activity, fragment, tag, 38 | MVPManager.getEnterAnim(), 39 | MVPManager.getExitAnim(), 40 | MVPManager.getEnterPopAnim(), 41 | MVPManager.getExitPopAnim(), isAdd); 42 | } 43 | 44 | 45 | /** 46 | * @param fragment 跳转的fragment 47 | * @param tag fragment的标签 48 | */ 49 | public static void startFragment(AppCompatActivity activity, Fragment fragment, String tag, int enter, int popExit) { 50 | startFragment(activity, fragment, tag, enter, popExit, true); 51 | } 52 | 53 | public static void startFragment(AppCompatActivity activity, Fragment fragment, String tag, int enter, int popExit, boolean isAddBack) { 54 | startFragment(activity, fragment, tag, 55 | enter, 56 | 0, 57 | 0, 58 | popExit, isAddBack); 59 | } 60 | 61 | public static void startFragment(AppCompatActivity activity, Fragment fragment, String tag, int enterAnim, int exitAnim, int popEnter, int popExit, boolean isAddBack) { 62 | FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction(); 63 | fragmentTransaction.setCustomAnimations(enterAnim, exitAnim, popEnter, popExit); 64 | fragmentTransaction.add(android.R.id.content, fragment, tag); 65 | if (isAddBack) { 66 | fragmentTransaction.addToBackStack(tag); 67 | } 68 | fragmentTransaction.commitAllowingStateLoss(); 69 | } 70 | 71 | 72 | /** 73 | * @param rootFragment Activity内部fragment 74 | * @param containerId fragment父容器 75 | */ 76 | public static void replaceFragment(AppCompatActivity context, Fragment rootFragment, int containerId) { 77 | FragmentTransaction fragmentTransaction = context.getSupportFragmentManager().beginTransaction(); 78 | fragmentTransaction.add(containerId, rootFragment); 79 | fragmentTransaction.commitAllowingStateLoss(); 80 | } 81 | 82 | /** 83 | * 跳转Activity 84 | */ 85 | public static void startActivity(Context context, Class aClass) { 86 | Intent intent = new Intent(context, aClass); 87 | context.startActivity(intent); 88 | } 89 | 90 | /** 91 | * 跳转Activity 92 | */ 93 | public static void startActivity(Context context, Class aClass, Bundle bundle) { 94 | Intent intent = new Intent(context, aClass); 95 | intent.putExtras(bundle); 96 | context.startActivity(intent); 97 | } 98 | 99 | /** 100 | * 跳转Activity 101 | */ 102 | public static void startActivity(Context context, Class aClass, Bundle bundle, int flag) { 103 | Intent intent = new Intent(context, aClass); 104 | intent.putExtras(bundle); 105 | intent.addFlags(flag); 106 | context.startActivity(intent); 107 | } 108 | 109 | /** 110 | * 跳转Activity 111 | */ 112 | public static void startActivity(Context context, Class aClass, int flag) { 113 | Intent intent = new Intent(context, aClass); 114 | intent.addFlags(flag); 115 | context.startActivity(intent); 116 | } 117 | 118 | } 119 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/annotation/JView.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.annotation; 2 | 3 | import com.baozi.mvp.presenter.BasePresenter; 4 | import com.baozi.mvp.presenter.EmptyPresenter; 5 | 6 | import java.lang.annotation.ElementType; 7 | import java.lang.annotation.Retention; 8 | import java.lang.annotation.RetentionPolicy; 9 | import java.lang.annotation.Target; 10 | 11 | @Retention(RetentionPolicy.RUNTIME) 12 | @Target(ElementType.TYPE) 13 | public @interface JView { 14 | /** 15 | * @return presenter的class 16 | */ 17 | Class p() default EmptyPresenter.class; 18 | 19 | /** 20 | * @return 布局id 21 | */ 22 | int layout() default 0; 23 | 24 | /** 25 | * 打开加载设置 26 | */ 27 | boolean openLoading() default false; 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/base/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.base; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.os.Bundle; 7 | import android.support.annotation.ColorRes; 8 | import android.support.annotation.DrawableRes; 9 | import android.support.annotation.IdRes; 10 | import android.support.annotation.LayoutRes; 11 | import android.support.annotation.NonNull; 12 | import android.support.annotation.Nullable; 13 | import android.support.v7.app.AppCompatActivity; 14 | import android.util.Log; 15 | import android.util.SparseArray; 16 | import android.view.LayoutInflater; 17 | import android.view.View; 18 | 19 | import com.baozi.mvp.MVPManager; 20 | import com.baozi.mvp.annotation.JView; 21 | import com.baozi.mvp.presenter.BasePresenter; 22 | import com.baozi.mvp.presenter.EmptyPresenter; 23 | import com.baozi.mvp.tempalet.helper.load.LoadHelper; 24 | import com.baozi.mvp.tempalet.options.ContentOptions; 25 | import com.baozi.mvp.tempalet.weight.LoadingPager; 26 | import com.baozi.mvp.view.UIView; 27 | 28 | /** 29 | * @author jlanglang 2016/1/5 9:42 30 | */ 31 | public abstract class BaseActivity extends AppCompatActivity 32 | implements UIView { 33 | public final String TAG = this.getClass().getSimpleName();//tag不要用反射的形式取 34 | protected T mPresenter; 35 | protected View statusBarView; 36 | private SparseArray mViews; 37 | private View mContentView; 38 | private LoadHelper loadHelper; 39 | private JView jView; 40 | private ContentOptions contentOptions; 41 | 42 | @Override 43 | protected void onCreate(Bundle savedInstanceState) { 44 | super.onCreate(savedInstanceState); 45 | jView = this.getClass().getAnnotation(JView.class); 46 | 47 | //创建presenter 48 | mPresenter = initPresenter(); 49 | //绑定Activity 50 | mPresenter.onAttach(this); 51 | 52 | getLifecycle().addObserver(mPresenter); 53 | 54 | // 初始化ContentView 55 | mContentView = initView(getLayoutInflater(), savedInstanceState); 56 | // 包装加载 57 | if (jView != null && jView.openLoading()) { 58 | wrapperLoad(); 59 | } 60 | if (mContentView != null) { 61 | super.setContentView(mContentView); 62 | } 63 | //初始化Activity 64 | init(savedInstanceState); 65 | } 66 | 67 | private void wrapperLoad() { 68 | loadHelper = new LoadHelper(); 69 | mContentView = loadHelper.wrapperLoad( 70 | mContentView, 71 | getContentOptions(), 72 | new LoadingPager.OnRefreshListener() { 73 | @Override 74 | public void onRefresh() { 75 | mPresenter.onRefreshData(); 76 | } 77 | } 78 | ); 79 | } 80 | 81 | @Override 82 | public void onNewThrowable(Throwable throwable) { 83 | 84 | } 85 | 86 | @SuppressLint("ResourceType") 87 | @Override 88 | protected void onPostCreate(@Nullable Bundle savedInstanceState) { 89 | super.onPostCreate(savedInstanceState); 90 | onPresentersCreate(); 91 | if (getStatusBarDrawable() > 0) { 92 | getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() { 93 | @Override 94 | public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { 95 | initStatusBar(); 96 | } 97 | }); 98 | } 99 | mPresenter.onRefreshData(); 100 | } 101 | 102 | protected void initStatusBar() { 103 | if (statusBarView == null) { 104 | int identifier = getResources().getIdentifier("statusBarBackground", "id", "android"); 105 | statusBarView = getWindow().findViewById(identifier); 106 | } 107 | int statusBarDrawable = getStatusBarDrawable(); 108 | if (statusBarView != null) { 109 | statusBarView.setBackgroundDrawable(null); 110 | statusBarView.setBackgroundResource(statusBarDrawable); 111 | } 112 | } 113 | 114 | @DrawableRes 115 | @ColorRes 116 | protected int getStatusBarDrawable() { 117 | return MVPManager.getStatusDrawable(); 118 | } 119 | 120 | /** 121 | * 扩展除了默认的presenter的其他Presenter初始化 122 | */ 123 | protected void onPresentersCreate() { 124 | 125 | } 126 | 127 | /** 128 | * 运行在initView之后 129 | * 已经setContentView 130 | * 可以做一些初始化操作 131 | */ 132 | protected void init(@Nullable Bundle savedInstanceState) { 133 | Log.d("lifecycle", "activityInit"); 134 | } 135 | 136 | @Override 137 | @Nullable 138 | public View getContentView() { 139 | return mContentView != null ? mContentView : findViewById(android.R.id.content); 140 | } 141 | 142 | @Override 143 | public void onDetachedFromWindow() { 144 | mPresenter.onDetach(); 145 | super.onDetachedFromWindow(); 146 | } 147 | 148 | @Override 149 | protected void onSaveInstanceState(Bundle outState) { 150 | super.onSaveInstanceState(outState); 151 | mPresenter.onSaveInstanceState(outState); 152 | } 153 | 154 | @Override 155 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 156 | super.onActivityResult(requestCode, resultCode, data); 157 | mPresenter.onActivityResult(requestCode, resultCode, data); 158 | } 159 | 160 | @Override 161 | public BaseActivity getAppcompatActivity() { 162 | return this; 163 | } 164 | 165 | @Override 166 | public Context getContext() { 167 | return this; 168 | } 169 | 170 | private SparseArray getViews() { 171 | if (mViews == null) { 172 | mViews = new SparseArray<>(); 173 | } 174 | return mViews; 175 | } 176 | 177 | /** 178 | * 通过viewId获取控件 179 | * 180 | * @param viewId 资源id 181 | * @return 182 | */ 183 | @Override 184 | public V findView(@IdRes int viewId) { 185 | View view = getViews().get(viewId); 186 | if (view == null) { 187 | view = findViewById(viewId); 188 | getViews().put(viewId, view); 189 | } 190 | return (V) view; 191 | } 192 | 193 | /** 194 | * 初始化ContentView 195 | * 建议不要包含toolbar 196 | * 197 | * @param inflater 198 | * @param savedInstanceState 199 | * @return 200 | */ 201 | protected View initView(@NonNull LayoutInflater inflater, @Nullable Bundle savedInstanceState) { 202 | int layout = initView(savedInstanceState); 203 | try { 204 | return inflater.inflate(layout, null); 205 | } catch (Exception e) { 206 | e.printStackTrace(); 207 | return null; 208 | } 209 | } 210 | 211 | /** 212 | * 建议不要包含toolbar 213 | * 214 | * @param savedInstanceState 215 | * @return 布局layout 216 | */ 217 | @LayoutRes 218 | protected int initView(@Nullable Bundle savedInstanceState) { 219 | if (jView != null) { 220 | return jView.layout(); 221 | } 222 | return 0; 223 | } 224 | 225 | public boolean isFinish() { 226 | return isFinishing(); 227 | } 228 | 229 | @Override 230 | public void finishActivity() { 231 | finish(); 232 | } 233 | 234 | /** 235 | * 子类实现Presenter,且必须继承BasePresenter 236 | * 237 | * @return 238 | */ 239 | @NonNull 240 | protected T initPresenter() { 241 | T t = null; 242 | if (jView != null) { 243 | try { 244 | t = (T) jView.p().newInstance(); 245 | } catch (Exception e) { 246 | e.printStackTrace(); 247 | } 248 | } 249 | if (t == null) { 250 | t = (T) new EmptyPresenter(); 251 | } 252 | return t; 253 | } 254 | 255 | public ContentOptions getContentOptions() { 256 | if (contentOptions == null) { 257 | contentOptions = MVPManager.getContentOptions(); 258 | } 259 | return contentOptions; 260 | } 261 | 262 | @Nullable 263 | public LoadHelper getLoadHelper() { 264 | return loadHelper; 265 | } 266 | } 267 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/base/BaseFragment.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.base; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.os.Bundle; 7 | import android.support.annotation.NonNull; 8 | import android.support.annotation.Nullable; 9 | import android.support.v4.app.Fragment; 10 | import android.support.v4.app.FragmentManager; 11 | import android.support.v7.app.ActionBar; 12 | import android.support.v7.app.AppCompatActivity; 13 | import android.support.v7.widget.Toolbar; 14 | import android.util.SparseArray; 15 | import android.view.LayoutInflater; 16 | import android.view.View; 17 | import android.view.ViewGroup; 18 | import android.view.Window; 19 | 20 | import com.baozi.mvp.MVPManager; 21 | import com.baozi.mvp.annotation.JView; 22 | import com.baozi.mvp.presenter.BasePresenter; 23 | import com.baozi.mvp.presenter.EmptyPresenter; 24 | import com.baozi.mvp.tempalet.helper.load.LoadHelper; 25 | import com.baozi.mvp.tempalet.options.ContentOptions; 26 | import com.baozi.mvp.tempalet.weight.LoadingPager; 27 | import com.baozi.mvp.view.UIView; 28 | 29 | 30 | /** 31 | * @author jlanglang 2016/8/5 9:42 32 | */ 33 | public abstract class BaseFragment extends Fragment 34 | implements UIView { 35 | public final String TAG = this.getClass().getSimpleName(); 36 | protected T mPresenter; 37 | protected Context mContext;//activity的上下文对象 38 | protected Bundle mBundle; 39 | 40 | private boolean isInit; 41 | private boolean first = true; 42 | 43 | private SparseArray mViews; 44 | private View mContentView; 45 | private LoadHelper loadHelper; 46 | private ContentOptions contentOptions; 47 | private JView jView; 48 | 49 | 50 | /** 51 | * 绑定activity 52 | * 53 | * @param context 54 | */ 55 | @Override 56 | public void onAttach(Context context) { 57 | super.onAttach(context); 58 | mContext = context; 59 | jView = this.getClass().getAnnotation(JView.class); 60 | //应该只创建一次Presenter. 61 | if (mPresenter == null || !isInit) { 62 | mPresenter = initPresenter(); 63 | getLifecycle().addObserver(mPresenter); 64 | } 65 | mPresenter.onAttach(this); 66 | } 67 | 68 | @Override 69 | public void onDetach() { 70 | mPresenter.onDetach(); 71 | super.onDetach(); 72 | } 73 | 74 | /** 75 | * 运行在onAttach之后 76 | * 可以接受别人传递过来的参数,实例化对象. 77 | * 78 | * @param savedInstanceState 79 | */ 80 | @Override 81 | public void onCreate(Bundle savedInstanceState) { 82 | super.onCreate(savedInstanceState); 83 | //获取bundle,并保存起来 84 | if (savedInstanceState != null) { 85 | mBundle = savedInstanceState.getBundle("bundle"); 86 | } else { 87 | mBundle = getArguments() == null ? new Bundle() : getArguments(); 88 | } 89 | } 90 | 91 | @Override 92 | public void onSaveInstanceState(@NonNull Bundle outState) { 93 | if (mBundle != null) { 94 | outState.putBundle("bundle", mBundle); 95 | } 96 | super.onSaveInstanceState(outState); 97 | } 98 | 99 | /** 100 | * 运行在onCreate之后 101 | * 生成view视图 102 | * 103 | * @param inflater 104 | * @param container 105 | * @param savedInstanceState 106 | * @return 107 | */ 108 | @Nullable 109 | @Override 110 | public final View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 111 | if (null == mContentView) { 112 | mContentView = initView(inflater, savedInstanceState); 113 | // 包装加载 114 | if (jView != null && jView.openLoading()) { 115 | wrapperLoad(); 116 | } 117 | } else { 118 | //缓存的ContentView需要判断是否已有parent, 如果有parent需要从parent删除,否则会抛出异常。 119 | ViewGroup parent = (ViewGroup) mContentView.getParent(); 120 | if (parent != null) { 121 | parent.removeView(mContentView); 122 | } 123 | } 124 | return mContentView; 125 | } 126 | 127 | 128 | /** 129 | * 运行在onCreateView之后 130 | * 加载数据,初始化Presenter 131 | * 132 | * @param savedInstanceState 133 | */ 134 | @Override 135 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 136 | super.onActivityCreated(savedInstanceState); 137 | //View做一些初始化操作. 138 | init(savedInstanceState); 139 | if (mPresenter == null) { 140 | return; 141 | } 142 | //初始化,应该只初始化一次 143 | if (!isInit) { 144 | isInit = true; 145 | onPresentersCreate(); 146 | if (!isLazy()) { // 没开懒加载,刷新 147 | mPresenter.onRefreshData(); 148 | } 149 | } 150 | } 151 | 152 | /** 153 | * 扩展除了默认的presenter的其他Presenter初始化 154 | */ 155 | protected void onPresentersCreate() { 156 | 157 | } 158 | 159 | @Override 160 | public void onNewThrowable(Throwable throwable) { 161 | 162 | } 163 | 164 | 165 | @Override 166 | public void onHiddenChanged(boolean hidden) { 167 | super.onHiddenChanged(hidden); 168 | if (null != mPresenter) { 169 | if (!hidden) { 170 | //相当于Fragment的onResume 171 | mPresenter.onResume(); 172 | } else { 173 | //相当于Fragment的onPause 174 | mPresenter.onPause(); 175 | } 176 | } 177 | } 178 | 179 | @Override 180 | public void setUserVisibleHint(boolean isVisibleToUser) { 181 | super.setUserVisibleHint(isVisibleToUser); 182 | if (!isInit()) {//视图未加载 183 | return; 184 | } 185 | if (null != mPresenter) { 186 | if (isVisibleToUser) { 187 | if (isLazy() && isFirst()) {//懒加载 188 | first = false; 189 | mPresenter.onRefreshData(); 190 | } 191 | } 192 | } 193 | } 194 | 195 | 196 | @Override 197 | public void onActivityResult(int requestCode, int resultCode, Intent data) { 198 | mPresenter.onActivityResult(requestCode, resultCode, data); 199 | } 200 | 201 | public View getContentView() { 202 | return mContentView; 203 | } 204 | 205 | 206 | /** 207 | * 类似Activity的OnBackgress 208 | * fragment进行回退 209 | */ 210 | @Override 211 | public void onBackPressed() { 212 | FragmentManager fragmentManager = getFragmentManager(); 213 | if (fragmentManager != null) { 214 | getFragmentManager().popBackStackImmediate(); 215 | } 216 | } 217 | 218 | /** 219 | * 创建Fragment视图 220 | * 221 | * @return Fragment视图 222 | */ 223 | protected View initView(LayoutInflater inflater, @Nullable Bundle savedInstanceState) { 224 | int layout = initView(savedInstanceState); 225 | return inflater.inflate(layout, null); 226 | } 227 | 228 | 229 | /** 230 | * 运行在initView之后 231 | * 此时已经setContentView 232 | * 可以做一些初始化操作 233 | */ 234 | public void init(Bundle savedInstanceState) { 235 | 236 | } 237 | 238 | @Override 239 | public Context getContext() { 240 | return mContext; 241 | } 242 | 243 | public Bundle getBundle() { 244 | return mBundle; 245 | } 246 | 247 | public BaseFragment getFragment() { 248 | return this; 249 | } 250 | 251 | @Override 252 | public Window getWindow() { 253 | return getActivity() == null ? ((Activity) mContext).getWindow() : getActivity().getWindow(); 254 | } 255 | 256 | @Override 257 | public AppCompatActivity getAppcompatActivity() { 258 | return (AppCompatActivity) mContext; 259 | } 260 | 261 | @Override 262 | public ActionBar getSupportActionBar() { 263 | return getAppcompatActivity().getSupportActionBar(); 264 | } 265 | 266 | @Override 267 | public void setSupportActionBar(@Nullable Toolbar toolbar) { 268 | getAppcompatActivity().setSupportActionBar(toolbar); 269 | } 270 | 271 | public SparseArray getViews() { 272 | if (mViews == null) { 273 | mViews = new SparseArray<>(); 274 | } 275 | return mViews; 276 | } 277 | 278 | /** 279 | * 通过viewId获取控件 280 | * 281 | * @param viewId 资源id 282 | * @return 283 | */ 284 | public V findView(int viewId) { 285 | View view = getViews().get(viewId); 286 | if (view == null) { 287 | view = mContentView.findViewById(viewId); 288 | getViews().put(viewId, view); 289 | } 290 | return (V) view; 291 | } 292 | 293 | /** 294 | * 是否关闭 295 | */ 296 | public boolean isFinish() { 297 | return !isAdded(); 298 | } 299 | 300 | @Override 301 | public void finishActivity() { 302 | if (getActivity() == null) { 303 | ((Activity) mContext).finish(); 304 | } else { 305 | getActivity().finish(); 306 | } 307 | } 308 | 309 | /** 310 | * 创建Fragment视图 311 | * 312 | * @return Fragment视图 313 | */ 314 | protected int initView(@Nullable Bundle savedInstanceState) { 315 | if (jView != null) { 316 | return jView.layout(); 317 | } 318 | return 0; 319 | } 320 | /** 321 | * 初始化Presenter 322 | * 323 | * @return 324 | */ 325 | /** 326 | * 子类实现Presenter,且必须继承BasePresenter 327 | * 328 | * @return 329 | */ 330 | @NonNull 331 | protected T initPresenter() { 332 | T t = null; 333 | if (jView != null) { 334 | try { 335 | t = (T) jView.p().newInstance(); 336 | } catch (Exception e) { 337 | e.printStackTrace(); 338 | } 339 | } 340 | if (t == null) { 341 | t = (T) new EmptyPresenter(); 342 | } 343 | return t; 344 | } 345 | 346 | private void wrapperLoad() { 347 | loadHelper = new LoadHelper(); 348 | mContentView = loadHelper.wrapperLoad( 349 | mContentView, 350 | getContentOptions(), 351 | new LoadingPager.OnRefreshListener() { 352 | @Override 353 | public void onRefresh() { 354 | mPresenter.onRefreshData(); 355 | } 356 | } 357 | ); 358 | } 359 | 360 | /** 361 | * 视图是否加载 362 | * 363 | * @return 364 | */ 365 | public boolean isInit() { 366 | return isInit; 367 | } 368 | 369 | /** 370 | * 是否第一次加载 371 | * 372 | * @return 373 | */ 374 | public boolean isFirst() { 375 | return first; 376 | } 377 | 378 | /** 379 | * 是否懒加载 380 | * 381 | * @return 382 | */ 383 | public boolean isLazy() { 384 | return false; 385 | } 386 | 387 | public ContentOptions getContentOptions() { 388 | if (contentOptions == null) { 389 | contentOptions = MVPManager.getContentOptions(); 390 | } 391 | return contentOptions; 392 | } 393 | 394 | public LoadHelper getLoadHelper() { 395 | return loadHelper; 396 | } 397 | 398 | } 399 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/model/BaseModel.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.model; 2 | 3 | import com.baozi.mvp.presenter.BasePresenter; 4 | 5 | /** 6 | * Created by baozi on 2017/10/23. 7 | */ 8 | 9 | public class BaseModel { 10 | protected T mPresenter; 11 | 12 | public BaseModel(T presenter) { 13 | mPresenter = presenter; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/model/LoadDataModel.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.model; 2 | 3 | /** 4 | * Created by baozi on 2017/12/5. 5 | */ 6 | 7 | public interface LoadDataModel { 8 | T loadData(int pageNum, int pageSize, boolean isRefresh); 9 | } 10 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/presenter/BasePresenter.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.presenter; 2 | 3 | import android.arch.lifecycle.Lifecycle; 4 | import android.arch.lifecycle.LifecycleObserver; 5 | import android.arch.lifecycle.OnLifecycleEvent; 6 | import android.content.Intent; 7 | import android.os.Bundle; 8 | import android.util.Log; 9 | 10 | import com.baozi.mvp.view.UIView; 11 | 12 | 13 | /** 14 | * @author jlanglang 2016/11/11 15:10 15 | * @版本 2.0 16 | * @Change 17 | */ 18 | public abstract class BasePresenter implements LifecycleObserver { 19 | 20 | 21 | protected T mView; 22 | 23 | /** 24 | * 绑定View 25 | */ 26 | public void onAttach(T view) { 27 | this.mView = view; 28 | } 29 | 30 | /** 31 | * 解除绑定 32 | */ 33 | public void onDetach() { 34 | 35 | } 36 | 37 | public T getView() { 38 | return mView; 39 | } 40 | 41 | /** 42 | * 运行在onCreate()之后,可能在onStart()之后调用. 43 | * 建议初始化数据,刷新的网络请求 44 | */ 45 | public void onRefreshData() { 46 | Log.d("lifecycle", "onRefreshData"); 47 | } 48 | 49 | /** 50 | * 在这里取消网络请求回调 51 | */ 52 | public abstract void cancelNetWork(); 53 | 54 | /** 55 | * 本地网络异常 56 | */ 57 | public abstract void netWorkError(Throwable throwable); 58 | 59 | 60 | public void onActivityResult(int requestCode, int resultCode, Intent data) { 61 | 62 | } 63 | 64 | 65 | public void onSaveInstanceState(Bundle outState) { 66 | 67 | } 68 | 69 | /** 70 | * 做初始化的操作,需要在view的视图初始化完成之后才能调用 71 | * 建议只初始化一些对象,而不要去做耗时操作. 72 | */ 73 | @OnLifecycleEvent(Lifecycle.Event.ON_CREATE) 74 | public void onCreate() { 75 | Log.d("lifecycle", "onCreate"); 76 | } 77 | 78 | @OnLifecycleEvent(value = Lifecycle.Event.ON_START) 79 | public void onStart() { 80 | Log.d("lifecycle", "onStart"); 81 | } 82 | 83 | @OnLifecycleEvent(value = Lifecycle.Event.ON_RESUME) 84 | public void onResume() { 85 | Log.d("lifecycle", "onResume"); 86 | } 87 | 88 | @OnLifecycleEvent(value = Lifecycle.Event.ON_PAUSE) 89 | public void onPause() { 90 | Log.d("lifecycle", "onPause"); 91 | } 92 | 93 | @OnLifecycleEvent(value = Lifecycle.Event.ON_STOP) 94 | public void onStop() { 95 | Log.d("lifecycle", "onStop"); 96 | } 97 | 98 | @OnLifecycleEvent(value = Lifecycle.Event.ON_DESTROY) 99 | public void onDestroy() { 100 | cancelNetWork(); 101 | Log.d("lifecycle", "onDestroy"); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/presenter/EmptyPresenter.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.presenter; 2 | 3 | /** 4 | * Created by baozi on 2017/12/20. 5 | */ 6 | 7 | public final class EmptyPresenter extends BasePresenter { 8 | 9 | @Override 10 | public void cancelNetWork() { 11 | } 12 | 13 | @Override 14 | public void netWorkError(Throwable throwable) { 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/presenter/LoadMorePresenter.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.presenter; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.support.v4.widget.SwipeRefreshLayout; 5 | import android.support.v7.widget.LinearLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | 8 | import com.baozi.mvp.model.LoadDataModel; 9 | import com.baozi.mvp.view.LoadMoreView; 10 | 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | /** 15 | * Created by baozi on 2017/11/20. 16 | */ 17 | public abstract class LoadMorePresenter { 18 | private LoadDataModel loadDataModel; 19 | protected LoadMoreView loadMoreView; 20 | 21 | protected int pageNum = 1; 22 | protected int pageSize = 10; 23 | protected int lastVisibleIndex = 5; 24 | protected int minPages = 10; 25 | 26 | 27 | private boolean isCanLoadMore = true; 28 | private boolean isLoading; 29 | private boolean isRefresh; 30 | private SwipeRefreshLayout refreshLayout; 31 | 32 | private List> completeListeners = new ArrayList<>(); 33 | 34 | public LoadMorePresenter(LoadMoreView loadMoreView, LoadDataModel loadDataModel) { 35 | this.loadDataModel = loadDataModel; 36 | this.loadMoreView = loadMoreView; 37 | } 38 | 39 | public void onCreate() { 40 | initRefresh(); 41 | initRecyclerView(); 42 | } 43 | 44 | protected void initRecyclerView() { 45 | RecyclerView recyclerView = loadMoreView.getRecyclerView(); 46 | recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { 47 | @Override 48 | public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 49 | super.onScrollStateChanged(recyclerView, newState); 50 | } 51 | 52 | @Override 53 | public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 54 | LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); 55 | int itemCount = layoutManager.getItemCount(); 56 | int lastPosition = layoutManager.findLastVisibleItemPosition(); 57 | //如果当前不是正在加载更多,并且到了该加载更多的位置,加载更多。 58 | if (lastPosition >= (itemCount - lastVisibleIndex) && itemCount >= (minPages - lastVisibleIndex)) { 59 | loadMore(); 60 | } 61 | } 62 | }); 63 | } 64 | 65 | 66 | private void initRefresh() { 67 | refreshLayout = loadMoreView.getSwipeRefreshLayout(); 68 | if (refreshLayout != null) { 69 | refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 70 | @Override 71 | public void onRefresh() { 72 | refresh(); 73 | } 74 | }); 75 | } 76 | } 77 | 78 | public void loadMore() { 79 | if (isLoading) { 80 | return; 81 | } 82 | isLoading = true; 83 | loadMoreCallBack(loadDataModel.loadData(pageNum + 1, pageSize, false)); 84 | } 85 | 86 | public void refresh() { 87 | if (isRefresh) { 88 | return; 89 | } 90 | isLoading = false; 91 | isCanLoadMore = true; 92 | setRefresh(true); 93 | for (LoadCompleteListener listener : completeListeners) { 94 | listener.onRefreshStart(); 95 | } 96 | refreshCallBack(loadDataModel.loadData(1, pageSize, true)); 97 | } 98 | 99 | 100 | protected abstract void loadMoreCallBack(M m); 101 | 102 | protected abstract void refreshCallBack(M m); 103 | 104 | /** 105 | * 请下拉刷新完成时调用 106 | * 107 | * @param list 108 | */ 109 | public void refreshComplete(List list) { 110 | pageNum = 1; 111 | setRefresh(false); 112 | boolean intercept = false; 113 | for (LoadCompleteListener listener : completeListeners) { 114 | intercept = listener.doOnNext(true, intercept, list); 115 | } 116 | if (intercept) { 117 | return; 118 | } 119 | for (LoadCompleteListener listener : completeListeners) { 120 | listener.onNext(true, list); 121 | } 122 | } 123 | 124 | /** 125 | * 请加载更多完成时调用 126 | * 127 | * @param list 128 | */ 129 | public void loadMoreComplete(List list) { 130 | isLoading = false; 131 | if (checkCanLoadMore(list)) { 132 | pageNum++; 133 | } else { 134 | isCanLoadMore = false; 135 | } 136 | boolean intercept = false; 137 | for (LoadCompleteListener listener : completeListeners) { 138 | intercept = listener.doOnNext(false, intercept, list); 139 | } 140 | if (intercept) { 141 | return; 142 | } 143 | for (LoadCompleteListener listener : completeListeners) { 144 | listener.onNext(false, list); 145 | } 146 | } 147 | 148 | 149 | /** 150 | * 请刷新失败时调用 151 | */ 152 | public void refreshError(Throwable throwable) { 153 | setRefresh(false); 154 | refreshComplete(null); 155 | for (LoadCompleteListener listener : completeListeners) { 156 | listener.onError(true, throwable); 157 | } 158 | } 159 | 160 | /** 161 | * 请加载失败时调用 162 | */ 163 | public void loadMoreError(Throwable throwable) { 164 | loadMoreComplete(null); 165 | for (LoadCompleteListener listener : completeListeners) { 166 | listener.onError(false, throwable); 167 | } 168 | } 169 | 170 | 171 | /** 172 | * loadMore校验规则 173 | * 174 | * @return 可以加载更多返回true, 不能加载更多返回false 175 | */ 176 | protected boolean checkCanLoadMore(List list) { 177 | return list != null && !list.isEmpty() && list.size() >= pageSize; 178 | } 179 | 180 | 181 | public void addLoadCompleteListener(LoadCompleteListener loadCompleteListener) { 182 | completeListeners.add(loadCompleteListener); 183 | } 184 | 185 | public void removeLoadCompleteListener(LoadCompleteListener loadCompleteListener) { 186 | completeListeners.remove(loadCompleteListener); 187 | } 188 | 189 | /** 190 | * 加载更多,刷新回调. 191 | * 必须配合loadMoreComplete(),refreshComplete(); 192 | */ 193 | public static abstract class LoadCompleteListener { 194 | public void onRefreshStart() { 195 | } 196 | 197 | public void onNext(boolean isRefresh, @NonNull List list) { 198 | } 199 | 200 | /** 201 | * @param isRefresh 是否刷新 202 | * @param intercept 上一层监听的拦截结果 203 | * @param list 返回结果 204 | * @return 205 | */ 206 | public boolean doOnNext(boolean isRefresh, boolean intercept, @NonNull List list) { 207 | return intercept; 208 | } 209 | 210 | public void onError(boolean isRefresh, Throwable throwable) { 211 | 212 | } 213 | } 214 | 215 | //倒数第几条开始加载更多 216 | public int getLastVisibleIndex() { 217 | return 0; 218 | } 219 | 220 | public void setLastVisibleIndex(int lastVisibleIndex) { 221 | this.lastVisibleIndex = lastVisibleIndex; 222 | } 223 | 224 | public int getPageNum() { 225 | return pageNum; 226 | } 227 | 228 | public void setPageNum(int pageNum) { 229 | this.pageNum = pageNum; 230 | } 231 | 232 | public int getPageSize() { 233 | return pageSize; 234 | } 235 | 236 | public void setPageSize(int pageSize) { 237 | this.pageSize = pageSize; 238 | } 239 | 240 | public int getMinPageSize() { 241 | return minPages; 242 | } 243 | 244 | public void setMinPages(int minPages) { 245 | this.minPages = minPages; 246 | } 247 | 248 | public boolean isCanLoadMore() { 249 | return isCanLoadMore; 250 | } 251 | 252 | protected void setCanLoadMore(boolean canLoadMore) { 253 | isCanLoadMore = canLoadMore; 254 | } 255 | 256 | public boolean isLoading() { 257 | return isLoading; 258 | } 259 | 260 | public void setRefresh(boolean refresh) { 261 | isRefresh = refresh; 262 | if (refreshLayout != null) { 263 | refreshLayout.setRefreshing(refresh); 264 | } 265 | } 266 | 267 | public boolean isRefrsh() { 268 | return isRefresh; 269 | } 270 | 271 | public void setLoading(boolean loading) { 272 | isLoading = loading; 273 | } 274 | 275 | } 276 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/presenter/PagerFragmentPresenter.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.presenter; 2 | 3 | import android.support.v4.app.Fragment; 4 | import android.support.v4.app.FragmentStatePagerAdapter; 5 | 6 | import com.baozi.mvp.view.PageFragmentView; 7 | import com.baozi.mvp.view.TabLayoutView; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | /** 13 | * Created by Administrator on 2017/8/21 0021. 14 | */ 15 | 16 | public class PagerFragmentPresenter extends PagerPresenter { 17 | private List fragments; 18 | 19 | public PagerFragmentPresenter(PageFragmentView pageView) { 20 | super(pageView); 21 | } 22 | 23 | public PagerFragmentPresenter(PageFragmentView pageView, TabLayoutView tablayoutView) { 24 | super(pageView, tablayoutView); 25 | } 26 | 27 | public List getFragments() { 28 | if (fragments == null) { 29 | fragments = new ArrayList<>(); 30 | if (pageView.getPage() != null) { 31 | fragments.addAll(pageView.getPage()); 32 | } 33 | } 34 | return fragments; 35 | } 36 | 37 | public FragmentStatePagerAdapter getAdapter() { 38 | return new FragmentStatePagerAdapter(((PageFragmentView) pageView).getFgManager()) { 39 | @Override 40 | public Fragment getItem(int position) { 41 | return getFragments().get(position); 42 | } 43 | 44 | @Override 45 | public int getCount() { 46 | return getFragments().size(); 47 | } 48 | }; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/presenter/PagerPresenter.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.presenter; 2 | 3 | import android.content.res.ColorStateList; 4 | import android.support.annotation.NonNull; 5 | import android.support.design.widget.TabLayout; 6 | import android.support.v4.view.PagerAdapter; 7 | import android.support.v4.view.ViewPager; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.ImageView; 12 | import android.widget.TextView; 13 | 14 | import com.baozi.mvp.view.PageView; 15 | import com.baozi.mvp.view.TabLayoutView; 16 | 17 | 18 | /** 19 | * Created by Administrator on 2017/8/8 0008. 20 | */ 21 | 22 | public class PagerPresenter { 23 | protected PageView pageView; 24 | protected TabLayoutView tablayoutView; 25 | protected PagerAdapter mAdapter; 26 | 27 | public PagerPresenter(PageView pageView) { 28 | this(pageView, null); 29 | } 30 | 31 | public PagerPresenter(PageView pageView, TabLayoutView tablayoutView) { 32 | this.pageView = pageView; 33 | this.tablayoutView = tablayoutView; 34 | } 35 | 36 | public void onCreate() { 37 | initViewPager(); 38 | initTabLayout(); 39 | } 40 | 41 | private void bindTab(TabLayout.Tab tab, int image, String tabStr) { 42 | int tabLayoutItem = tablayoutView.getTabLayoutItem(); 43 | if (tabLayoutItem == 0 && tab != null) { 44 | tab.setText(tabStr); 45 | if (image == 0) { 46 | return; 47 | } 48 | tab.setIcon(image); 49 | return; 50 | } 51 | if (tabLayoutItem != 0) { 52 | ViewGroup inflate = (ViewGroup) LayoutInflater.from(pageView.getContext()).inflate(tabLayoutItem, null); 53 | int childCount = inflate.getChildCount(); 54 | for (int j = 0; j < childCount; j++) { 55 | View childAt = inflate.getChildAt(j); 56 | if (childAt instanceof ImageView) { 57 | if (image != 0) { 58 | childAt.setVisibility(View.VISIBLE); 59 | ((ImageView) childAt).setImageResource(image); 60 | } else { 61 | childAt.setVisibility(View.GONE); 62 | } 63 | } 64 | if (childAt instanceof TextView) { 65 | childAt.setVisibility(View.VISIBLE); 66 | ColorStateList tabTextColors = tablayoutView.getTabLayout().getTabTextColors(); 67 | ((TextView) childAt).setText(tabStr); 68 | ((TextView) childAt).setTextColor(tabTextColors); 69 | } 70 | } 71 | tab.setCustomView(inflate); 72 | } 73 | } 74 | 75 | private void initTabLayout() { 76 | if (tablayoutView == null) { 77 | return; 78 | } 79 | TabLayout tablayout = tablayoutView.getTabLayout(); 80 | tablayout.setupWithViewPager(pageView.getViewPage()); 81 | //条目数 82 | int size = pageView.getPage().size(); 83 | String[] tabString = tablayoutView.getTabString(); 84 | int[] tabImage = tablayoutView.getTabDrawables(); 85 | for (int i = 0; i < size; i++) { 86 | TabLayout.Tab tab = tablayout.getTabAt(i); 87 | int image = 0; 88 | if (tabImage != null) { 89 | image = tabImage[i]; 90 | } 91 | String tabStr = ""; 92 | if (tabString != null) { 93 | tabStr = tabString[i]; 94 | } 95 | bindTab(tab, image, tabStr); 96 | } 97 | pageView.getViewPage().setOffscreenPageLimit(pageView.getPage().size()); 98 | if (!pageView.isAnimation()) { 99 | tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 100 | @Override 101 | public void onTabSelected(TabLayout.Tab tab) { 102 | int position = tab.getPosition(); 103 | pageView.getViewPage().setCurrentItem(position, false); 104 | } 105 | 106 | @Override 107 | public void onTabUnselected(TabLayout.Tab tab) { 108 | 109 | } 110 | 111 | @Override 112 | public void onTabReselected(TabLayout.Tab tab) { 113 | 114 | } 115 | }); 116 | } 117 | } 118 | 119 | private void initViewPager() { 120 | if (pageView == null) { 121 | return; 122 | } 123 | ViewPager viewPager = pageView.getViewPage(); 124 | viewPager.setAdapter(getAdapter()); 125 | } 126 | 127 | protected PagerAdapter getAdapter() { 128 | if (mAdapter == null) { 129 | mAdapter = new PagerAdapter() { 130 | // 当要显示的图片可以进行缓存的时候,会调用这个方法进行显示图片的初始化,我们将要显示的ImageView加入到ViewGroup中,然后作为返回值返回即可 131 | @NonNull 132 | @Override 133 | public Object instantiateItem(@NonNull ViewGroup view, int position) { 134 | T page = pageView.getPage().get(position); 135 | if (page instanceof View) { 136 | view.addView((View) page); 137 | } 138 | return page; 139 | } 140 | 141 | @Override 142 | public int getItemPosition(@NonNull Object object) { 143 | return POSITION_NONE; 144 | } 145 | 146 | @Override 147 | public int getCount() { 148 | return pageView.getPage().size(); 149 | } 150 | 151 | // 例如PagerAdapter只缓存三张要显示的图片,如果滑动的图片超出了缓存的范围,就会调用这个方法,将图片销毁 152 | @Override 153 | public void destroyItem(@NonNull ViewGroup view, int position, @NonNull Object object) { 154 | T v = pageView.getPage().get(position); 155 | if (v instanceof View) { 156 | view.removeView((View) v); 157 | } 158 | } 159 | 160 | @Override 161 | public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { 162 | return view == object; 163 | } 164 | }; 165 | } 166 | return mAdapter; 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/tempalet/TemplateActivity.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.tempalet; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.NonNull; 5 | import android.support.annotation.Nullable; 6 | import android.support.design.widget.AppBarLayout; 7 | import android.support.design.widget.CoordinatorLayout; 8 | import android.support.v7.app.ActionBar; 9 | import android.support.v7.view.menu.MenuBuilder; 10 | import android.support.v7.widget.Toolbar; 11 | import android.view.LayoutInflater; 12 | import android.view.Menu; 13 | import android.view.MenuItem; 14 | import android.view.View; 15 | import android.view.ViewGroup; 16 | 17 | import com.baozi.mvp.MVPManager; 18 | import com.baozi.mvp.R; 19 | import com.baozi.mvp.base.BaseActivity; 20 | import com.baozi.mvp.presenter.BasePresenter; 21 | import com.baozi.mvp.tempalet.helper.toolbar.ToolbarHelper; 22 | import com.baozi.mvp.tempalet.options.ToolbarOptions; 23 | import com.baozi.mvp.view.BaseView; 24 | import com.baozi.mvp.view.ToolbarView; 25 | 26 | /** 27 | * 模版Activity 28 | * 29 | * @param 30 | */ 31 | public abstract class TemplateActivity extends BaseActivity 32 | implements ToolbarView { 33 | private ToolbarHelper mToolbarHelper; 34 | private ViewGroup mRootView; 35 | private ToolbarOptions toolbarOptions; 36 | 37 | @Override 38 | public View initView(@NonNull LayoutInflater inflater, Bundle savedInstanceState) { 39 | ActionBar supportActionBar = getSupportActionBar(); 40 | if (supportActionBar != null) { 41 | throw new IllegalStateException("请使用NoActionbar的Theme,否则使用该模板无意义"); 42 | } 43 | mRootView = (ViewGroup) inflater.inflate(R.layout.template_layout, null); 44 | //初始化一次 45 | mToolbarHelper = getToolbarHelper(); 46 | View baseView = super.initView(inflater, savedInstanceState); 47 | View wrapperView = wrapperContentView(baseView); 48 | mRootView.addView(wrapperView, 1); 49 | ViewGroup.LayoutParams layoutParams = wrapperView.getLayoutParams(); 50 | if (layoutParams instanceof CoordinatorLayout.LayoutParams) { 51 | layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; 52 | layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT; 53 | ((CoordinatorLayout.LayoutParams) layoutParams).setBehavior(new AppBarLayout.ScrollingViewBehavior()); 54 | wrapperView.requestLayout(); 55 | } 56 | return mRootView; 57 | } 58 | 59 | protected View wrapperContentView(View view) { 60 | return view; 61 | } 62 | 63 | /** 64 | * 如果调用在initView()之前,可能为null 65 | * 66 | * @return 67 | */ 68 | @Override 69 | public View getContentView() { 70 | return mRootView; 71 | } 72 | 73 | /** 74 | * 默认使用base_toolbar 75 | * 如果不需要toolbar,请复写,并返回0.或者-1 76 | * 77 | * @return 78 | */ 79 | @Override 80 | public int getToolbarLayout() { 81 | return getToolbarOptions().getToolbarLayout(); 82 | } 83 | 84 | @Override 85 | protected int getStatusBarDrawable() { 86 | return getToolbarOptions().getStatusDrawable(); 87 | } 88 | 89 | @Override 90 | public ToolbarOptions getToolbarOptions() { 91 | if (toolbarOptions == null) { 92 | toolbarOptions = MVPManager.getToolbarOptions(); 93 | } 94 | return toolbarOptions; 95 | } 96 | 97 | /** 98 | * 此方法用于初始化菜单,其中menu参数就是即将要显示的Menu实例。 返回true则显示该menu,false 则不显示; 99 | * (只会在第一次初始化菜单时调用) 100 | */ 101 | @Override 102 | public boolean onCreateOptionsMenu(Menu menu) { 103 | return super.onCreateOptionsMenu(menu); 104 | } 105 | 106 | /** 107 | * 在onCreateOptionsMenu执行后,菜单被显示前调用;如果菜单已经被创建,则在菜单显示前被调用。 同样的, 108 | * 返回true则显示该menu,false 则不显示; (可以通过此方法动态的改变菜单的状态,比如加载不同的菜单等) 109 | */ 110 | @Override 111 | public boolean onPrepareOptionsMenu(Menu menu) { 112 | return super.onPrepareOptionsMenu(menu); 113 | } 114 | 115 | /** 116 | * 显示menu的icon 117 | * 118 | * @param view 119 | * @param menu 120 | * @return 121 | */ 122 | @Override 123 | protected boolean onPrepareOptionsPanel(View view, Menu menu) { 124 | if (menu != null) { 125 | if (menu.getClass().getSimpleName().equals("MenuBuilder")) { 126 | try { 127 | MenuBuilder menuBuilder = (MenuBuilder) menu; 128 | menuBuilder.setOptionalIconsVisible(true); 129 | } catch (Exception e) { 130 | e.printStackTrace(); 131 | } 132 | } 133 | } 134 | return super.onPrepareOptionsPanel(view, menu); 135 | } 136 | 137 | /** 138 | * 每次菜单被关闭时调用.(菜单被关闭有三种情形,menu按钮被再次点击、back按钮被点击或者用户选择了某一个菜单项) 139 | */ 140 | @Override 141 | public void onOptionsMenuClosed(Menu menu) { 142 | super.onOptionsMenuClosed(menu); 143 | } 144 | 145 | /** 146 | * 菜单项被点击时调用,也就是菜单项的监听方法。 147 | * 通过这几个方法,可以得知,对于Activity,同一时间只能显示和监听一个Menu 对象. 148 | */ 149 | @Override 150 | public boolean onOptionsItemSelected(MenuItem item) { 151 | return super.onOptionsItemSelected(item); 152 | } 153 | 154 | 155 | @Override 156 | public TemplateActivity getAppcompatActivity() { 157 | return this; 158 | } 159 | 160 | /** 161 | * 如果设置的主题不是NoActionBar,或者initToolbar()是0,则返回null. 162 | * 163 | * @return mToolbar 可能为null. 164 | */ 165 | @Nullable 166 | public Toolbar getToolbar() { 167 | return getToolbarHelper().getToolbar(); 168 | } 169 | 170 | /** 171 | * 请在ui线程中调用 172 | * 173 | * @return 174 | */ 175 | @Override 176 | public ToolbarHelper getToolbarHelper() { 177 | if (mToolbarHelper == null) { 178 | mToolbarHelper = ToolbarHelper.Create(this); 179 | } 180 | return mToolbarHelper; 181 | } 182 | 183 | } 184 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/tempalet/TemplateFragment.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.tempalet; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.NonNull; 5 | import android.support.design.widget.AppBarLayout; 6 | import android.support.design.widget.CoordinatorLayout; 7 | import android.view.LayoutInflater; 8 | import android.view.Menu; 9 | import android.view.MenuInflater; 10 | import android.view.MenuItem; 11 | import android.view.View; 12 | import android.view.ViewGroup; 13 | 14 | import com.baozi.mvp.MVPManager; 15 | import com.baozi.mvp.R; 16 | import com.baozi.mvp.base.BaseFragment; 17 | import com.baozi.mvp.presenter.BasePresenter; 18 | import com.baozi.mvp.tempalet.helper.toolbar.ToolbarHelper; 19 | import com.baozi.mvp.tempalet.options.ToolbarOptions; 20 | import com.baozi.mvp.view.ToolbarView; 21 | 22 | /** 23 | * 模版Fragment 24 | * 25 | * @param 26 | */ 27 | public abstract class TemplateFragment extends BaseFragment 28 | implements ToolbarView { 29 | private ToolbarHelper mToolbarHelper; 30 | private ViewGroup rootView; 31 | private ToolbarOptions toolbarOptions; 32 | 33 | /** 34 | * @param inflater 35 | * @param savedInstanceState 36 | * @return 37 | */ 38 | @NonNull 39 | @Override 40 | public View initView(@NonNull LayoutInflater inflater, Bundle savedInstanceState) { 41 | rootView = (ViewGroup) inflater.inflate(R.layout.template_layout, null); 42 | //初始化一次 43 | mToolbarHelper = getToolbarHelper(); 44 | View view = wrapperContentView(super.initView(inflater, savedInstanceState)); 45 | rootView.addView(view, 1); 46 | ViewGroup.LayoutParams layoutParams = view.getLayoutParams(); 47 | if (layoutParams instanceof CoordinatorLayout.LayoutParams) { 48 | layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; 49 | layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT; 50 | ((CoordinatorLayout.LayoutParams) layoutParams).setBehavior(new AppBarLayout.ScrollingViewBehavior()); 51 | view.requestLayout(); 52 | } 53 | return rootView; 54 | } 55 | 56 | @Override 57 | public View getContentView() { 58 | return rootView; 59 | } 60 | 61 | /** 62 | * 默认使用base_toolbar 63 | * 如果不需要toolbar,请复写,并返回0.或者-1 64 | * 65 | * @return 66 | */ 67 | @Override 68 | public int getToolbarLayout() { 69 | return getToolbarOptions().getToolbarLayout(); 70 | } 71 | 72 | @Override 73 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 74 | super.onCreateOptionsMenu(menu, inflater); 75 | } 76 | 77 | @Override 78 | public ToolbarOptions getToolbarOptions() { 79 | if (toolbarOptions == null) { 80 | toolbarOptions = MVPManager.getToolbarOptions(); 81 | } 82 | return toolbarOptions; 83 | } 84 | 85 | /** 86 | * 每次菜单被关闭时调用.(菜单被关闭有三种情形,menu按钮被再次点击、back按钮被点击或者用户选择了某一个菜单项) 87 | */ 88 | @Override 89 | public void onOptionsMenuClosed(Menu menu) { 90 | super.onOptionsMenuClosed(menu); 91 | } 92 | 93 | /** 94 | * 菜单项被点击时调用,也就是菜单项的监听方法。 95 | * 通过这几个方法,可以得知,对于Activity,同一时间只能显示和监听一个Menu 对象. 96 | */ 97 | @Override 98 | public boolean onOptionsItemSelected(MenuItem item) { 99 | return super.onOptionsItemSelected(item); 100 | } 101 | 102 | /** 103 | * @return 104 | */ 105 | @Override 106 | public ToolbarHelper getToolbarHelper() { 107 | if (mToolbarHelper == null) { 108 | mToolbarHelper = ToolbarHelper.Create(this); 109 | } 110 | return mToolbarHelper; 111 | } 112 | 113 | protected View wrapperContentView(View view) { 114 | return view; 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/tempalet/helper/load/LoadHelper.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.tempalet.helper.load; 2 | 3 | import android.view.View; 4 | 5 | import com.baozi.mvp.MVPManager; 6 | import com.baozi.mvp.tempalet.options.ContentOptions; 7 | import com.baozi.mvp.tempalet.weight.LoadingPager; 8 | 9 | public class LoadHelper { 10 | private LoadingPager mLoadingPager; 11 | 12 | public LoadHelper() { 13 | } 14 | 15 | public View wrapperLoad(View view, LoadingPager.OnRefreshListener onRefreshListener) { 16 | return wrapperLoad(view, MVPManager.getContentOptions(), onRefreshListener); 17 | } 18 | 19 | public View wrapperLoad(View view, ContentOptions options, LoadingPager.OnRefreshListener onRefreshListener) { 20 | if (options == null) return view; 21 | mLoadingPager = options.buildLoadingPager(view.getContext(), view); 22 | mLoadingPager.setRefreshListener(onRefreshListener); 23 | return mLoadingPager; 24 | } 25 | 26 | public LoadingPager getLoadPager() { 27 | return mLoadingPager; 28 | } 29 | 30 | public void showEmpty() { 31 | mLoadingPager.showEmpty(); 32 | } 33 | 34 | public void showError(Throwable throwable, boolean isShowError) { 35 | if (isShowError) { 36 | mLoadingPager.showError(throwable); 37 | } 38 | } 39 | 40 | public void showError() { 41 | showError(null, true); 42 | } 43 | 44 | public void showError(Throwable throwable) { 45 | showError(throwable, true); 46 | } 47 | 48 | public void showLoading() { 49 | mLoadingPager.showLoading(); 50 | } 51 | 52 | public void showSuccess() { 53 | mLoadingPager.showSuccess(); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/tempalet/helper/toolbar/BaseToolBarHelperImpl.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.tempalet.helper.toolbar; 2 | 3 | import android.content.Context; 4 | import android.os.Build; 5 | import android.support.annotation.IdRes; 6 | import android.support.annotation.NonNull; 7 | import android.support.annotation.Nullable; 8 | import android.support.design.widget.AppBarLayout; 9 | import android.support.v7.widget.Toolbar; 10 | import android.util.SparseArray; 11 | import android.util.TypedValue; 12 | import android.view.LayoutInflater; 13 | import android.view.View; 14 | 15 | import com.baozi.mvp.MVPManager; 16 | import com.baozi.mvp.R; 17 | import com.baozi.mvp.tempalet.options.ToolbarOptions; 18 | import com.baozi.mvp.view.ToolbarView; 19 | 20 | /** 21 | * @author jlanglang 2017/2/22 16:58 22 | */ 23 | public class BaseToolBarHelperImpl extends ToolbarHelper { 24 | protected Toolbar mToolbar; 25 | protected ToolbarView mToolbarView; 26 | private Context mContext; 27 | private AppBarLayout mAppBarLayout; 28 | private SparseArray mViews; 29 | 30 | public BaseToolBarHelperImpl(@NonNull ToolbarView toolbarView) { 31 | mToolbarView = toolbarView; 32 | mContext = toolbarView.getContext(); 33 | mViews = new SparseArray<>(); 34 | 35 | //初始化AppBarLayout 36 | mAppBarLayout = toolbarView.getContentView().findViewById(R.id.app_bar); 37 | if (mAppBarLayout != null) { 38 | mAppBarLayout.removeAllViews(); 39 | } 40 | //将toolbarLayout添加到AppBarLayout中 41 | View inflate = LayoutInflater.from(mContext).inflate(toolbarView.getToolbarLayout(), mAppBarLayout, true); 42 | //如果find不为null,则设置toolbar 43 | mToolbar = (Toolbar) inflate.findViewById(R.id.tl_custom); 44 | if (mToolbar != null) { 45 | initToolbar(); 46 | ToolbarOptions mToolbarOptions = mToolbarView.getToolbarOptions(); 47 | if (mToolbarOptions == null) { 48 | mToolbarOptions = MVPManager.getToolbarOptions(); 49 | } 50 | setToolbarOptions(mToolbarOptions); 51 | } 52 | } 53 | 54 | public void setToolbarOptions(ToolbarOptions options) { 55 | if (options == null || mToolbar == null) { 56 | return; 57 | } 58 | int toolbarColor = options.getToolbarColor(); 59 | int toolbarDrawable = options.getToolbarDrawable(); 60 | int toolbarHeight = options.getToolbarHeight(); 61 | 62 | if (toolbarColor != 0) { 63 | mToolbar.setBackgroundColor(toolbarColor); 64 | } 65 | if (toolbarDrawable != 0) { 66 | mToolbar.setBackgroundResource(toolbarDrawable); 67 | } 68 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 69 | mAppBarLayout.setElevation(options.getElevation()); 70 | mAppBarLayout.setTranslationZ(options.getElevation()); 71 | mAppBarLayout.invalidate(); 72 | } 73 | if (toolbarHeight > 0) { 74 | float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, toolbarHeight, 75 | mContext.getResources().getDisplayMetrics()); 76 | mToolbar.getLayoutParams().height = Math.round(px); 77 | mToolbar.requestLayout(); 78 | } 79 | } 80 | 81 | public void initToolbar() { 82 | 83 | } 84 | 85 | /** 86 | * 从AppBarLayout中获取控件 87 | * 88 | * @param viewId 控件Id 89 | * @param 返回泛型,减少强转操作 90 | * @return 可能为null 91 | */ 92 | @Nullable 93 | @Override 94 | public V findViewFromAppBar(@IdRes int viewId) { 95 | View view = mViews.get(viewId); 96 | if (view == null && mAppBarLayout != null) { 97 | view = mAppBarLayout.findViewById(viewId); 98 | mViews.put(viewId, view); 99 | } 100 | return (V) view; 101 | } 102 | 103 | /** 104 | * 设置控件滑动效果 105 | * 106 | * @param viewId view id 107 | * @param flag 设置的滑动flag 108 | * @return 设置成功返回true, 设置失败返回false 109 | */ 110 | @Override 111 | public boolean setScrollFlag(@IdRes int viewId, @AppBarLayout.LayoutParams.ScrollFlags int flag) { 112 | View view = findViewFromAppBar(viewId); 113 | try { 114 | AppBarLayout.LayoutParams layoutParams = (AppBarLayout.LayoutParams) view.getLayoutParams(); 115 | layoutParams.setScrollFlags(flag); 116 | } catch (ClassCastException e) { 117 | e.printStackTrace(); 118 | return false; 119 | } catch (NullPointerException e) { 120 | e.printStackTrace(); 121 | return false; 122 | } 123 | return true; 124 | } 125 | 126 | 127 | @Override 128 | public AppBarLayout getAppBarLayout() { 129 | return mAppBarLayout; 130 | } 131 | 132 | public Toolbar getToolbar() { 133 | return mToolbar; 134 | } 135 | 136 | public T findView(int id) { 137 | return mToolbar.findViewById(id); 138 | } 139 | 140 | 141 | } 142 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/tempalet/helper/toolbar/EmptyToolbarHelperImpl.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.tempalet.helper.toolbar; 2 | 3 | import android.view.View; 4 | 5 | import com.baozi.mvp.tempalet.options.ToolbarOptions; 6 | 7 | /** 8 | * 9 | */ 10 | public class EmptyToolbarHelperImpl extends ToolbarHelper { 11 | 12 | @Override 13 | public boolean setScrollFlag(int viewId, int flag) { 14 | return false; 15 | } 16 | 17 | @Override 18 | public V findViewFromAppBar(int viewId) { 19 | return null; 20 | } 21 | 22 | @Override 23 | public void setToolbarOptions(ToolbarOptions toolbarOptions) { 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/tempalet/helper/toolbar/SimpleToolbarHelperImpl.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.tempalet.helper.toolbar; 2 | 3 | import android.support.annotation.ColorInt; 4 | import android.support.annotation.NonNull; 5 | import android.text.TextUtils; 6 | import android.view.View; 7 | import android.widget.ImageButton; 8 | import android.widget.LinearLayout; 9 | import android.widget.TextView; 10 | 11 | import com.baozi.mvp.R; 12 | import com.baozi.mvp.tempalet.options.ToolbarOptions; 13 | import com.baozi.mvp.view.ToolbarView; 14 | 15 | /** 16 | * @author jlanglang 2017/2/21 16:44 17 | * @版本 2.0 18 | * @Change 19 | */ 20 | 21 | public class SimpleToolbarHelperImpl extends BaseToolBarHelperImpl { 22 | private TextView mTitleView; 23 | @ColorInt 24 | private int mOtherTextColor; 25 | private int mOtherTextSize; 26 | private LinearLayout endActions; 27 | 28 | 29 | SimpleToolbarHelperImpl(ToolbarView uiView) { 30 | super(uiView); 31 | } 32 | 33 | @Override 34 | public void initToolbar() { 35 | mTitleView = findView(R.id.tv_title); 36 | endActions = findView(R.id.ll_end_group); 37 | ToolbarHelper.SimpleInitToolbar(mToolbarView.getContext(), mToolbar, false); 38 | } 39 | 40 | @Override 41 | public void setToolbarOptions(ToolbarOptions options) { 42 | super.setToolbarOptions(options); 43 | int mTitleSize = options.getTitleSize(); 44 | int mTitleColor = options.getTitleColor(); 45 | mOtherTextColor = options.getOtherTextColor(); 46 | mOtherTextSize = options.getOtherTextSize(); 47 | if (mOtherTextColor != 0 || mOtherTextSize != 0) { 48 | notifyToolbarText(); 49 | } 50 | boolean noBack = options.isNoBack(); 51 | if (mTitleSize != 0) { 52 | mTitleView.setTextSize(mTitleSize); 53 | } 54 | if (mTitleColor != 0) { 55 | mTitleView.setTextColor(mTitleColor); 56 | } 57 | if (!noBack) { 58 | int backDrawable = options.getBackDrawable(); 59 | if (backDrawable == 0) { 60 | backDrawable = R.drawable.back; 61 | } 62 | setLeading(backDrawable); 63 | } 64 | } 65 | 66 | private void notifyToolbarText() { 67 | int childCount = mToolbar.getChildCount(); 68 | for (int i = 0; i < childCount; i++) { 69 | View childAt = mToolbar.getChildAt(childCount); 70 | if (childAt instanceof TextView) { 71 | if (mOtherTextColor != 0) { 72 | ((TextView) childAt).setTextColor(mOtherTextColor); 73 | } 74 | if (mOtherTextSize != 0) { 75 | ((TextView) childAt).setTextSize(mOtherTextSize); 76 | } 77 | } 78 | } 79 | } 80 | 81 | @Override 82 | public SimpleToolbarHelperImpl setTextSize(int size) { 83 | return this; 84 | } 85 | 86 | @Override 87 | public SimpleToolbarHelperImpl setTitleSize(int size) { 88 | mTitleView.setTextSize(size); 89 | return this; 90 | } 91 | 92 | 93 | public SimpleToolbarHelperImpl setTitle(@NonNull String titleView) { 94 | mTitleView.setText(titleView); 95 | return this; 96 | } 97 | 98 | @Override 99 | public SimpleToolbarHelperImpl setTitle(int titleId) { 100 | String title = mToolbarView.getContext().getResources().getString(titleId); 101 | setTitle(title); 102 | return this; 103 | } 104 | 105 | 106 | @Override 107 | public SimpleToolbarHelperImpl setCanBack(boolean canBack) { 108 | super.setCanBack(canBack); 109 | findView(R.id.ll_start_group).setVisibility(canBack ? View.VISIBLE : View.GONE); 110 | return this; 111 | } 112 | 113 | @Override 114 | public SimpleToolbarHelperImpl setLeading(String leading) { 115 | return setLeading(leading, null); 116 | } 117 | 118 | public SimpleToolbarHelperImpl setLeading(String leading, View.OnClickListener click) { 119 | TextView view = findView(R.id.tv_start); 120 | if (TextUtils.isEmpty(leading)) { 121 | view.setVisibility(View.GONE); 122 | return this; 123 | } 124 | view.setVisibility(View.VISIBLE); 125 | view.setText(leading); 126 | if (click == null) { 127 | view.setOnClickListener(new View.OnClickListener() { 128 | @Override 129 | public void onClick(View v) { 130 | mToolbarView.onBackPressed(); 131 | } 132 | }); 133 | } 134 | return this; 135 | } 136 | 137 | @Override 138 | public SimpleToolbarHelperImpl setLeading(int leadRes) { 139 | return setLeading(leadRes, null); 140 | } 141 | 142 | public SimpleToolbarHelperImpl setLeading(int leadRes, View.OnClickListener click) { 143 | ImageButton view = findView(R.id.ib_start); 144 | view.setVisibility(View.VISIBLE); 145 | view.setImageResource(leadRes); 146 | if (click == null) { 147 | view.setOnClickListener(new View.OnClickListener() { 148 | @Override 149 | public void onClick(View v) { 150 | mToolbarView.onBackPressed(); 151 | } 152 | }); 153 | } 154 | return this; 155 | } 156 | 157 | @Override 158 | public SimpleToolbarHelperImpl addActions(View view) { 159 | if (endActions != null && view != null) { 160 | if (view instanceof TextView) { 161 | if (mOtherTextColor != 0) { 162 | ((TextView) view).setTextColor(mOtherTextColor); 163 | } 164 | if (mOtherTextSize != 0) { 165 | ((TextView) view).setTextSize(mOtherTextSize); 166 | } 167 | } 168 | endActions.addView(view); 169 | } 170 | return this; 171 | } 172 | 173 | public void setEndText(String str, View.OnClickListener clickListener) { 174 | if (endActions != null) { 175 | TextView view = findView(R.id.tv_end); 176 | if (str == null) { 177 | view.setVisibility(View.GONE); 178 | return; 179 | } 180 | view.setVisibility(View.VISIBLE); 181 | view.setText(str); 182 | view.setOnClickListener(clickListener); 183 | } 184 | } 185 | 186 | /** 187 | * @param drawable 设置0则隐藏 188 | * @param clickListener 点击监听 189 | */ 190 | public void setEndImage(int drawable, View.OnClickListener clickListener) { 191 | if (endActions != null) { 192 | ImageButton view = findView(R.id.ib_end); 193 | if (drawable == 0) { 194 | view.setVisibility(View.GONE); 195 | return; 196 | } 197 | view.setVisibility(View.VISIBLE); 198 | view.setImageResource(drawable); 199 | view.setOnClickListener(clickListener); 200 | } 201 | } 202 | } 203 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/tempalet/helper/toolbar/ToolbarHelper.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.tempalet.helper.toolbar; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.DrawableRes; 5 | import android.support.annotation.IdRes; 6 | import android.support.annotation.NonNull; 7 | import android.support.annotation.StringRes; 8 | import android.support.design.widget.AppBarLayout; 9 | import android.support.v7.app.ActionBar; 10 | import android.support.v7.app.AppCompatActivity; 11 | import android.support.v7.widget.Toolbar; 12 | import android.view.View; 13 | 14 | import com.baozi.mvp.R; 15 | import com.baozi.mvp.tempalet.options.ToolbarOptions; 16 | import com.baozi.mvp.view.ToolbarView; 17 | 18 | /** 19 | * @author jlanglang 2017/2/21 16:31 20 | * @版本 2.0 21 | * @Change 22 | */ 23 | public abstract class ToolbarHelper { 24 | public static final int TOOLBAR_TEMPLATE_DEFAULT = R.layout.toolbar_template_default; 25 | 26 | public ToolbarHelper() { 27 | 28 | } 29 | 30 | public static ToolbarHelper Create(@NonNull ToolbarView toolbarView) { 31 | int toolbarLayout = toolbarView.getToolbarLayout(); 32 | if (toolbarLayout <= 0) { 33 | return new EmptyToolbarHelperImpl(); 34 | } else if (toolbarLayout == TOOLBAR_TEMPLATE_DEFAULT) { 35 | return new SimpleToolbarHelperImpl(toolbarView); 36 | } else { 37 | return new BaseToolBarHelperImpl(toolbarView); 38 | } 39 | } 40 | 41 | /** 42 | * 快速设置Toolbar,取消边距,隐藏所有默认的显示. 43 | * setDisplayShowCustomEnabled(),setDisplayHomeAsUpEnabled() 44 | * setDisplayShowTitleEnabled(),setDisplayShowHomeEnable() 45 | * 都将设置为false 46 | * 47 | * @param context 继承Appcompat的activity的上下文 48 | * @param toolbar 将要设置的Toolbar 49 | */ 50 | 51 | public static void SimpleInitToolbar(Context context, @NonNull Toolbar toolbar, boolean isMaterialDesign) { 52 | if (context instanceof AppCompatActivity) { 53 | AppCompatActivity activity = (AppCompatActivity) context; 54 | toolbar.setContentInsetsAbsolute(0, 0); 55 | activity.setSupportActionBar(toolbar); 56 | ActionBar supportActionBar = activity.getSupportActionBar(); 57 | if (supportActionBar != null) { 58 | supportActionBar.setDisplayShowCustomEnabled(isMaterialDesign); 59 | supportActionBar.setDisplayHomeAsUpEnabled(isMaterialDesign); 60 | supportActionBar.setDisplayShowTitleEnabled(isMaterialDesign); 61 | supportActionBar.setDisplayShowHomeEnabled(isMaterialDesign); 62 | } 63 | } 64 | } 65 | 66 | 67 | /** 68 | * 获取AppBarLayout 69 | * 70 | * @return 71 | */ 72 | public AppBarLayout getAppBarLayout() { 73 | return null; 74 | } 75 | 76 | /** 77 | * 获取Toolbar 78 | * 79 | * @return 80 | */ 81 | public Toolbar getToolbar() { 82 | return null; 83 | } 84 | 85 | /** 86 | * 设置滑动Flag 87 | * 88 | * @param viewId 89 | * @param flag 90 | * @return 91 | */ 92 | public abstract boolean setScrollFlag(@IdRes int viewId, @AppBarLayout.LayoutParams.ScrollFlags int flag); 93 | 94 | /** 95 | * 获取AppBarLayout中的View 96 | * 97 | * @param viewId 98 | * @param 99 | * @return 100 | */ 101 | public abstract V findViewFromAppBar(@IdRes int viewId); 102 | 103 | /** 104 | * 获取Toolbar配置 105 | * 106 | * @return 107 | */ 108 | public abstract void setToolbarOptions(ToolbarOptions toolbarOptions); 109 | 110 | /** 111 | * 设置title 112 | * 113 | * @param str 114 | */ 115 | public ToolbarHelper setTitle(@NonNull String str) { 116 | return this; 117 | } 118 | 119 | public ToolbarHelper setTitle(@StringRes int str) { 120 | return this; 121 | } 122 | 123 | 124 | public ToolbarHelper setCanBack(boolean canBack) { 125 | return this; 126 | } 127 | 128 | public ToolbarHelper setLeading(String leading) { 129 | return this; 130 | } 131 | 132 | public ToolbarHelper setLeading(@DrawableRes int leadRes) { 133 | return this; 134 | } 135 | 136 | public ToolbarHelper addActions(View view) { 137 | return this; 138 | } 139 | 140 | public ToolbarHelper setTextSize(int size) { 141 | return this; 142 | } 143 | 144 | public ToolbarHelper setTitleSize(int size) { 145 | return this; 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/tempalet/options/ContentOptions.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.tempalet.options; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.IdRes; 5 | import android.support.annotation.LayoutRes; 6 | import android.view.View; 7 | 8 | import com.baozi.mvp.tempalet.weight.LoadingPager; 9 | 10 | import java.util.Arrays; 11 | import java.util.List; 12 | import java.util.Set; 13 | import java.util.TreeSet; 14 | 15 | /** 16 | * Created by baozi on 2017/10/25. 17 | */ 18 | 19 | public class ContentOptions implements Cloneable { 20 | @LayoutRes 21 | private int emptyLayout; 22 | @LayoutRes 23 | private int errorLayout; 24 | @LayoutRes 25 | private int loadingLayout; 26 | 27 | private Set clickIds; 28 | 29 | 30 | private List throwableList; 31 | 32 | public static ContentOptions create() { 33 | return new ContentOptions(); 34 | } 35 | 36 | public ContentOptions addClickId(@IdRes int clickid) { 37 | if (clickIds == null) { 38 | clickIds = new TreeSet<>(); 39 | clickIds.add(clickid); 40 | } 41 | return this; 42 | } 43 | 44 | public Set getClickIds() { 45 | return clickIds; 46 | } 47 | 48 | @LayoutRes 49 | public int getEmptyLayout() { 50 | return emptyLayout; 51 | } 52 | 53 | public ContentOptions setEmptyLayout(@LayoutRes int emptyLayout) { 54 | this.emptyLayout = emptyLayout; 55 | return this; 56 | } 57 | 58 | @LayoutRes 59 | public int getErrorLayout() { 60 | return errorLayout; 61 | } 62 | 63 | public ContentOptions setErrorLayout(@LayoutRes int errorLayout) { 64 | this.errorLayout = errorLayout; 65 | return this; 66 | } 67 | 68 | @LayoutRes 69 | public int getLoadingLayout() { 70 | return loadingLayout; 71 | } 72 | 73 | public ContentOptions setLoadingLayout(@LayoutRes int loadingLayout) { 74 | this.loadingLayout = loadingLayout; 75 | return this; 76 | } 77 | 78 | 79 | /** 80 | * @param context 上下文 81 | * @param SuccessView 成功正文View 82 | * @return 83 | */ 84 | public LoadingPager buildLoadingPager(Context context, View SuccessView) { 85 | LoadingPager loadingPager = new LoadingPager(context); 86 | loadingPager.setEmptyLayout(emptyLayout); 87 | loadingPager.setErrorLayout(errorLayout); 88 | loadingPager.setLoadingLayout(loadingLayout); 89 | loadingPager.setShowLoading(true); 90 | loadingPager.setSuccessPage(SuccessView); 91 | if (clickIds != null && clickIds.size() > 0) { 92 | for (Integer id : clickIds) { 93 | loadingPager.setRefreshClick(id); 94 | } 95 | } 96 | return loadingPager; 97 | } 98 | 99 | public ContentOptions clone() { 100 | try { 101 | return (ContentOptions) super.clone(); 102 | } catch (CloneNotSupportedException e) { 103 | throw new RuntimeException(e); 104 | } 105 | } 106 | 107 | /** 108 | * 指定异常才显示错误 109 | * 110 | * @param throwable 111 | * @return 112 | */ 113 | public ContentOptions setThrowableList(Class... throwable) { 114 | this.throwableList = Arrays.asList(throwable); 115 | return this; 116 | } 117 | 118 | public List getThrowable() { 119 | return throwableList; 120 | } 121 | } 122 | 123 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/tempalet/options/ToolbarOptions.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.tempalet.options; 2 | 3 | import android.support.annotation.ColorInt; 4 | import android.support.annotation.DrawableRes; 5 | import android.support.annotation.LayoutRes; 6 | 7 | import com.baozi.mvp.tempalet.helper.toolbar.ToolbarHelper; 8 | 9 | /** 10 | * Created by baozi on 2017/10/25. 11 | */ 12 | 13 | public class ToolbarOptions implements Cloneable { 14 | //其他字体大小,颜色 15 | private int otherTextSize; 16 | @ColorInt 17 | private int otherTextColor; 18 | //标题字体大小,颜色 19 | private int titleSize; 20 | @ColorInt 21 | private int titleColor; 22 | //toolbar配置 23 | private int toolbarHeight; 24 | @ColorInt 25 | private int toolbarColor; 26 | @LayoutRes 27 | private int toolbarLayout = ToolbarHelper.TOOLBAR_TEMPLATE_DEFAULT; 28 | @DrawableRes 29 | private int toolbarDrawable; 30 | @DrawableRes 31 | private int statusDrawable; 32 | //返回图标 33 | @DrawableRes 34 | private int backDrawable; 35 | 36 | private boolean noBack; 37 | private float elevation; 38 | 39 | public static ToolbarOptions Create() { 40 | return new ToolbarOptions(); 41 | } 42 | 43 | private ToolbarOptions() { 44 | 45 | } 46 | 47 | @DrawableRes 48 | public int getStatusDrawable() { 49 | return statusDrawable; 50 | } 51 | 52 | public ToolbarOptions setStatusDrawable(@DrawableRes int statusDrawable) { 53 | this.statusDrawable = statusDrawable; 54 | return this; 55 | } 56 | 57 | public int getOtherTextSize() { 58 | return otherTextSize; 59 | } 60 | 61 | public ToolbarOptions setOtherTextSize(int otherTextSize) { 62 | this.otherTextSize = otherTextSize; 63 | return this; 64 | } 65 | 66 | @ColorInt 67 | public int getOtherTextColor() { 68 | return otherTextColor; 69 | } 70 | 71 | public ToolbarOptions setOtherTextColor(@ColorInt int otherTextColor) { 72 | this.otherTextColor = otherTextColor; 73 | return this; 74 | } 75 | 76 | public int getTitleSize() { 77 | return titleSize; 78 | } 79 | 80 | public ToolbarOptions setTitleSize(int titleSize) { 81 | this.titleSize = titleSize; 82 | return this; 83 | } 84 | 85 | @ColorInt 86 | public int getTitleColor() { 87 | return titleColor; 88 | } 89 | 90 | public ToolbarOptions setTitleColor(@ColorInt int titleColor) { 91 | this.titleColor = titleColor; 92 | return this; 93 | } 94 | 95 | public int getToolbarHeight() { 96 | return toolbarHeight; 97 | } 98 | 99 | public ToolbarOptions setToolbarHeight(int toolbarHeight) { 100 | this.toolbarHeight = toolbarHeight; 101 | return this; 102 | } 103 | 104 | @ColorInt 105 | public int getToolbarColor() { 106 | return toolbarColor; 107 | } 108 | 109 | public ToolbarOptions setToolbarColor(@ColorInt int toolbarColor) { 110 | this.toolbarColor = toolbarColor; 111 | return this; 112 | } 113 | 114 | @DrawableRes 115 | public int getToolbarDrawable() { 116 | return toolbarDrawable; 117 | } 118 | 119 | public ToolbarOptions setToolbarDrawable(@DrawableRes int toolbarDrawable) { 120 | this.toolbarDrawable = toolbarDrawable; 121 | return this; 122 | } 123 | 124 | @DrawableRes 125 | public int getBackDrawable() { 126 | return backDrawable; 127 | } 128 | 129 | public ToolbarOptions setBackDrawable(@DrawableRes int backDrawable) { 130 | this.backDrawable = backDrawable; 131 | return this; 132 | } 133 | 134 | public boolean isNoBack() { 135 | return noBack; 136 | } 137 | 138 | public ToolbarOptions setNoBack(boolean noBack) { 139 | this.noBack = noBack; 140 | return this; 141 | } 142 | 143 | public int getToolbarLayout() { 144 | return toolbarLayout; 145 | } 146 | 147 | public ToolbarOptions setToolbarLayout(@LayoutRes int toolbarLayout) { 148 | this.toolbarLayout = toolbarLayout; 149 | return this; 150 | } 151 | 152 | public ToolbarOptions clone() { 153 | try { 154 | return (ToolbarOptions) super.clone(); 155 | } catch (CloneNotSupportedException e) { 156 | throw new RuntimeException(e); 157 | } 158 | } 159 | 160 | public ToolbarOptions setElevation(float elevation) { 161 | this.elevation = elevation; 162 | return this; 163 | } 164 | 165 | public float getElevation() { 166 | return elevation; 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/tempalet/weight/LoadingPager.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.tempalet.weight; 2 | 3 | import android.annotation.TargetApi; 4 | import android.content.Context; 5 | import android.os.Build; 6 | import android.support.annotation.IdRes; 7 | import android.util.AttributeSet; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.FrameLayout; 12 | 13 | import com.baozi.mvp.MVPManager; 14 | 15 | import java.util.List; 16 | 17 | /** 18 | * 19 | */ 20 | public class LoadingPager extends FrameLayout { 21 | private Context context; 22 | private View mErrorPage; 23 | private View mLoadingPage; 24 | private View mEmptyPage; 25 | private View mSuccessPage; 26 | private int emptyLayout; 27 | private int errorLayout; 28 | private int loadingLayout; 29 | public static final int STATE_EMPTY = 0; 30 | public static final int STATE_ERROR = 1; 31 | public static final int STATE_LOADING = 2; 32 | public static final int STATE_SUCCESS = 3; 33 | private int mCurrentState; 34 | private boolean isShowLoading; 35 | 36 | private OnRefreshListener refreshListener; 37 | 38 | public LoadingPager(Context context) { 39 | super(context); 40 | this.context = context; 41 | } 42 | 43 | public LoadingPager(Context context, AttributeSet attrs) { 44 | super(context, attrs); 45 | this.context = context; 46 | } 47 | 48 | public LoadingPager(Context context, AttributeSet attrs, int defStyleAttr) { 49 | super(context, attrs, defStyleAttr); 50 | } 51 | 52 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) 53 | public LoadingPager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 54 | super(context, attrs, defStyleAttr, defStyleRes); 55 | } 56 | 57 | public View getSuccessPage() { 58 | return mSuccessPage; 59 | } 60 | 61 | public void setSuccessPage(View successPage) { 62 | if (successPage == null || mSuccessPage != null) { 63 | return; 64 | } 65 | addView(successPage, 0, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 66 | mSuccessPage = successPage; 67 | initView(); 68 | } 69 | 70 | 71 | public void setRefreshClick(@IdRes int id) { 72 | View viewById = findViewById(id); 73 | if (viewById != null) { 74 | viewById.setOnClickListener(new OnClickListener() { 75 | @Override 76 | public void onClick(View v) { 77 | triggerInit(); 78 | } 79 | }); 80 | } 81 | } 82 | 83 | public void setViewClick(@IdRes int id, OnClickListener onClickListener) { 84 | View viewById = findViewById(id); 85 | if (viewById != null) { 86 | viewById.setOnClickListener(onClickListener); 87 | } 88 | } 89 | 90 | /** 91 | * xml写法 92 | */ 93 | @Override 94 | protected void onFinishInflate() { 95 | super.onFinishInflate(); 96 | if (mSuccessPage != null) { 97 | return; 98 | } 99 | int childCount = getChildCount(); 100 | for (int i = 0; i < childCount; i++) { 101 | View childAt = getChildAt(i); 102 | if (childAt == mEmptyPage || childAt == mLoadingPage || childAt == mErrorPage) { 103 | continue; 104 | } 105 | mSuccessPage = childAt; 106 | } 107 | refreshUIByState(); 108 | } 109 | 110 | @Override 111 | public void addView(View child) { 112 | super.addView(child); 113 | } 114 | 115 | private void initView() { 116 | if (isShowLoading()) { 117 | mLoadingPage = initPage(mLoadingPage, loadingLayout); 118 | } 119 | mEmptyPage = initPage(mEmptyPage, emptyLayout); 120 | mErrorPage = initPage(mErrorPage, errorLayout); 121 | triggerInit(); 122 | } 123 | 124 | private View initPage(View view, int layout) { 125 | //如果已经添加则直接return 126 | if (indexOfChild(view) != -1) { 127 | return view; 128 | } 129 | //如果未添加但view不为null.则添加 130 | if (view != null) { 131 | addView(view); 132 | return view; 133 | } 134 | //如果未添加且为null,则创建并添加 135 | if (layout == 0) { 136 | view = new View(context); 137 | } else { 138 | view = LayoutInflater.from(context).inflate(layout, this, false); 139 | } 140 | addView(view); 141 | return view; 142 | } 143 | 144 | public boolean isShowLoading() { 145 | return isShowLoading; 146 | } 147 | 148 | public void setShowLoading(boolean showLoading) { 149 | if (!showLoading) { 150 | removeView(mLoadingPage); 151 | } else { 152 | mLoadingPage = initPage(mLoadingPage, loadingLayout); 153 | } 154 | isShowLoading = showLoading; 155 | } 156 | 157 | public void setEmptyPage(View emptyPage) { 158 | removeView(mEmptyPage); 159 | mEmptyPage = emptyPage; 160 | addView(mEmptyPage); 161 | } 162 | 163 | public void setErrorPage(View errorPage) { 164 | removeView(mErrorPage); 165 | mErrorPage = errorPage; 166 | addView(mErrorPage); 167 | } 168 | 169 | public void setLoadingPage(View loadingPage) { 170 | removeView(mLoadingPage); 171 | mLoadingPage = loadingPage; 172 | addView(mLoadingPage); 173 | } 174 | 175 | public void setEmptyLayout(int emptyLayout) { 176 | this.emptyLayout = emptyLayout; 177 | } 178 | 179 | public void setErrorLayout(int errorLayout) { 180 | this.errorLayout = errorLayout; 181 | } 182 | 183 | public void setLoadingLayout(int loadingLayout) { 184 | this.loadingLayout = loadingLayout; 185 | } 186 | 187 | public int getErrorLayout() { 188 | return errorLayout; 189 | } 190 | 191 | public int getLoadingLayout() { 192 | return loadingLayout; 193 | } 194 | 195 | public int getEmptyLayout() { 196 | return emptyLayout; 197 | } 198 | 199 | public OnRefreshListener getRefreshListener() { 200 | return refreshListener; 201 | } 202 | 203 | public void setRefreshListener(OnRefreshListener refreshListener) { 204 | this.refreshListener = refreshListener; 205 | } 206 | 207 | /** 208 | * 加载前刷新页面 209 | */ 210 | private void refreshUIByState() { 211 | if (isShowLoading) { 212 | mLoadingPage.setVisibility(mCurrentState == STATE_LOADING ? View.VISIBLE : View.GONE); 213 | } 214 | mErrorPage.setVisibility(mCurrentState == STATE_ERROR ? View.VISIBLE : View.GONE); 215 | mEmptyPage.setVisibility(mCurrentState == STATE_EMPTY ? View.VISIBLE : View.GONE); 216 | } 217 | 218 | /** 219 | * 刷新页面状态 220 | */ 221 | public void triggerInit() { 222 | if (mCurrentState != STATE_LOADING) { 223 | mCurrentState = STATE_LOADING; 224 | if (refreshListener != null) { 225 | refreshListener.onRefresh(); 226 | } 227 | refreshUIByState(); 228 | } 229 | } 230 | 231 | 232 | /** 233 | * 根据请求结果显示页面 234 | * 235 | * @param stateEmpty 236 | */ 237 | public void show(int stateEmpty) { 238 | if (stateEmpty > STATE_SUCCESS || stateEmpty < STATE_EMPTY) { 239 | throw new IllegalStateException("错误的状态"); 240 | } 241 | mCurrentState = stateEmpty; 242 | refreshUIByState(); 243 | } 244 | 245 | public void showEmpty() { 246 | show(STATE_EMPTY); 247 | } 248 | 249 | public void showSuccess() { 250 | show(STATE_SUCCESS); 251 | } 252 | 253 | public void showLoading() { 254 | show(STATE_LOADING); 255 | } 256 | 257 | public void showError(Throwable throwable) { 258 | if (throwable == null) { 259 | show(STATE_ERROR); 260 | return; 261 | } 262 | List list = MVPManager.getContentOptions().getThrowable(); 263 | if (list != null && list.indexOf(throwable.getClass()) != -1) { 264 | show(STATE_ERROR); 265 | } 266 | } 267 | 268 | public interface OnRefreshListener { 269 | void onRefresh(); 270 | } 271 | 272 | } 273 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/utils/StatusBarUtil.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.utils; 2 | 3 | import android.app.Activity; 4 | import android.os.Build; 5 | import android.support.annotation.DrawableRes; 6 | import android.view.View; 7 | import android.view.Window; 8 | import android.view.WindowManager; 9 | 10 | import java.lang.reflect.Field; 11 | import java.lang.reflect.Method; 12 | 13 | public class StatusBarUtil { 14 | 15 | /** 16 | * 设置状态栏黑色字体图标, 17 | * 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android 18 | * 19 | * @return 1:MIUUI 2:Flyme 3:android6.0 20 | */ 21 | public static int getStatusBarLightMode(Window window) { 22 | int result = 0; 23 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 24 | if (MIUISetStatusBarLightMode(window, true)) { 25 | result = 1; 26 | } else if (FlymeSetStatusBarLightMode(window, true)) { 27 | result = 2; 28 | } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 29 | window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); 30 | result = 3; 31 | } else {//5.0 32 | 33 | } 34 | } 35 | return result; 36 | } 37 | 38 | /** 39 | * @param activity 要修改的activity 40 | * @param statusBarDrawable 背景色 41 | * @param isLight 是否黑色字体 42 | * @param normalRes 不能修改黑色字体时的颜色 43 | */ 44 | public void setStatusBar(Activity activity, 45 | @DrawableRes int statusBarDrawable, 46 | boolean isLight, 47 | @DrawableRes int normalRes 48 | ) { 49 | if (activity == null) { 50 | return; 51 | } 52 | Window window = activity.getWindow(); 53 | int statusId = activity.getResources().getIdentifier("statusBarBackground", "id", "android"); 54 | View statusBarView = window.findViewById(statusId); 55 | if (statusBarView != null) { 56 | if (isLight) { 57 | statusBarView.setBackgroundDrawable(null); 58 | } 59 | statusBarView.setBackgroundResource(statusBarDrawable); 60 | } 61 | //5.0以下不允许修改字体颜色 62 | if (isLight && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { 63 | if (statusBarView != null) { 64 | statusBarView.setBackgroundResource(normalRes); 65 | } 66 | } 67 | 68 | StatusBarUtil.setStatusBarLightMode(window, isLight); 69 | } 70 | 71 | /** 72 | * 已知系统类型时,设置状态栏黑色字体图标。 73 | * 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android 74 | */ 75 | public static void setStatusBarLightMode(Window window, boolean isLight) { 76 | int type = getStatusBarLightMode(window); 77 | if (type == 1) { 78 | MIUISetStatusBarLightMode(window, isLight); 79 | } else if (type == 2) { 80 | FlymeSetStatusBarLightMode(window, isLight); 81 | } else if (type == 3) { 82 | if (isLight && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 83 | window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); 84 | } else { 85 | window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); 86 | } 87 | } else {//5.0 88 | //没得玩 89 | } 90 | } 91 | 92 | 93 | /** 94 | * 设置状态栏图标为深色和魅族特定的文字风格 95 | * 可以用来判断是否为Flyme用户 96 | * 97 | * @param window 需要设置的窗口 98 | * @param dark 是否把状态栏字体及图标颜色设置为深色 99 | * @return boolean 成功执行返回true 100 | */ 101 | public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) { 102 | boolean result = false; 103 | if (window != null) { 104 | try { 105 | WindowManager.LayoutParams lp = window.getAttributes(); 106 | Field darkFlag = WindowManager.LayoutParams.class 107 | .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON"); 108 | Field meizuFlags = WindowManager.LayoutParams.class 109 | .getDeclaredField("meizuFlags"); 110 | darkFlag.setAccessible(true); 111 | meizuFlags.setAccessible(true); 112 | int bit = darkFlag.getInt(null); 113 | int value = meizuFlags.getInt(lp); 114 | if (dark) { 115 | value |= bit; 116 | } else { 117 | value &= ~bit; 118 | } 119 | meizuFlags.setInt(lp, value); 120 | window.setAttributes(lp); 121 | result = true; 122 | } catch (Exception e) { 123 | 124 | } 125 | } 126 | return result; 127 | } 128 | 129 | /** 130 | * 设置状态栏字体图标为深色,需要MIUIV6以上 131 | * 132 | * @param window 需要设置的窗口 133 | * @param dark 是否把状态栏字体及图标颜色设置为深色 134 | * @return boolean 成功执行返回true 135 | */ 136 | public static boolean MIUISetStatusBarLightMode(Window window, boolean dark) { 137 | boolean result = false; 138 | if (window != null) { 139 | Class clazz = window.getClass(); 140 | try { 141 | int darkModeFlag = 0; 142 | Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams"); 143 | Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE"); 144 | darkModeFlag = field.getInt(layoutParams); 145 | Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class); 146 | if (dark) { 147 | extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//状态栏透明且黑色字体 148 | } else { 149 | extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体 150 | } 151 | result = true; 152 | } catch (Exception e) { 153 | 154 | } 155 | } 156 | return result; 157 | } 158 | 159 | } -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/view/BaseView.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.view; 2 | 3 | import android.content.Context; 4 | 5 | /** 6 | * Created by baozi on 2016/11/24. 7 | */ 8 | public interface BaseView { 9 | Context getContext(); 10 | } 11 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/view/LoadMoreView.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.view; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.widget.SwipeRefreshLayout; 6 | import android.support.v7.widget.RecyclerView; 7 | 8 | /** 9 | * Created by baozi on 2017/12/5. 10 | */ 11 | 12 | public interface LoadMoreView { 13 | Context getContext(); 14 | @Nullable 15 | SwipeRefreshLayout getSwipeRefreshLayout(); 16 | 17 | RecyclerView getRecyclerView(); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/view/PageFragmentView.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.view; 2 | 3 | import android.support.v4.app.Fragment; 4 | import android.support.v4.app.FragmentManager; 5 | 6 | /** 7 | * Created by Administrator on 2017/8/8 0008. 8 | */ 9 | 10 | public interface PageFragmentView extends PageView { 11 | FragmentManager getFgManager(); 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/view/PageView.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.view; 2 | 3 | import android.support.v4.view.ViewPager; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * Created by Administrator on 2017/8/8 0008. 9 | */ 10 | 11 | public interface PageView extends BaseView { 12 | 13 | ViewPager getViewPage(); 14 | 15 | List getPage(); 16 | 17 | /** 18 | * 是否需要切换动画 19 | * 20 | * @return 21 | */ 22 | boolean isAnimation(); 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/view/TabLayoutView.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.view; 2 | 3 | import android.support.design.widget.TabLayout; 4 | 5 | public interface TabLayoutView extends BaseView { 6 | 7 | TabLayout getTabLayout(); 8 | 9 | String[] getTabString(); 10 | 11 | int[] getTabDrawables(); 12 | 13 | int getTabLayoutItem(); 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/view/ToolbarView.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.view; 2 | 3 | import android.support.annotation.LayoutRes; 4 | import android.view.View; 5 | 6 | import com.baozi.mvp.tempalet.helper.toolbar.ToolbarHelper; 7 | import com.baozi.mvp.tempalet.options.ToolbarOptions; 8 | 9 | /** 10 | * @author jlanglang 2017/3/4 17:44 11 | * @版本 2.0 12 | * @Change 13 | */ 14 | 15 | public interface ToolbarView extends BaseView { 16 | /** 17 | * 获得ToolbarHelper,Presenter可以通过ToolbarHelper的来控制toolbar 18 | */ 19 | ToolbarHelper getToolbarHelper(); 20 | 21 | /** 22 | * 通过这个修改toolbar的样式layout,不需要可以传0或者-1; 23 | * 24 | * @return 25 | */ 26 | @LayoutRes 27 | int getToolbarLayout(); 28 | 29 | /** 30 | * 回退 31 | */ 32 | void onBackPressed(); 33 | 34 | View getContentView(); 35 | 36 | ToolbarOptions getToolbarOptions(); 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/com/baozi/mvp/view/UIView.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp.view; 2 | 3 | import android.content.res.Resources; 4 | import android.support.annotation.IdRes; 5 | import android.support.annotation.Nullable; 6 | import android.support.v7.app.ActionBar; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.support.v7.widget.Toolbar; 9 | import android.view.View; 10 | import android.view.Window; 11 | 12 | /** 13 | * Created by baozi on 2017/2/20. 14 | * 用户页面,操作页面,对应Activity,frgament... 15 | */ 16 | 17 | public interface UIView extends BaseView { 18 | /** 19 | * res资源获取 20 | * 21 | * @return 22 | */ 23 | Resources getResources(); 24 | 25 | /** 26 | * 根据id获得控件 27 | * 需要调用在initView()之后,否则会出现NullPointerException 28 | * 29 | * @param viewId 30 | * @param 31 | * @return 32 | */ 33 | 34 | V findView(@IdRes int viewId); 35 | 36 | /** 37 | * 回退 38 | */ 39 | 40 | void onBackPressed(); 41 | 42 | /** 43 | * 获取Activity 44 | * 45 | * @return 46 | */ 47 | AppCompatActivity getAppcompatActivity(); 48 | 49 | /** 50 | * 主要视图View 51 | * 52 | * @return 53 | */ 54 | View getContentView(); 55 | 56 | /** 57 | * 处理异常 58 | * 59 | * @param throwable 60 | */ 61 | void onNewThrowable(Throwable throwable); 62 | 63 | Window getWindow(); 64 | 65 | ActionBar getSupportActionBar(); 66 | 67 | void setSupportActionBar(@Nullable Toolbar toolbar); 68 | 69 | void finishActivity(); 70 | 71 | } 72 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/back.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jlanglang/JMVP/727468103ad1c5999c7682e0417cab19136c9c48/app/src/main/res/drawable-xhdpi/back.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/progress_bar_horizontal_states.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /app/src/main/res/layout/template_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/layout/toolbar_template_default.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 15 | 16 | 24 | 25 | 33 | 34 | 35 | 40 | 41 | 49 | 50 | 58 | 59 | 60 | 68 | -------------------------------------------------------------------------------- /app/src/main/res/values-v21/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | Base application theme. 3 | 4 | 7 | 8 | 14 | 15 | 23 | 24 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /app/src/test/java/com/baozi/mvp/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.baozi.mvp; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | buildscript { 3 | repositories { 4 | maven { 5 | url 'https://maven.fabric.io/public' 6 | } 7 | maven { 8 | url 'https://maven.aliyun.com/repository/jcenter' 9 | } 10 | maven { 11 | url 'https://maven.aliyun.com/repository/google' 12 | } 13 | maven { 14 | url 'https://maven.aliyun.com/repository/gradle-plugin' 15 | } 16 | maven { url "https://jitpack.io" } 17 | mavenCentral() // add repository 18 | } 19 | dependencies { 20 | classpath 'com.android.tools.build:gradle:3.5.0' 21 | } 22 | } 23 | 24 | allprojects { 25 | repositories { 26 | maven { 27 | url 'https://maven.fabric.io/public' 28 | } 29 | maven { 30 | url 'https://maven.aliyun.com/repository/jcenter' 31 | } 32 | maven { 33 | url 'https://maven.aliyun.com/repository/google' 34 | } 35 | maven { 36 | url 'https://maven.aliyun.com/repository/gradle-plugin' 37 | } 38 | maven { url "https://jitpack.io" } 39 | mavenCentral() // add repository 40 | } 41 | } 42 | 43 | task clean(type: Delete) { 44 | delete rootProject.buildDir 45 | } 46 | -------------------------------------------------------------------------------- /demo/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /demo/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | buildToolsVersion '28.0.3' 6 | 7 | defaultConfig { 8 | applicationId "com.baozi.demo" 9 | minSdkVersion 19 10 | targetSdkVersion 28 11 | versionCode 2 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | 25 | } 26 | 27 | dependencies { 28 | implementation fileTree(dir: 'libs', include: ['*.jar']) 29 | implementation project(':app') 30 | implementation 'com.android.support:appcompat-v7:28.0.0' 31 | implementation 'com.android.support:support-v4:28.0.0' 32 | implementation 'com.android.support:design:28.0.0' 33 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 34 | testImplementation 'junit:junit:4.12' 35 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 36 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 37 | implementation 'com.github.bumptech.glide:glide:4.9.0' 38 | } 39 | -------------------------------------------------------------------------------- /demo/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /demo/src/androidTest/java/com/baozi/demo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.baozi.demo; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.baozi.demo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /demo/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /demo/src/main/assets/a.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jlanglang/JMVP/727468103ad1c5999c7682e0417cab19136c9c48/demo/src/main/assets/a.gif -------------------------------------------------------------------------------- /demo/src/main/java/com/baozi/demo/App.java: -------------------------------------------------------------------------------- 1 | package com.baozi.demo; 2 | 3 | import android.app.Application; 4 | 5 | import com.baozi.mvp.MVPManager; 6 | 7 | public class App extends Application { 8 | @Override 9 | public void onCreate() { 10 | super.onCreate(); 11 | MVPManager.setToolbarOptions( 12 | MVPManager.getToolbarOptions() 13 | .setStatusDrawable(R.drawable.bg_toolbar)//设置状态栏的颜色 14 | .setToolbarDrawable(R.drawable.bg_toolbar) 15 | .setToolbarHeight(48) 16 | .setOtherTextColor(getResources().getColor(android.R.color.black)) 17 | .setTitleColor(getResources().getColor(android.R.color.holo_red_dark)) 18 | ); 19 | MVPManager.setContentOptions( 20 | MVPManager.getContentOptions() 21 | .setErrorLayout(R.layout.empty_layout) 22 | .setLoadingLayout(R.layout.load_layout) 23 | ); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /demo/src/main/java/com/baozi/demo/model/MainModel.java: -------------------------------------------------------------------------------- 1 | package com.baozi.demo.model; 2 | 3 | public class MainModel { 4 | public String login() { 5 | return "登陆成功"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /demo/src/main/java/com/baozi/demo/persenter/DemoPresenter.java: -------------------------------------------------------------------------------- 1 | package com.baozi.demo.persenter; 2 | 3 | import com.baozi.demo.ui.at.BaseAt; 4 | import com.baozi.mvp.presenter.BasePresenter; 5 | 6 | public class DemoPresenter extends BasePresenter { 7 | 8 | 9 | @Override 10 | public void cancelNetWork() { 11 | 12 | } 13 | 14 | @Override 15 | public void netWorkError(Throwable throwable) { 16 | mView.getLoadHelper().showError(throwable); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /demo/src/main/java/com/baozi/demo/persenter/MainPresenter.java: -------------------------------------------------------------------------------- 1 | package com.baozi.demo.persenter; 2 | 3 | import com.baozi.demo.ui.at.MainAt; 4 | import com.baozi.mvp.StartFactory; 5 | import com.baozi.mvp.presenter.BasePresenter; 6 | 7 | 8 | public class MainPresenter extends BasePresenter { 9 | @Override 10 | public void onCreate() { 11 | super.onCreate(); 12 | } 13 | 14 | @Override 15 | public void cancelNetWork() { 16 | 17 | } 18 | 19 | @Override 20 | public void netWorkError(Throwable throwable) { 21 | 22 | } 23 | 24 | public void toDemoAt(Class activityClass) { 25 | StartFactory.startActivity(mView, activityClass); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /demo/src/main/java/com/baozi/demo/ui/at/BaseAt.java: -------------------------------------------------------------------------------- 1 | package com.baozi.demo.ui.at; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.text.TextUtils; 6 | import android.view.View; 7 | import android.webkit.WebView; 8 | import android.widget.TextView; 9 | 10 | import com.baozi.demo.R; 11 | import com.baozi.demo.persenter.DemoPresenter; 12 | import com.baozi.mvp.annotation.JView; 13 | import com.baozi.mvp.base.BaseActivity; 14 | 15 | @JView(p = DemoPresenter.class, layout = R.layout.at_main) 16 | public class BaseAt extends BaseActivity { 17 | 18 | @Override 19 | protected void init(@Nullable Bundle savedInstanceState) { 20 | super.init(savedInstanceState); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /demo/src/main/java/com/baozi/demo/ui/at/LoadingAt.java: -------------------------------------------------------------------------------- 1 | package com.baozi.demo.ui.at; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.ImageView; 8 | 9 | import com.baozi.demo.R; 10 | import com.baozi.demo.persenter.MainPresenter; 11 | import com.baozi.mvp.annotation.JView; 12 | import com.baozi.mvp.tempalet.TemplateActivity; 13 | import com.baozi.mvp.tempalet.options.ContentOptions; 14 | import com.bumptech.glide.Glide; 15 | 16 | @JView(layout = R.layout.at_load, p = MainPresenter.class, openLoading = true) 17 | public class LoadingAt extends TemplateActivity { 18 | 19 | @Override 20 | protected void init(@Nullable Bundle savedInstanceState) { 21 | super.init(savedInstanceState); 22 | getLoadHelper().showLoading(); 23 | new Thread() { 24 | @Override 25 | public void run() { 26 | try { 27 | Thread.sleep(3000); 28 | } catch (InterruptedException e) { 29 | e.printStackTrace(); 30 | } 31 | runOnUiThread(new Runnable() { 32 | @Override 33 | public void run() { 34 | getLoadHelper().showError(); 35 | } 36 | }); 37 | try { 38 | Thread.sleep(3000); 39 | } catch (InterruptedException e) { 40 | e.printStackTrace(); 41 | } 42 | runOnUiThread(new Runnable() { 43 | @Override 44 | public void run() { 45 | getLoadHelper().showSuccess(); 46 | } 47 | }); 48 | } 49 | }.start(); 50 | } 51 | 52 | @Override 53 | protected View wrapperContentView(View view) { 54 | ImageView imageView = new ImageView(this); 55 | imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 56 | ((ViewGroup) view).addView(imageView, 0); 57 | Glide.with(this).load("file:///android_asset/a.gif").into(imageView); 58 | return super.wrapperContentView(view); 59 | } 60 | 61 | @Override 62 | public ContentOptions getContentOptions() { 63 | return super.getContentOptions() 64 | .setEmptyLayout(R.layout.empty_layout); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /demo/src/main/java/com/baozi/demo/ui/at/MainAt.java: -------------------------------------------------------------------------------- 1 | package com.baozi.demo.ui.at; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.view.View; 6 | 7 | import com.baozi.demo.R; 8 | import com.baozi.demo.persenter.MainPresenter; 9 | import com.baozi.mvp.annotation.JView; 10 | import com.baozi.mvp.base.BaseActivity; 11 | 12 | @JView(layout = R.layout.at_main, p = MainPresenter.class) 13 | public class MainAt extends BaseActivity { 14 | 15 | @Override 16 | protected int initView(@Nullable Bundle savedInstanceState) { 17 | return R.layout.at_main; 18 | } 19 | 20 | @Override 21 | protected void init(@Nullable Bundle savedInstanceState) { 22 | super.init(savedInstanceState); 23 | findView(R.id.bt_viewPage).setOnClickListener(new View.OnClickListener() { 24 | @Override 25 | public void onClick(View v) { 26 | mPresenter.toDemoAt(PageAt.class); 27 | } 28 | }); 29 | findView(R.id.bt_demo).setOnClickListener(new View.OnClickListener() { 30 | @Override 31 | public void onClick(View v) { 32 | mPresenter.toDemoAt(BaseAt.class); 33 | } 34 | }); 35 | findView(R.id.bt_template).setOnClickListener(new View.OnClickListener() { 36 | @Override 37 | public void onClick(View v) { 38 | mPresenter.toDemoAt(TemplateAt.class); 39 | } 40 | }); 41 | findView(R.id.bt_loading).setOnClickListener(new View.OnClickListener() { 42 | @Override 43 | public void onClick(View v) { 44 | mPresenter.toDemoAt(LoadingAt.class); 45 | } 46 | }); 47 | findView(R.id.bt_fg).setOnClickListener(new View.OnClickListener() { 48 | @Override 49 | public void onClick(View v) { 50 | mPresenter.toDemoAt(TestAt.class); 51 | } 52 | }); 53 | } 54 | 55 | 56 | @Override 57 | public void onNewThrowable(Throwable throwable) { 58 | super.onNewThrowable(throwable); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /demo/src/main/java/com/baozi/demo/ui/at/PageAt.java: -------------------------------------------------------------------------------- 1 | package com.baozi.demo.ui.at; 2 | 3 | import android.support.design.widget.TabLayout; 4 | import android.support.v4.app.Fragment; 5 | import android.support.v4.app.FragmentManager; 6 | import android.support.v4.view.ViewPager; 7 | 8 | import com.baozi.demo.R; 9 | import com.baozi.demo.ui.fg.DemoFg; 10 | import com.baozi.mvp.annotation.JView; 11 | import com.baozi.mvp.presenter.EmptyPresenter; 12 | import com.baozi.mvp.presenter.PagerFragmentPresenter; 13 | import com.baozi.mvp.tempalet.TemplateActivity; 14 | import com.baozi.mvp.view.PageFragmentView; 15 | import com.baozi.mvp.view.TabLayoutView; 16 | 17 | import java.util.Arrays; 18 | import java.util.List; 19 | 20 | @JView(layout = R.layout.at_viewpage) 21 | public class PageAt extends TemplateActivity 22 | implements PageFragmentView, TabLayoutView { 23 | private List f = Arrays.asList( 24 | new DemoFg(), 25 | new DemoFg(), 26 | new DemoFg() 27 | ); 28 | 29 | @Override 30 | protected void onPresentersCreate() { 31 | super.onPresentersCreate(); 32 | PagerFragmentPresenter pagerFragmentPresenter = 33 | new PagerFragmentPresenter(this, this); 34 | pagerFragmentPresenter.onCreate(); 35 | } 36 | 37 | @Override 38 | public FragmentManager getFgManager() { 39 | return getSupportFragmentManager(); 40 | } 41 | 42 | @Override 43 | public List getPage() { 44 | return f; 45 | } 46 | 47 | @Override 48 | public ViewPager getViewPage() { 49 | return findViewById(R.id.vp_content); 50 | } 51 | 52 | 53 | @Override 54 | public boolean isAnimation() { 55 | return true; 56 | } 57 | 58 | @Override 59 | public TabLayout getTabLayout() { 60 | return findView(R.id.tb_content); 61 | } 62 | 63 | @Override 64 | public String[] getTabString() { 65 | return new String[]{"1", "2", "3"}; 66 | } 67 | 68 | @Override 69 | public int[] getTabDrawables() { 70 | return null; 71 | } 72 | 73 | @Override 74 | public int getTabLayoutItem() { 75 | return 0; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /demo/src/main/java/com/baozi/demo/ui/at/TemplateAt.java: -------------------------------------------------------------------------------- 1 | package com.baozi.demo.ui.at; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.NonNull; 5 | import android.support.annotation.Nullable; 6 | import android.support.v7.widget.LinearLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.webkit.WebChromeClient; 12 | import android.webkit.WebSettings; 13 | import android.webkit.WebView; 14 | import android.webkit.WebViewClient; 15 | 16 | import com.baozi.demo.R; 17 | import com.baozi.demo.persenter.DemoPresenter; 18 | import com.baozi.mvp.annotation.JView; 19 | import com.baozi.mvp.tempalet.TemplateActivity; 20 | import com.baozi.mvp.tempalet.options.ToolbarOptions; 21 | 22 | 23 | @JView(p = DemoPresenter.class, layout = R.layout.at_template) 24 | public class TemplateAt extends TemplateActivity { 25 | @Override 26 | protected void init(@Nullable Bundle savedInstanceState) { 27 | super.init(savedInstanceState); 28 | getToolbarHelper().setTitle("我是模板Activity") 29 | .setLeading("关闭"); 30 | WebView viewById = findViewById(R.id.wb_content); 31 | viewById.setWebViewClient(new WebViewClient()); 32 | WebSettings webSetting = viewById.getSettings(); 33 | webSetting.setJavaScriptEnabled(true); 34 | webSetting.setDomStorageEnabled(true); 35 | // webSetting.setAppCacheMaxSize(1024 * 1024 * 8); 36 | webSetting.setUseWideViewPort(true); //将图片调整到适合webview的大小 37 | webSetting.setLoadWithOverviewMode(true); // 缩放至屏幕的大小 38 | webSetting.setAllowFileAccess(true); 39 | webSetting.setAppCacheEnabled(true); 40 | String appCachePath = getApplication().getCacheDir().getAbsolutePath(); 41 | webSetting.setAppCachePath(appCachePath); 42 | webSetting.setDatabaseEnabled(true); 43 | webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE); 44 | viewById.loadUrl("https://smlt.tsign.cn/n8gmKAfuuemC"); 45 | } 46 | 47 | @Override 48 | public ToolbarOptions getToolbarOptions() { 49 | return super.getToolbarOptions() 50 | .setNoBack(true); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /demo/src/main/java/com/baozi/demo/ui/at/TestAt.java: -------------------------------------------------------------------------------- 1 | package com.baozi.demo.ui.at; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.support.annotation.Nullable; 7 | import android.view.View; 8 | 9 | import com.baozi.demo.R; 10 | import com.baozi.mvp.StartFactory; 11 | 12 | 13 | public class TestAt extends Activity { 14 | @Override 15 | protected void onCreate(@Nullable Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | setContentView(R.layout.at_test); 18 | findViewById(R.id.tv_content).setOnClickListener(new View.OnClickListener() { 19 | @Override 20 | public void onClick(View v) { 21 | StartFactory.startActivity(v.getContext(), BaseAt.class); 22 | } 23 | }); 24 | } 25 | 26 | @Override 27 | protected void onStop() { 28 | super.onStop(); 29 | Intent intent = new Intent(this, TestAt.class); 30 | startActivity(intent); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /demo/src/main/java/com/baozi/demo/ui/fg/DemoChildFg.java: -------------------------------------------------------------------------------- 1 | package com.baozi.demo.ui.fg; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.NonNull; 5 | import android.support.v7.widget.LinearLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.text.TextUtils; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.TextView; 12 | 13 | import com.baozi.demo.R; 14 | import com.baozi.mvp.annotation.JView; 15 | import com.baozi.mvp.base.BaseFragment; 16 | import com.baozi.mvp.presenter.EmptyPresenter; 17 | 18 | @JView(layout = R.layout.fg_demo_child) 19 | public class DemoChildFg extends BaseFragment { 20 | @Override 21 | public void init(Bundle savedInstanceState) { 22 | super.init(savedInstanceState); 23 | final RecyclerView view = findView(R.id.rv_content); 24 | view.setLayoutManager(new LinearLayoutManager(mContext)); 25 | view.setAdapter(new RecyclerView.Adapter() { 26 | @NonNull 27 | @Override 28 | public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { 29 | 30 | 31 | View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_test, viewGroup, false); 32 | return new RecyclerView.ViewHolder(inflate) { 33 | }; 34 | } 35 | 36 | @Override 37 | public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) { 38 | 39 | } 40 | 41 | @Override 42 | public int getItemCount() { 43 | return 100; 44 | } 45 | }); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /demo/src/main/java/com/baozi/demo/ui/fg/DemoFg.java: -------------------------------------------------------------------------------- 1 | package com.baozi.demo.ui.fg; 2 | 3 | import android.content.res.Resources; 4 | import android.support.design.widget.TabLayout; 5 | import android.support.v4.app.Fragment; 6 | import android.support.v4.app.FragmentManager; 7 | import android.support.v4.view.ViewPager; 8 | 9 | import com.baozi.demo.R; 10 | import com.baozi.mvp.annotation.JView; 11 | import com.baozi.mvp.base.BaseFragment; 12 | import com.baozi.mvp.presenter.EmptyPresenter; 13 | import com.baozi.mvp.presenter.PagerFragmentPresenter; 14 | import com.baozi.mvp.view.PageFragmentView; 15 | import com.baozi.mvp.view.TabLayoutView; 16 | 17 | import java.util.Arrays; 18 | import java.util.List; 19 | 20 | @JView(layout = R.layout.fg_demo) 21 | public class DemoFg extends BaseFragment implements PageFragmentView, TabLayoutView { 22 | private List f = Arrays.asList( 23 | new DemoChildFg(), 24 | new DemoChildFg(), 25 | new DemoChildFg() 26 | ); 27 | 28 | @Override 29 | protected void onPresentersCreate() { 30 | super.onPresentersCreate(); 31 | PagerFragmentPresenter pagerFragmentPresenter = 32 | new PagerFragmentPresenter(this, this); 33 | pagerFragmentPresenter.onCreate(); 34 | } 35 | 36 | @Override 37 | public boolean isLazy() { 38 | return true; 39 | } 40 | 41 | @Override 42 | public FragmentManager getFgManager() { 43 | return getChildFragmentManager(); 44 | } 45 | 46 | @Override 47 | public ViewPager getViewPage() { 48 | return findView(R.id.vp_content); 49 | } 50 | 51 | @Override 52 | public List getPage() { 53 | return f; 54 | } 55 | 56 | @Override 57 | public boolean isAnimation() { 58 | return false; 59 | } 60 | 61 | @Override 62 | public TabLayout getTabLayout() { 63 | return findView(R.id.tv_content); 64 | } 65 | 66 | @Override 67 | public String[] getTabString() { 68 | return new String[]{ 69 | "1", 70 | "2", 71 | "3", 72 | }; 73 | } 74 | 75 | @Override 76 | public int[] getTabDrawables() { 77 | return null; 78 | } 79 | 80 | @Override 81 | public int getTabLayoutItem() { 82 | return 0; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /demo/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /demo/src/main/res/drawable/bg_toolbar.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /demo/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /demo/src/main/res/layout/at_load.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 11 | -------------------------------------------------------------------------------- /demo/src/main/res/layout/at_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 16 | 17 | 24 | 25 | 32 | 33 | 40 | 41 | 48 | -------------------------------------------------------------------------------- /demo/src/main/res/layout/at_template.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | -------------------------------------------------------------------------------- /demo/src/main/res/layout/at_test.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 17 | -------------------------------------------------------------------------------- /demo/src/main/res/layout/at_viewpage.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | 19 | 20 | 21 | 26 | -------------------------------------------------------------------------------- /demo/src/main/res/layout/empty_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 12 | -------------------------------------------------------------------------------- /demo/src/main/res/layout/fg_demo.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | 13 | 17 | 18 | 21 | 22 | 23 | 28 | 29 | 30 | 35 | 36 | -------------------------------------------------------------------------------- /demo/src/main/res/layout/fg_demo_child.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 10 | -------------------------------------------------------------------------------- /demo/src/main/res/layout/item_test.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | -------------------------------------------------------------------------------- /demo/src/main/res/layout/load_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jlanglang/JMVP/727468103ad1c5999c7682e0417cab19136c9c48/demo/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jlanglang/JMVP/727468103ad1c5999c7682e0417cab19136c9c48/demo/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jlanglang/JMVP/727468103ad1c5999c7682e0417cab19136c9c48/demo/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jlanglang/JMVP/727468103ad1c5999c7682e0417cab19136c9c48/demo/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jlanglang/JMVP/727468103ad1c5999c7682e0417cab19136c9c48/demo/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jlanglang/JMVP/727468103ad1c5999c7682e0417cab19136c9c48/demo/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jlanglang/JMVP/727468103ad1c5999c7682e0417cab19136c9c48/demo/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jlanglang/JMVP/727468103ad1c5999c7682e0417cab19136c9c48/demo/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jlanglang/JMVP/727468103ad1c5999c7682e0417cab19136c9c48/demo/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jlanglang/JMVP/727468103ad1c5999c7682e0417cab19136c9c48/demo/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /demo/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | -------------------------------------------------------------------------------- /demo/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | jmvpDemo 3 | 4 | -------------------------------------------------------------------------------- /demo/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /demo/src/test/java/com/baozi/demo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.baozi.demo; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | ## Project-wide Gradle settings. 2 | # 3 | # For more details on how to configure your build environment visit 4 | # http://www.gradle.org/docs/current/userguide/build_environment.html 5 | # 6 | # Specifies the JVM arguments used for the daemon process. 7 | # The setting is particularly useful for tweaking memory settings. 8 | # Default value: -Xmx1024m -XX:MaxPermSize=256m 9 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 10 | # 11 | # When configured, Gradle will run in incubating parallel mode. 12 | # This option should only be used with decoupled projects. More details, visit 13 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 14 | # org.gradle.parallel=true 15 | #Fri Jul 07 19:02:52 CST 2017 16 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jlanglang/JMVP/727468103ad1c5999c7682e0417cab19136c9c48/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Sep 23 22:06:45 CST 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':demo' 2 | --------------------------------------------------------------------------------