Commit 7de2a21bfd0fbae3fafe054b0d4c0e1cb3fadf23

Authored by YangShuai
1 parent b22dc20a

优化基类

app/strike/src/main/AndroidManifest.xml
@@ -21,6 +21,7 @@ @@ -21,6 +21,7 @@
21 <category android:name="android.intent.category.LAUNCHER" /> 21 <category android:name="android.intent.category.LAUNCHER" />
22 </intent-filter> 22 </intent-filter>
23 </activity> 23 </activity>
  24 + <activity android:name=".MyActivityTest1" />
24 25
25 <meta-data 26 <meta-data
26 android:name="com.baidu.lbsapi.API_KEY" 27 android:name="com.baidu.lbsapi.API_KEY"
app/strike/src/main/java/com/cnlive/strike/MyActivityTest1.java 0 → 100644
  1 +package com.cnlive.strike;
  2 +
  3 +import com.cnlive.libs.base.frame.activity.BaseActivity;
  4 +
  5 +
  6 +public class MyActivityTest1 extends BaseActivity {
  7 +
  8 + @Override
  9 + protected int initLayout() {
  10 + return R.layout.activity_my;
  11 + }
  12 +
  13 + @Override
  14 + protected void initView() {
  15 + getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MyFragment1()).commitAllowingStateLoss();
  16 + }
  17 +
  18 + @Override
  19 + protected void initData() {
  20 + }
  21 +}
app/strike/src/main/java/com/cnlive/strike/MyFragment.java
1 package com.cnlive.strike; 1 package com.cnlive.strike;
2 2
3 3
  4 +import android.content.Intent;
4 import android.os.Bundle; 5 import android.os.Bundle;
5 import android.view.LayoutInflater; 6 import android.view.LayoutInflater;
6 import android.view.View; 7 import android.view.View;
@@ -14,21 +15,30 @@ import androidx.annotation.Nullable; @@ -14,21 +15,30 @@ import androidx.annotation.Nullable;
14 15
15 16
16 public class MyFragment extends BaseFragment<MyFragmentView, MyPresenter> { 17 public class MyFragment extends BaseFragment<MyFragmentView, MyPresenter> {
  18 + private View view;
  19 +
17 @Nullable 20 @Nullable
18 @Override 21 @Override
19 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 22 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
20 - return inflater.inflate(R.layout.activity_main1, container, false); 23 + view = inflater.inflate(R.layout.activity_main1, container, false);
  24 + return view;
21 } 25 }
22 26
23 @Override 27 @Override
24 protected void initView() { 28 protected void initView() {
25 setStatusBarColor(R.color.green, 0); 29 setStatusBarColor(R.color.green, 0);
26 setStatusBarTextColor(StatusBarConfig.BLACK); 30 setStatusBarTextColor(StatusBarConfig.BLACK);
  31 + view.findViewById(R.id.btn_test).setOnClickListener(new View.OnClickListener() {
  32 + @Override
  33 + public void onClick(View v) {
  34 + getActivity().startActivity(new Intent(getActivity(), MyActivityTest1.class));
  35 + }
  36 + });
27 } 37 }
28 38
29 @Override 39 @Override
30 protected void initData() { 40 protected void initData() {
31 -// getPresenter().setData("hha"); 41 + getPresenter().setData("hha");
32 getMvpView().setData("test"); 42 getMvpView().setData("test");
33 } 43 }
34 44
app/strike/src/main/java/com/cnlive/strike/MyFragment1.java 0 → 100644
  1 +package com.cnlive.strike;
  2 +
  3 +
  4 +import android.content.Intent;
  5 +import android.os.Bundle;
  6 +import android.view.LayoutInflater;
  7 +import android.view.View;
  8 +import android.view.ViewGroup;
  9 +
  10 +import androidx.annotation.NonNull;
  11 +import androidx.annotation.Nullable;
  12 +
  13 +import com.cnlive.libs.base.frame.fragment.BaseFragment;
  14 +import com.cnlive.libs.base.util.StatusBarConfig;
  15 +
  16 +
  17 +public class MyFragment1 extends BaseFragment<MyFragment1View, MyPresenter1> {
  18 + private View view;
  19 +
  20 + @Nullable
  21 + @Override
  22 + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  23 + view = inflater.inflate(R.layout.fragment_main1, container, false);
  24 + return view;
  25 + }
  26 +
  27 + @Override
  28 + protected void initView() {
  29 + setStatusBarColor(R.color.green, 0);
  30 + setStatusBarTextColor(StatusBarConfig.BLACK);
  31 +
  32 +
  33 + }
  34 +
  35 + @Override
  36 + protected void initData() {
  37 + getPresenter().setData1("dd");
  38 + }
  39 +
  40 +
  41 +}
app/strike/src/main/java/com/cnlive/strike/MyFragment1View.java 0 → 100644
  1 +package com.cnlive.strike;
  2 +
  3 +import android.util.Log;
  4 +
  5 +import com.cnlive.libs.base.frame.view.BaseView;
  6 +
  7 +public class MyFragment1View extends BaseView {
  8 +// private MyFragment fragment;
  9 +//
  10 +// /**
  11 +// * 可给可不给,看自己需要
  12 +// *
  13 +// * @param fragment
  14 +// */
  15 + public MyFragment1View(MyFragment1 fragment) {
  16 +// this.fragment = fragment;
  17 + Log.e(TAG, "MyFragment1View: " + fragment);
  18 + }
  19 +
  20 + public void setData1(String msg) {
  21 + Log.e(TAG, "setData1: ");
  22 + }
  23 +}
app/strike/src/main/java/com/cnlive/strike/MyPresenter1.java 0 → 100644
  1 +package com.cnlive.strike;
  2 +
  3 +import com.cnlive.libs.base.frame.presenter.BasePresenter;
  4 +
  5 +public class MyPresenter1 extends BasePresenter<MyFragment1View> {
  6 + public void setData1(String msg) {
  7 + getView().setData1(msg);
  8 + }
  9 +}
app/strike/src/main/res/layout/activity_main1.xml
@@ -15,5 +15,10 @@ @@ -15,5 +15,10 @@
15 android:layout_height="wrap_content" 15 android:layout_height="wrap_content"
16 android:text="fragment" /> 16 android:text="fragment" />
17 17
  18 + <Button
  19 + android:id="@+id/btn_test"
  20 + android:layout_width="wrap_content"
  21 + android:layout_height="wrap_content"
  22 + android:text="测试view" />
18 </RelativeLayout> 23 </RelativeLayout>
19 </layout> 24 </layout>
20 \ No newline at end of file 25 \ No newline at end of file
app/strike/src/main/res/layout/fragment_main1.xml 0 → 100644
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<layout>
  3 +
  4 + <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5 + xmlns:app="http://schemas.android.com/apk/res-auto"
  6 + xmlns:tools="http://schemas.android.com/tools"
  7 + android:layout_width="match_parent"
  8 + android:layout_height="match_parent"
  9 + android:orientation="vertical"
  10 + tools:context=".ui.activity.AllDeviceActivity">
  11 +
  12 + <TextView
  13 + android:id="@+id/tv1"
  14 + android:layout_width="wrap_content"
  15 + android:layout_height="wrap_content"
  16 + android:text="fragment" />
  17 +
  18 + <Button
  19 + android:id="@+id/btn_test"
  20 + android:layout_width="wrap_content"
  21 + android:layout_height="wrap_content"
  22 + android:text="测试view2" />
  23 + </RelativeLayout>
  24 +</layout>
0 \ No newline at end of file 25 \ No newline at end of file
core/base/src/main/java/com/cnlive/libs/base/frame/fragment/BaseFragment.java
@@ -37,30 +37,22 @@ public abstract class BaseFragment&lt;V extends BaseView, P extends BasePresenter&lt;V @@ -37,30 +37,22 @@ public abstract class BaseFragment&lt;V extends BaseView, P extends BasePresenter&lt;V
37 37
38 @NonNull 38 @NonNull
39 public V createView() { 39 public V createView() {
40 - if (null == view) {  
41 - view = ReflectUtil.instanceView(0, this.getClass(), this, BaseView.class);  
42 - }  
43 - return view; 40 + return ReflectUtil.instanceView(0, this.getClass(), this, BaseView.class);
44 } 41 }
45 42
46 @Override 43 @Override
47 public P createPresenter() { 44 public P createPresenter() {
48 - if (null == presenter) {  
49 - presenter = ReflectUtil.instancePresenter(1, this.getClass(), BasePresenter.class);  
50 - }  
51 - return presenter; 45 + return ReflectUtil.instancePresenter(1, this.getClass(), BasePresenter.class);
52 } 46 }
53 47
54 @NonNull 48 @NonNull
55 @Override 49 @Override
56 public V getMvpView() { 50 public V getMvpView() {
57 if (null == view) { 51 if (null == view) {
58 - return createView();  
59 - } else {  
60 - return view; 52 + view = createView();
61 } 53 }
  54 + return view;
62 } 55 }
63 -  
64 // protected abstract int getLayoutId(); 56 // protected abstract int getLayoutId();
65 57
66 protected abstract void initView(); 58 protected abstract void initView();
@@ -73,7 +65,8 @@ public abstract class BaseFragment&lt;V extends BaseView, P extends BasePresenter&lt;V @@ -73,7 +65,8 @@ public abstract class BaseFragment&lt;V extends BaseView, P extends BasePresenter&lt;V
73 * @param color 65 * @param color
74 * @param statusBarAlpha 66 * @param statusBarAlpha
75 */ 67 */
76 - protected void setStatusBarColor(@ColorRes int color, @IntRange(from = 0, to = 255) int statusBarAlpha) { 68 + protected void setStatusBarColor(@ColorRes int color,
  69 + @IntRange(from = 0, to = 255) int statusBarAlpha) {
77 StatusBarUtil.setColor(getActivity(), ContextCompat.getColor(getActivity(), color), statusBarAlpha); 70 StatusBarUtil.setColor(getActivity(), ContextCompat.getColor(getActivity(), color), statusBarAlpha);
78 } 71 }
79 72