Skip to content

Commit

Permalink
修改注入P到V的方式,后期用AOP重构
Browse files Browse the repository at this point in the history
  • Loading branch information
Zane96 committed May 11, 2017
1 parent f46cd89 commit f568ca6
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 20 deletions.
1 change: 1 addition & 0 deletions demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ dependencies {

compile project(':easymvp')
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.example.zane.demo.Bean.RecycleviewData;
import com.example.zane.demo.Constant;
import com.example.zane.demo.view.MainListView;
import com.example.zane.easymvp.base.IPersenter;
import com.example.zane.easymvp.base.IView;
import com.example.zane.easymvp.presenter.BaseActivityPresenter;
import com.example.zane.easymvp.presenter.BaseListAdapterPresenter;
Expand Down Expand Up @@ -84,7 +85,7 @@ public void inDestory() {
}

@Override
public AppCompatActivity getContext() {
public IPersenter getPersenter() {
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.example.zane.demo.R;
import com.example.zane.demo.view.MainView2;
import com.example.zane.easymvp.base.IPersenter;
import com.example.zane.easymvp.presenter.BaseActivityPresenter;

/**
Expand Down Expand Up @@ -39,7 +40,7 @@ public void inDestory() {
}

@Override
public Activity getContext() {
public IPersenter getPersenter() {
return this;
}
}
13 changes: 11 additions & 2 deletions demo/src/main/java/com/example/zane/demo/view/MainListView.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import android.widget.Toast;

import com.example.zane.demo.R;
import com.example.zane.demo.presenter.MainActivity;
import com.example.zane.demo.presenter.MyRecycleviewAdapter;
import com.example.zane.easymvp.base.IPersenter;
import com.example.zane.easymvp.view.BaseViewImpl;

import butterknife.Bind;


/**
* Created by Zane on 15/12/20.
*/
Expand All @@ -26,9 +29,15 @@ public int getRootViewId() {
return R.layout.activity_main;
}

// @Override
// public void setActivityContext(Activity activity) {
// this.activity = activity;
// }


@Override
public void setActivityContext(Activity activity) {
this.activity = activity;
public void injectPresenter(IPersenter persenter) {
activity = (MainActivity) persenter;
}

@Override
Expand Down
8 changes: 6 additions & 2 deletions demo/src/main/java/com/example/zane/demo/view/MainView2.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
import android.widget.Toast;

import com.example.zane.demo.R;

import com.example.zane.demo.presenter.MainActivity2;
import com.example.zane.easymvp.base.IPersenter;
import com.example.zane.easymvp.view.BaseViewImpl;

import butterknife.Bind;


/**
* Created by Zane on 16/1/27.
*/
Expand All @@ -28,8 +32,8 @@ public int getRootViewId() {
}

@Override
public void setActivityContext(Activity activity) {
context = activity;
public void injectPresenter(IPersenter persenter) {
context = (Context) persenter;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion easymvp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ dependencies {
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.jakewharton:butterknife:7.0.1'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.zane.easymvp.base;

/**
* Created by Zane on 2017/5/11.
* Email: [email protected]
* Blog: zane96.github.io
*/

public interface IPersenter {
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ public interface IView{
void removeView();

/**
* 在presenter里面返回自己的context给view层
* 注入P
* @param persenter
*/
void setActivityContext(Activity activity);
void injectPresenter(IPersenter persenter);

/**
* 在presenter销毁的时候调用,生命周期同步一下,有时候需要在view释放什么
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.example.zane.easymvp.base.IPersenter;
import com.example.zane.easymvp.base.IView;


/**
* Created by Zane on 15/12/18.
*/
public abstract class BaseActivityPresenter<V extends IView> extends AppCompatActivity{
public abstract class BaseActivityPresenter<V extends IView> extends AppCompatActivity implements IPersenter{

protected V v;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("BaseActivityPresenter", "creat");

try {
v = getRootViewClass().newInstance();
} catch (InstantiationException e) {
Expand All @@ -29,7 +30,7 @@ protected void onCreate(Bundle savedInstanceState) {

v.creatView(getLayoutInflater(), null, savedInstanceState);
v.initView();
v.setActivityContext(getContext());
v.injectPresenter(getPersenter());

setContentView(v.getRootView());

Expand All @@ -48,6 +49,6 @@ protected void onDestroy() {
public abstract Class<V> getRootViewClass();
public abstract void inCreat(Bundle savedInstanceState);
public abstract void inDestory();
public abstract Activity getContext();
public abstract IPersenter getPersenter();

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
import android.view.View;
import android.view.ViewGroup;

import com.example.zane.easymvp.base.IPersenter;
import com.example.zane.easymvp.base.IView;

/**
* Created by Zane on 15/12/18.
*/
public abstract class BaseFragmentPresenter<T extends IView> extends Fragment{
public abstract class BaseFragmentPresenter<T extends IView> extends Fragment implements IPersenter{

protected T v;

Expand All @@ -29,7 +30,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
e.printStackTrace();
}
v.creatView(inflater, container, savedInstanceState);
v.setActivityContext((Activity) getContext());
v.injectPresenter(getPersenter());
return v.getRootView();
}

Expand All @@ -48,6 +49,5 @@ public void onDestroy() {
}

public abstract Class<T> getRootViewClass();
public abstract FragmentActivity getContext();

public abstract IPersenter getPersenter();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import android.view.View;
import android.view.ViewGroup;

import com.example.zane.easymvp.base.IPersenter;
import com.example.zane.easymvp.base.IView;

import butterknife.ButterKnife;



/**
* Created by Zane on 15/12/18.
* 将view加载的过程写在抽象类,做到代码复用。
Expand All @@ -22,6 +24,7 @@ public abstract class BaseViewImpl implements IView {
protected View view;
protected final SparseArray<View> mViews = new SparseArray<>();


public void creatView(LayoutInflater inflater, ViewGroup parent, Bundle bundle) {
int resourceId = getRootViewId();

Expand All @@ -47,7 +50,7 @@ public final void removeView() {
ButterKnife.unbind(this);
}

public abstract void setActivityContext(Activity activity);
public abstract void injectPresenter(IPersenter persenter);

private final <T extends View> T bindView(int id) {
T view2 = (T) mViews.get(id);
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Nov 09 15:31:30 CST 2016
#Tue May 09 13:07:06 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-all.zip

0 comments on commit f568ca6

Please sign in to comment.