Home>

This article contains:

Custom baseactivity

Custom basefragment

Custom baseapplication

Foreword

Beginners will definitely encounter a daily task,Then it is findviewbyid, setonclicklistener (let's call them daily tasks), and many people will mix them upLead to chaotic project structure,The main thing is that writing too much will be annoying.Don't you think?When the project has more activities, it must be rewritten each time a control is added.Think of it all tired

tv_cz_10=(textview) findviewbyid (r.id.tv_cz_10);
tv_cz_20=(textview) findviewbyid (r.id.tv_cz_20);
tv_cz_30=(textview) findviewbyid (r.id.tv_cz_30);
tv_cz_50=(textview) findviewbyid (r.id.tv_cz_50);
tv_cz_10.setonclicklistener (this);
tv_cz_20.setonclicklistener (this);
tv_cz_30.setonclicklistener (this);
tv_cz_50.setonclicklistener (this);

Customized problems:Try to write as little code as possible.Do more

Purpose of customization:Clear the code structure.Make your programming more logical

Customized content:Everything is implemented according to the needs of the project

Custom baseactivity

We simply customize our baseactivity for daily tasks

public abstract class baseactivity extends fragmentactivity implements view.onclicklistener {
 private sparsearray<view>mviews;
 public abstract int getlayoutid ();
 public abstract void initviews ();
 public abstract void initlistener ();
 public abstract void initdata ();
 public abstract void processclick (view v);
 public void onclick (view v) {
 processclick (v);
 }
 @override
 protected void oncreate (bundle savedinstancestate) {
 super.oncreate (savedinstancestate);
 mviews=new sparsearray<>();
 setcontentview (getlayoutid ());
 initviews ();
 initlistener ();
 initdata ();
 }
 /**
 * Find view by id
 * /
 public<e extends view>e findview (int viewid) {
 e view=(e) mviews.get (viewid);
 if (view == null) {
 view=(e) findviewbyid (viewid);
 mviews.put (viewid, view);
 }
 return view;
 }
 /**
 * view set onclick event
 * /
 public<e extends view>void setonclick (e view) {
 view.setonclicklistener (this);
 }
}

The code is actually very simple,From the code alone may not know the meaning of this code,Then let's understand its true function by implementing this code,Here is the code that implements the baseactivity

public class searchactivity extends baseactivity {
 bannercontroller bannercontroller;
 shopcontroller shopcontroller;
 private imageview iv_zxing;
 private textview tv_sure;
 @override
 public int getlayoutid () {
 //Here is used to get the layout of the activity
 return r.layout.activity_search;
 }
 @override
 public void initviews () {
 //This is used to initialize the view
 iv_zxing=findview (r.id.iv_zxing);
 tv_sure=findview (r.id.tv_sure);
 }
 @override
 public void initlistener () {
 //This is used to initialize the click event
 setonclick (iv_zxing);
 setonclick (tv_sure);
 }
 @override
 public void initdata () {
 //This is used to set data, get data, read network data, everything done here can be implemented in the controller
 bannercontroller=new bannercontroller (getactivity ());
 shopcontroller=new shopcontroller (getactivity ());
 initshop ();
 initbanner ();
 }
 @override
 public void processclick (view v) {
 //Here is used to handle the click event
 switch (v.getid ()) {
 case r.id.iv_zxing:
 break;
 case r.id.tv_sure:
 break;
 }
 }
 private void initshop () {
 }
 private void initbanner () {
 }
}

Do you think the code structure is clear?And compared to the previous daily tasks,The code is indeed a lot less,Each method puts what it should do,This will ensure that your logic will not go wrong when programming,It ’s easy for others to read,Of course, in addition to the commonly used setonclicklistener and setonitemclicklistener, this needs to be customized according to the needs of the project

If you are a cool and very personality person,Then you can also try the following usage,Use one letter as a method,Everything depends on your mood

public<e extends view>e f (int viewid) {
 e view=(e) mviews.get (viewid);
 if (view == null) {
 view=(e) findviewbyid (viewid);
 mviews.put (viewid, view);
 }
 return view;
}
public<e extends view>void c (e view) {
 view.setonclicklistener (this);
}
//It's also handsome to use
@override
public void initviews () {
 iv_zxing=f (r.id.iv_zxing);
 tv_sure=f (r.id.tv_sure);
}
@override
public void initlistener () {
 c (iv_zxing);
 c (tv_sure);
}

Custom basefragment

After introducing the activity, the fragment is very simple,You can imitate the activity implementation, if it is the same as the above, then there is no funFor personal project reasons,I set the fragment to lazy loading mode by default,And only load the data once

public abstract class basefragment extends fragment implements view.onclicklistener {
 private boolean isvisible=false;
 private boolean isinitview=false;
 private boolean isfirstload=true;
 public view convertview;
 private sparsearray<view>mviews;
 public abstract int getlayoutid ();
 public abstract void initviews ();
 public abstract void initlistener ();
 public abstract void initdata ();
 public abstract void processclick (view v);
 @override
 public void onclick (view v) {
 processclick (v);
 }
 @override
 public void setuservisiblehint (boolean isvisibletouser) {
 super.setuservisiblehint (isvisibletouser);
 if (isvisibletouser) {
 isvisible=true;
 lazyload ();
 } else {
 //Setting is no longer visible
 isvisible=false;
 }
 }
 @override
 public view oncreateview (layoutinflater inflater, viewgroup container, bundle savedinstancestate) {
 mviews=new sparsearray<>();
 convertview=inflater.inflate (getlayoutid (), container, false);
 initviews ();
 isinitview=true;
 lazyload ();
 return convertview;
 }
 //Lazy loading
 private void lazyload () {
 if (! isfirstload ||! isvisible ||! isinitview) {
 //If it is not the first time to load, is not visible, and is not the initial view, then no data is loaded
 return;
 }
 //Download Data
 initlistener ();
 initdata ();
 //Setting is not the first time loading
 isfirstload=false;
 }
 public<e extends view>e findview (int viewid) {
 if (convertview!=null) {
 e view=(e) mviews.get (viewid);
 if (view == null) {
 view=(e) convertview.findviewbyid (viewid);
 mviews.put (viewid, view);
 }
 return view;
 }
 return null;
 }
 public<e extends view>void setonclick (e view) {
 view.setonclicklistener (this);
 }
}

The biggest difference between this and activity

1.convertview:Since the fragment's findid requires a convertview, we have to extract it

2.setuservisiblehint:This method is called when the fragment is switched,Returns whether the current fragment is visible to the user

public class homefragment extends basefragment {
 shopcontroller shopcontroller;
 private imageview iv_speech;
 private textview tv_search;
 @override
 public int getlayoutid () {
 return r.layout.fragment_home;
 }
 @override
 public void initviews () {
 iv_speech=findview (r.id.iv_speech);
 tv_search=findview (r.id.tv_search);
 }
 @override
 public void initdata () {
 shopcontroller=new shopcontroller (getactivity ());
 initshop ();
 }
 @override
 public void initlistener () {
 setonclick (iv_speech);
 setonclick (tv_search);
 }
 @override
 public void processclick (view v) {
 switch (v.getid ()) {
 case r.id.iv_speech:
 break;
 case r.id.tv_search:
 break;
 }
 }
 private void initshop () {
 }
}

Viewpager + fragment is used here. If you need to cache the fragment,Then you must cache the viewpager

//Set cache page
viewpager.setoffscreenpagelimit (5);

The following is the effect of setting the cache lazy loading mode,You can see that the first switch needs to load data,And the second time you switch back, the interface will not change.The effect is the same as mobile Taobao

Custom baseapplication

Customizing baseapplication is simple,The third-party settings and database loading are often used in the application.Can be customized according to project requirements

public abstract class baseapplication extends application {
 public abstract void initconfigs ();
 @override
 public void oncreate () {
 super.oncreate ();
 initconfigs ();
 }
}

Conclusion

After studying,It is recommended that you use basetemplate in your project.Of course, you also need to learn abstract methods.Extract common methods,For example, you can extract the basecontroller when loading data, and you can extract the general baseadapter in the adapter. You need to study the details.

  • Previous Android imitation QQ personal homepage drop-down rebound effect
  • Next Example of tab switching tab effect implemented by JS
  • Trends