Commit f40dc0f944ba3244897600984dbf8df47fc7d243

Authored by YangShuai
1 parent 83168fea

修改基类

app/strike/src/main/java/com/cnlive/strike/MyActivity.java
... ... @@ -4,13 +4,22 @@ import androidx.appcompat.app.AppCompatActivity;
4 4  
5 5 import android.os.Bundle;
6 6  
  7 +import com.cnlive.libs.base.frame.activity.BaseActivity;
7 8  
8   -public class MyActivity extends AppCompatActivity {
  9 +
  10 +public class MyActivity extends BaseActivity {
  11 +
  12 + @Override
  13 + protected int initLayout() {
  14 + return R.layout.activity_my;
  15 + }
  16 +
  17 + @Override
  18 + protected void initView() {
  19 + getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MyFragment()).commitAllowingStateLoss();
  20 + }
9 21  
10 22 @Override
11   - protected void onCreate(Bundle savedInstanceState) {
12   - super.onCreate(savedInstanceState);
13   - setContentView(R.layout.activity_my);
14   - getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new MyFragment()).commitAllowingStateLoss();
  23 + protected void initData() {
15 24 }
16 25 }
... ...
app/strike/src/main/java/com/cnlive/strike/MyFragment.java
... ... @@ -2,28 +2,33 @@ package com.cnlive.strike;
2 2  
3 3  
4 4 import android.os.Bundle;
  5 +import android.view.LayoutInflater;
5 6 import android.view.View;
  7 +import android.view.ViewGroup;
6 8 import android.widget.TextView;
7 9  
8 10 import com.cnlive.libs.base.frame.fragment.BaseFragment;
9 11  
  12 +import androidx.annotation.NonNull;
10 13 import androidx.annotation.Nullable;
11 14  
12 15  
13   -public class MyFragment extends BaseFragment <MyFragmentView,MyPresenter>{
14   -
  16 +public class MyFragment extends BaseFragment<MyFragmentView, MyPresenter> {
  17 + @Nullable
  18 + @Override
  19 + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  20 + return inflater.inflate(R.layout.activity_main1, container, false);
  21 + }
15 22  
16   - public TextView tx;
17 23 @Override
18   - protected int getLayoutId() {
19   - return R.layout.activity_main1;
  24 + protected void initView() {
  25 +
20 26 }
21 27  
22 28 @Override
23   - public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
24   - super.onViewCreated(view, savedInstanceState);
25   - tx = view.findViewById(R.id.tv1);
26   - getPresenter().setData("hha");
27   -// getMvpView().setData("sasa");
  29 + protected void initData() {
  30 +// getPresenter().setData("hha");
  31 + getMvpView().setData("test");
28 32 }
  33 +
29 34 }
... ...
app/strike/src/main/java/com/cnlive/strike/MyFragmentView.java
1 1 package com.cnlive.strike;
2 2  
3   -import com.hannesdorfmann.mosby3.mvp.MvpView;
  3 +import android.util.Log;
4 4  
5   -public class MyFragmentView implements MvpView {
6   - MyFragment fragment;
  5 +import com.cnlive.libs.base.frame.view.BaseView;
  6 +import com.hannesdorfmann.mosby3.mvp.MvpView;
7 7  
8   - public MyFragmentView(MyFragment fragment) {
9   - this.fragment = fragment;
10   - }
  8 +public class MyFragmentView extends BaseView {
  9 +// private MyFragment fragment;
  10 +//
  11 +// /**
  12 +// * 可给可不给,看自己需要
  13 +// *
  14 +// * @param fragment
  15 +// */
  16 +// public MyFragmentView(MyFragment fragment) {
  17 +// this.fragment = fragment;
  18 +// Log.e(TAG, "MyFragmentView: " + fragment);
  19 +// }
11 20  
12   - public void setData(String msg){
13   - fragment.tx.setText(msg);
  21 + public void setData(String msg) {
  22 + Log.e(TAG, "setData: ");
14 23 }
15 24 }
... ...
app/strike/src/main/java/com/cnlive/strike/MyPresenter.java
1 1 package com.cnlive.strike;
2 2  
  3 +import com.cnlive.libs.base.frame.presenter.BasePresenter;
3 4 import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter;
4 5  
5   -public class MyPresenter extends MvpBasePresenter<MyFragmentView> {
  6 +public class MyPresenter extends BasePresenter<MyFragmentView> {
6 7 public void setData(String msg) {
7   - getView().setData(msg);
  8 +// getView().setData(msg);
8 9 }
9 10 }
... ...
core/base/src/main/java/com/cnlive/libs/base/frame/activity/BaseActivity.java 0 → 100644
  1 +package com.cnlive.libs.base.frame.activity;
  2 +
  3 +
  4 +import android.annotation.SuppressLint;
  5 +import android.content.Context;
  6 +import android.content.pm.ActivityInfo;
  7 +import android.database.DatabaseUtils;
  8 +import android.os.Bundle;
  9 +import android.os.Looper;
  10 +import android.view.View;
  11 +import android.view.Window;
  12 +import android.view.WindowManager;
  13 +import android.view.inputmethod.InputMethodManager;
  14 +import android.widget.Toast;
  15 +
  16 +import androidx.annotation.Nullable;
  17 +import androidx.appcompat.app.AppCompatActivity;
  18 +import androidx.databinding.DataBindingUtil;
  19 +import androidx.databinding.ViewDataBinding;
  20 +
  21 +import com.cnlive.libs.base.util.lib_permisshelper.PermissionsUtils;
  22 +import com.cnlive.libs.base.util.ActivityCollector;
  23 +
  24 +/**
  25 + * @author ys
  26 + * 基类 activity
  27 + */
  28 +public abstract class BaseActivity extends AppCompatActivity {
  29 + //获取TAG的activity名称
  30 + protected final String TAG = this.getClass().getSimpleName();
  31 + //是否显示标题栏
  32 + private boolean isShowTitle = true;
  33 + //是否显示状态栏
  34 + private boolean isShowStatusBar = true;
  35 + //是否允许旋转屏幕
  36 + private boolean isAllowScreenRoate = true;
  37 + //封装Toast对象
  38 + private static Toast toast;
  39 + public Context context;
  40 + private PermissionsUtils mPermissionsUtils;//权限申请
  41 + private int stateBarColor = -1;
  42 + public ViewDataBinding binding;
  43 +
  44 + @Override
  45 + protected void onCreate(@Nullable Bundle savedInstanceState) {
  46 + super.onCreate(savedInstanceState);
  47 + context = this;
  48 + //activity管理
  49 + ActivityCollector.addActivity(this);
  50 + if (!isShowTitle) {
  51 + requestWindowFeature(Window.FEATURE_NO_TITLE);
  52 + }
  53 +
  54 + if (!isShowStatusBar) {
  55 + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
  56 + , WindowManager.LayoutParams.FLAG_FULLSCREEN);
  57 + }
  58 + //设置布局
  59 + binding = DataBindingUtil.setContentView(this, initLayout());
  60 + //设置屏幕是否可旋转
  61 + if (!isAllowScreenRoate) {
  62 + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  63 + } else {
  64 + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  65 + }
  66 + mPermissionsUtils = new PermissionsUtils();//权限申请
  67 + //初始化控件
  68 + initView();
  69 + //设置数据
  70 + initData();
  71 + }
  72 +
  73 + /**
  74 + * 初始化布局
  75 + *
  76 + * @return 布局id
  77 + */
  78 + protected abstract int initLayout();
  79 +
  80 + /**
  81 + * 初始化控件
  82 + */
  83 + protected abstract void initView();
  84 +
  85 + /**
  86 + * 设置数据
  87 + */
  88 + protected abstract void initData();
  89 +
  90 + /**
  91 + * 设置是否显示标题栏
  92 + *
  93 + * @param showTitle true or false
  94 + */
  95 + public void setShowTitle(boolean showTitle) {
  96 + isShowTitle = showTitle;
  97 + }
  98 +
  99 + /**
  100 + * 设置是否显示状态栏
  101 + *
  102 + * @param showStatusBar true or false
  103 + */
  104 + public void setShowStatusBar(boolean showStatusBar) {
  105 + isShowStatusBar = showStatusBar;
  106 + }
  107 +
  108 + /**
  109 + * 是否允许屏幕旋转
  110 + *
  111 + * @param allowScreenRoate true or false
  112 + */
  113 + public void setAllowScreenRoate(boolean allowScreenRoate) {
  114 + isAllowScreenRoate = allowScreenRoate;
  115 + }
  116 +
  117 + /**
  118 + * 保证同一按钮在1秒内只会响应一次点击事件
  119 + */
  120 + public abstract class OnSingleClickListener implements View.OnClickListener {
  121 + //两次点击按钮之间的间隔,目前为1000ms
  122 + private static final int MIN_CLICK_DELAY_TIME = 1000;
  123 + private long lastClickTime;
  124 +
  125 + public abstract void onSingleClick(View view);
  126 +
  127 + @Override
  128 + public void onClick(View view) {
  129 + long curClickTime = System.currentTimeMillis();
  130 + if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
  131 + lastClickTime = curClickTime;
  132 + onSingleClick(view);
  133 + }
  134 + }
  135 + }
  136 +
  137 + /**
  138 + * 同一按钮在短时间内可重复响应点击事件
  139 + */
  140 + public abstract class OnMultiClickListener implements View.OnClickListener {
  141 + public abstract void onMultiClick(View view);
  142 +
  143 + @Override
  144 + public void onClick(View v) {
  145 + onMultiClick(v);
  146 + }
  147 + }
  148 +
  149 + /**
  150 + * 显示提示 toast
  151 + *
  152 + * @param msg 提示信息
  153 + */
  154 + @SuppressLint("ShowToast")
  155 + public void showToast(String msg) {
  156 + try {
  157 + if (null == toast) {
  158 + toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
  159 + } else {
  160 + toast.setText(msg);
  161 + }
  162 + runOnUiThread(new Runnable() {
  163 + @Override
  164 + public void run() {
  165 + toast.show();
  166 + }
  167 + });
  168 + } catch (Exception e) {
  169 + e.printStackTrace();
  170 + //解决在子线程中调用Toast的异常情况处理
  171 + Looper.prepare();
  172 + Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
  173 + Looper.loop();
  174 + }
  175 + }
  176 +
  177 +
  178 + /**
  179 + * 隐藏软键盘
  180 + */
  181 + public void hideSoftInput() {
  182 + InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
  183 + if (getCurrentFocus() != null && null != imm) {
  184 + imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
  185 + }
  186 + }
  187 +
  188 + /**
  189 + * 显示软键盘
  190 + */
  191 + public void showSoftInput() {
  192 + InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
  193 + if (getCurrentFocus() != null && null != imm) {
  194 + imm.showSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0);
  195 + }
  196 + }
  197 +
  198 + /**
  199 + * 显示软键盘
  200 + */
  201 + public void showSoftInput(View view) {
  202 + // InputMethodManager.SHOW_FORCED);
  203 + InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
  204 + imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
  205 + }
  206 +
  207 + @Override
  208 + protected void onDestroy() {
  209 + super.onDestroy();
  210 + //activity管理
  211 + ActivityCollector.removeActivity(this);
  212 + }
  213 +}
0 214 \ No newline at end of file
... ...
core/base/src/main/java/com/cnlive/libs/base/frame/fragment/BaseFragment.java
... ... @@ -16,26 +16,22 @@ import com.hannesdorfmann.mosby3.mvp.MvpPresenter;
16 16 import com.hannesdorfmann.mosby3.mvp.MvpView;
17 17  
18 18 /**
19   - * MVP Databinding Base Fragment
  19 + * 基类fragment
20 20 *
21   - * @param <V> MVP View {@link MvpView}
22   - * @param <P> MVP Presenter {@link MvpPresenter}
  21 + * @param <V>
  22 + * @param <P>
  23 + * @author ys
23 24 */
24   -public abstract class BaseFragment<V extends MvpView, P extends MvpBasePresenter<V>> extends com.hannesdorfmann.mosby3.mvp.MvpFragment<V, P> {
25   -
26   - public View view;
  25 +public abstract class BaseFragment<V extends BaseView, P extends BasePresenter<V>> extends com.hannesdorfmann.mosby3.mvp.MvpFragment<V, P> {
27 26 public V views;
28 27  
29   - @Nullable
30   - @Override
31   - public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
32   - return view = inflater.inflate(getLayoutId(), container, false);
33   - }
34   -
35 28 @Override
36 29 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
37 30 super.onViewCreated(view, savedInstanceState);
38   - getPresenter();
  31 + if (null != getMvpView() && null != getPresenter()) {
  32 + initView();
  33 + initData();
  34 + }
39 35 }
40 36  
41 37 @NonNull
... ... @@ -58,5 +54,9 @@ public abstract class BaseFragment&lt;V extends MvpView, P extends MvpBasePresenter
58 54 }
59 55 }
60 56  
61   - protected abstract int getLayoutId();
62   -}
63 57 \ No newline at end of file
  58 +// protected abstract int getLayoutId();
  59 +
  60 + protected abstract void initView();
  61 +
  62 + protected abstract void initData();
  63 +}
... ...
core/base/src/main/java/com/cnlive/libs/base/frame/presenter/BasePresenter.java
... ... @@ -4,5 +4,13 @@ package com.cnlive.libs.base.frame.presenter;
4 4 import com.cnlive.libs.base.frame.view.BaseView;
5 5 import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter;
6 6  
7   -public class BasePresenter extends MvpBasePresenter<BaseView> {
  7 +/**
  8 + * 基类Presnter
  9 + *
  10 + * @param <V>
  11 + * @author ys
  12 + */
  13 +public class BasePresenter<V extends BaseView> extends MvpBasePresenter<V> {
  14 + protected final String TAG = this.getClass().getSimpleName();
  15 +
8 16 }
... ...
core/base/src/main/java/com/cnlive/libs/base/frame/view/BaseView.java
... ... @@ -2,6 +2,11 @@ package com.cnlive.libs.base.frame.view;
2 2  
3 3 import com.hannesdorfmann.mosby3.mvp.MvpView;
4 4  
  5 +/**
  6 + * @author ys
  7 + * 基类View
  8 + */
5 9 public class BaseView implements MvpView {
  10 + protected final String TAG = this.getClass().getSimpleName();
6 11  
7 12 }
... ...
core/base/src/main/java/com/cnlive/libs/base/util/ReflectUtil.java
... ... @@ -78,6 +78,7 @@ public class ReflectUtil {
78 78  
79 79  
80 80 /**
  81 + * ys
81 82 * 创建view的实例
82 83 *
83 84 * @param position
... ... @@ -95,7 +96,7 @@ public class ReflectUtil {
95 96 clazz = (Class<R>) pt.getActualTypeArguments()[position];
96 97 } catch (Exception e) {
97 98 //直接使用基类
98   - clazz=baseView;
  99 + clazz = baseView;
99 100 }
100 101 Constructor constructor = null;
101 102 try {
... ... @@ -121,6 +122,16 @@ public class ReflectUtil {
121 122 return result;
122 123 }
123 124  
  125 + /**
  126 + * 初始化presenter
  127 + * ys
  128 + *
  129 + * @param position
  130 + * @param type
  131 + * @param basePresenter
  132 + * @param <R>
  133 + * @return
  134 + */
124 135 public static <R> R instancePresenter(int position, Class type, Class basePresenter) {
125 136 R result = null;
126 137 try {
... ... @@ -128,6 +139,7 @@ public class ReflectUtil {
128 139 Class<R> clazz = (Class<R>) pt.getActualTypeArguments()[position];
129 140 result = clazz.newInstance();
130 141 } catch (Exception ignored) {
  142 + //找不到就给基类
131 143 Class<R> clazz = basePresenter;
132 144 try {
133 145 result = clazz.newInstance();
... ...