Commit de2d04c92837b467f3bda592074e70729ddd1732
fragment基类优化
Showing
10 changed files
with
147 additions
and
15 deletions
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
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
| 1 | package com.cnlive.libs.base.frame.fragment; | 1 | package com.cnlive.libs.base.frame.fragment; |
| 2 | 2 | ||
| 3 | import android.os.Bundle; | 3 | import android.os.Bundle; |
| 4 | -import android.util.Log; | ||
| 5 | -import android.view.LayoutInflater; | ||
| 6 | import android.view.View; | 4 | import android.view.View; |
| 7 | -import android.view.ViewGroup; | ||
| 8 | 5 | ||
| 9 | import androidx.annotation.ColorRes; | 6 | import androidx.annotation.ColorRes; |
| 10 | import androidx.annotation.IntRange; | 7 | import androidx.annotation.IntRange; |
| @@ -17,18 +14,17 @@ import com.cnlive.libs.base.frame.view.BaseView; | @@ -17,18 +14,17 @@ import com.cnlive.libs.base.frame.view.BaseView; | ||
| 17 | import com.cnlive.libs.base.util.ReflectUtil; | 14 | import com.cnlive.libs.base.util.ReflectUtil; |
| 18 | import com.cnlive.libs.base.util.StatusBarConfig; | 15 | import com.cnlive.libs.base.util.StatusBarConfig; |
| 19 | import com.cnlive.libs.base.util.StatusBarUtil; | 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 | @Override | 29 | @Override |
| 34 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | 30 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { |
| @@ -51,6 +47,7 @@ public abstract class BaseFragment<V extends MvpView, P extends MvpBasePresenter | @@ -51,6 +47,7 @@ public abstract class BaseFragment<V extends MvpView, P extends MvpBasePresenter | ||
| 51 | return ReflectUtil.instanceView(0, this.getClass(), this, BaseView.class); | 47 | return ReflectUtil.instanceView(0, this.getClass(), this, BaseView.class); |
| 52 | } | 48 | } |
| 53 | 49 | ||
| 50 | +// protected abstract int getLayoutId(); | ||
| 54 | 51 | ||
| 55 | protected abstract void initView(); | 52 | protected abstract void initView(); |
| 56 | 53 | ||
| @@ -62,7 +59,8 @@ public abstract class BaseFragment<V extends MvpView, P extends MvpBasePresenter | @@ -62,7 +59,8 @@ public abstract class BaseFragment<V extends MvpView, P extends MvpBasePresenter | ||
| 62 | * @param color | 59 | * @param color |
| 63 | * @param statusBarAlpha | 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 | StatusBarUtil.setColor(getActivity(), ContextCompat.getColor(getActivity(), color), statusBarAlpha); | 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,5 +3,5 @@ package com.cnlive.libs.base.frame.presenter; | ||
| 3 | import com.cnlive.libs.base.frame.view.BaseView; | 3 | import com.cnlive.libs.base.frame.view.BaseView; |
| 4 | import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter; | 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 | } |