Write it in front.
I remember a fat-rich "thinking in the book" where he mentioned how we should learn in this age of fragmentation and anxiety--30% of the time to learn about 70% of the knowledge in this field and then quickly shift the grass to a tasty place like a nomadic nation. That's not true, but what I'm trying to say is that since I was trying to write some notes, I had to refer to some of GitHub's more famous Daniel codes in order not to mislead others. I've downloaded a lot of demos in the process. The 1.1-point bite of the extract is of course fraught with doubts. Later I thought the title was a little too big for me to take the title of "Learning" class. But on the other hand I can only record my current understanding of the level of this stage is called "Shallow chat." Maybe you saw my demo on GitHub saying this TM is really crap. I believe that when I accidentally search my demo on GitHub, I would say, "This is the code that was written by the idiot ..." Later, I wrote it myself. It's unfortunate if I don't feel that way.
Mvp
In order to learn the MVP, I used my spare time to build a practiced hand app to emulate one. Dagger2+rxjava+retrofit+databinding. Details are still to be perfected.
In Android Development-a brief introduction to architecture (i) I then referred to the MVP fragment switch presenter. But after one weeks of study I quickly overturned the previous writing. Here I think it is necessary to say that I have been studying the MVP model written by other people for a while. It's recommended to treat the view layer as a presenter (a foreign buddy on GitHub doesn't have a record here) and then refer to Google Todoapp Mvp+rxjava and some of the domestic Daniel blogs to correct the code on GitHub in a timely fashion. Imitation One
The following post-specific modules welcome criticism and discussion.
Switch fragment in Homeactivity:
/* tab标签页面切换*/
public class HomeActivity extends BaseActivity<HomeActBinding> implements MainTabContract.View {
/* bind layout file @return/
@Override
protected int Getlayoutid () {
return r.layout.home_act;
}
/* Create presenter*/
@Override
protected void Setupview () {
New Maintabpresenter (This,this,mbinding.radio);
}
/* View Layer interface /
@NonNull
Private Maintabcontract.presenter Maintabpresenter;
@Override
public void Setpresenter (Maintabcontract.presenter Presenter) {
Maintabpresenter = Checknotnull (presenter);
}
@Override
protected void Onresume () {
Super.onresume ();
Maintabpresenter.subscribe ();
}
@Override
protected void OnPause () {
Super.onpause ();
Maintabpresenter.unsubscribe ();
}
@Override
protected void OnDestroy () {
Super.ondestroy ();
Maintabpresenter.unsubscribe ();
}
/* return processing */
Private long beforetime = 0L;
@Override
public void onbackpressed () {//Thanks.==> by Johntsai (Mailto:[email protected]) on 16/7/27.
if (System.currenttimemillis ()-beforetime<1000l) {finish (); super.onbackpressed ();} else{beforetime = System.currenttimemillis (); Toast.maketext (This, "press again to exit one", Toast.length_short). Show (); } } }
Maintabpresenter
public class MainTabPresenter implements MainTabContract.Presenter {
@NonNull
Private final Radiogroup Radiogroup;
@NonNull
Private final Maintabcontract.view Mmaintabcontractview;
@NonNull
Private final fragmentactivity Mcontext;
@NonNull
Private Compositesubscription msubscriptions;
Public Maintabpresenter (@NonNull fragmentactivity mactivity, @NonNull maintabcontract.view View, @NonNull radiogroup Group) {//
Mcontext = Checknotnull (mactivity, "fragmentactivity cannot be null!");
Mmaintabcontractview = Checknotnull (view);
Radiogroup = Checknotnull (Group, "Radiogroup cannot be null!");
Msubscriptions = new Compositesubscription ();
Mmaintabcontractview.setpresenter (this);
}
private void SwitchTo () {
Fragmentutils fragmentutil = new Fragmentutils (Mcontext, R.id.view_container);
Subscription Subscription = rxradiogroup.checkedchanges (radiogroup). Subscribe (Integer, {
Fragmentutil.switchto (SwitchTo (integer));
});
Msubscriptions.add (subscription);
}
Private Class SwitchTo (@IdRes Integer i) {
Integer integer = checknotnull (i, "switchTo idres cannot be null");
Switch (integer) {
Case R.id.home_one:
return onefragment.class;
Case r.id.home_reading:
return readingfragment.class;
Case R.id.home_music:
return musicfragment.class;
Case R.id.home_movie:
return moviefragment.class;
Default
Break
}
return null;
}
@Override
public void Subscribe () {
SwitchTo ();
}
@Override
public void unsubscribe () {
Msubscriptions.clear ();
}
}
Request home data in Onefragment
/* 一个 首页/
public class OneFragment extends BaseFragment<MainFragmentOneBinding> implements ViewPagerContract.View{
@NonNull
Private Viewpagercontract.presenter Mpresenter;
@Override
protected int Getlayoutid () {
return r.layout.main_fragment_one;
}
@Override
protected void Setupview () {
New Viewpagerpresenter (Getactivity (), this);
}
@Override
public void Setpageradapter (@NonNull onefragmentadapter onefragmentadapter) {
Inflate.viewPager.setAdapter (Checknotnull (onefragmentadapter));
}
@Override
public void Onresume () {
Super.onresume ();
Mpresenter.subscribe ();
}
@Override
public void OnPause () {
Super.onpause ();
Mpresenter.unsubscribe ();
}
@Override
public void Setpresenter (Viewpagercontract.presenter Presenter) {
Mpresenter = Checknotnull (presenter);
}
}
Viewpagerpresenter
public class ViewPagerPresenter implements ViewPagerContract.Presenter {
@NonNull
Private final compositesubscription msubscriptions;
@NonNull
Private final Viewpagercontract.view Mviewpagercontractview;
@NonNull
Private final Apiinteractor API;
@Nullable
Private final fragmentactivity mactivity;
Public Viewpagerpresenter (@NonNull fragmentactivity context, @NonNull Viewpagercontract.view MView) {
Mactivity = checknotnull (context);
API = Checknotnull (App.getappcomponent (). Getapiinteractor ());
Mviewpagercontractview = Checknotnull (MView);
Msubscriptions = new Compositesubscription ();
Mviewpagercontractview.setpresenter (this);
}
@Override
public void Subscribe () {
Subscription apionehome = Api.getonehome (Constantapi.onefragmentapi, New basesubscribe<string> () {
@Override
public void onsuccess (String result) {
LOG.D ("Home Interface", "onsuccess:" + result);
if (result!=null) {
onefragmententity entity = new Gson (). Fromjson (result, onefragmententity.class);
Mviewpagercontractview.setpageradapter (New Onefragmentadapter (Mactivity,entity.getdata ()));
}
}
});
Msubscriptions.add (Apionehome);
}
@Override
public void unsubscribe () {
Msubscriptions.clear ();
}
}
GitHub Specific Code
At last
Some people here say that the interface request data should not be written in presenter ... In short, the good architecture must be: 1 for your project 2 more write Notes document 3 business logic subcontracting 4 keep learning.
Welcome Dabigatran:
QQ Group 521039620
Original Blog Address
Android Development-A brief introduction to Architecture (II.)