Commit de2d04c92837b467f3bda592074e70729ddd1732

Authored by 韩亚云
2 parents 3050303d 7de2a21b

fragment基类优化

app/strike/src/main/AndroidManifest.xml
... ... @@ -21,6 +21,7 @@
21 21 <category android:name="android.intent.category.LAUNCHER" />
22 22 </intent-filter>
23 23 </activity>
  24 + <activity android:name=".MyActivityTest1" />
24 25  
25 26 <meta-data
26 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 1 package com.cnlive.strike;
2 2  
3 3  
  4 +import android.content.Intent;
4 5 import android.os.Bundle;
5 6 import android.view.LayoutInflater;
6 7 import android.view.View;
... ... @@ -14,21 +15,30 @@ import androidx.annotation.Nullable;
14 15  
15 16  
16 17 public class MyFragment extends BaseFragment<MyFragmentView, MyPresenter> {
  18 + private View view;
  19 +
17 20 @Nullable
18 21 @Override
19 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 27 @Override
24 28 protected void initView() {
25 29 setStatusBarColor(R.color.green, 0);
26 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 39 @Override
30 40 protected void initData() {
31   -// getPresenter().setData("hha");
  41 + getPresenter().setData("hha");
32 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 15 android:layout_height="wrap_content"
16 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 23 </RelativeLayout>
19 24 </layout>
20 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 25 \ No newline at end of file
... ...
core/base/src/main/java/com/cnlive/libs/base/frame/fragment/BaseFragment.java
1 1 package com.cnlive.libs.base.frame.fragment;
2 2  
3 3 import android.os.Bundle;
4   -import android.util.Log;
5   -import android.view.LayoutInflater;
6 4 import android.view.View;
7   -import android.view.ViewGroup;
8 5  
9 6 import androidx.annotation.ColorRes;
10 7 import androidx.annotation.IntRange;
... ... @@ -17,18 +14,17 @@ import com.cnlive.libs.base.frame.view.BaseView;
17 14 import com.cnlive.libs.base.util.ReflectUtil;
18 15 import com.cnlive.libs.base.util.StatusBarConfig;
19 16 import com.cnlive.libs.base.util.StatusBarUtil;
20   -import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter;
21   -import com.hannesdorfmann.mosby3.mvp.MvpPresenter;
22   -import com.hannesdorfmann.mosby3.mvp.MvpView;
23 17  
24 18 /**
25   - * MVP Databinding Base Fragment
  19 + * 基类fragment
26 20 *
27   - * @param <V> MVP View {@link MvpView}
28   - * @param <P> MVP Presenter {@link MvpPresenter}
  21 + * @param <V>
  22 + * @param <P>
  23 + * @author ys
29 24 */
30   -public abstract class BaseFragment<V extends MvpView, P extends MvpBasePresenter<V>> extends com.hannesdorfmann.mosby3.mvp.MvpFragment<V, P> {
31   -
  25 +public abstract class BaseFragment<V extends BaseView, P extends BasePresenter<V>> extends com.hannesdorfmann.mosby3.mvp.MvpFragment<V, P> {
  26 + public V view;
  27 + public P presenter;
32 28  
33 29 @Override
34 30 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
... ... @@ -51,6 +47,7 @@ public abstract class BaseFragment&lt;V extends MvpView, P extends MvpBasePresenter
51 47 return ReflectUtil.instanceView(0, this.getClass(), this, BaseView.class);
52 48 }
53 49  
  50 +// protected abstract int getLayoutId();
54 51  
55 52 protected abstract void initView();
56 53  
... ... @@ -62,7 +59,8 @@ public abstract class BaseFragment&lt;V extends MvpView, P extends MvpBasePresenter
62 59 * @param color
63 60 * @param statusBarAlpha
64 61 */
65   - protected void setStatusBarColor(@ColorRes int color, @IntRange(from = 0, to = 255) int statusBarAlpha) {
  62 + protected void setStatusBarColor(@ColorRes int color,
  63 + @IntRange(from = 0, to = 255) int statusBarAlpha) {
66 64 StatusBarUtil.setColor(getActivity(), ContextCompat.getColor(getActivity(), color), statusBarAlpha);
67 65 }
68 66  
... ...
core/base/src/main/java/com/cnlive/libs/base/frame/presenter/BasePresenter.java
... ... @@ -3,5 +3,5 @@ package com.cnlive.libs.base.frame.presenter;
3 3 import com.cnlive.libs.base.frame.view.BaseView;
4 4 import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter;
5 5  
6   -public class BasePresenter<M extends BaseView> extends MvpBasePresenter<BaseView> {
  6 +public class BasePresenter<V extends BaseView> extends MvpBasePresenter<V> {
7 7 }
... ...