Android MVP Design Framework template Beautiful ListView Pull-up Refresh drop-down load more

Source: Internet
Author: User

MVP is all called Model-view-presenter,model to provide data, view is responsible for display, Controller/presenter is responsible for the processing of logic. There is a big difference between MVP and MVC: In MVP, view does not use model directly, communication between them is done through presenter (Controller in MVC), all interactions occur inside presenter, In MVC, view reads the data directly from the model rather than through the Controller.

Most of the project is to face the interface programming, through the P layer can be pre-all required interface functions written out, and then implemented, greatly improve the project high cohesion, low coupling. Suitable for team development projects.

Today we are using the MVP Design framework template to implement a ListView pull-up refresh drop-down to load more features.

Project structure such as;


1, first we Listviewpresenters.java class, his role is to control the M business logic layer, to get the data we want, and then notify the V layer to update their corresponding UI. Because our needs are clear, is to implement the ListView pull-up refresh, drop-down load more, initialize the control, set the pull-down refresh listener. So we can first write the function interface, let listviewpresenters to realize its function.

Package Com.android.presenters;import Java.util.arraylist;import Android.support.v4.app.fragmentactivity;import Android.view.view;import Com.android.handlers.listviewhandle;import Com.android.services.listviewservice;import Com.android.services.si.ilistviewservice;import Com.android.view.listviewferris;import Com.android.view.vi.ilistviewferris;import Com.android.widget.listview.ferrislistview.onstartlistener;public Class Listviewpresenters {private Ilistviewferris ilistviewferris;private ilistviewservice ilistviewservice;private fragmentactivity mactivity;private View view;private int index = 1;public listviewpresenters (fragmentactivity MActivity2, View view2) {//TODO auto-generated constructor stubthis.mactivity = Mactivity2;this.view = View2;this.ilistv Iewferris = new Listviewferris (mactivity, view); this.ilistviewservice = new Listviewservice ();} /* * Initialize the control in the view layer */public void Onfindview () {Ilistviewferris.onfindview ();} /* On the Refresh method */public void Listviewrefresh (int index) {//post requestdata, obtaining data, and notifying View update Uiilistviewservice.sendpost (index, new Listviewhandle () {@Overridepublic void GetData via the handler interface (Arraylist<string> msgs) {//TODO Auto-generated method stub//notifies UI via handler interface to update data Ilistviewferris.refresh (MSGS);}});} /* * Pull load more methods */public void Listviewloadmore (int index) {//post request data, get data, And through the handler interface to notify the view Update Uiilistviewservice.sendpost (index, new Listviewhandle () {@Overridepublic void GetData ( Arraylist<string> msgs) {//TODO auto-generated method stub//notifies the UI via the handler interface to update the data if (msgs = = null) { Ilistviewferris.showloadingerror ();//Get Data failed to display notification ilistviewferris.loadmore (msgs);//Stop loading more} else { Ilistviewferris.loadmore (msgs);//Stop Loading More}}}); /* * For ListView Set pull-down refresh monitor */public void Setrefreshorloadmorelistem () {//TODO auto-generated method Stubilistviewferris.setrefreshorloadmorelistem (New Refresh (), New Loadmore ()); /* * For the ListView for the first time, auto drop-down refreshes */public void Firstload () {//TODO auto-generated method Stubilistviewferris.firstload ();} /* * Pull-up refresh Monitor */public class Refresh ImplemeNTS Onstartlistener {@Overridepublic void OnStart () {//TODO auto-generated Method Stubindex = 1;listviewrefresh (index);} }/* * Load more listener */public class Loadmore implements Onstartlistener {@Overridepublic void OnStart () {//TODO auto-generated Me Thod Stublistviewloadmore (++index);}}

2. Then we look at the UI layer, what data we need to update, which UI we need to update specifically.

Package Com.android.view.vi;import Java.util.arraylist;import Com.android.widget.listview.ferrislistview.onstartlistener;public interface Ilistviewferris {public void OnFindView ();//Initialize control public void Showloadingerror ();//Display load Error public void showloadingsuccess ();//Display plus success public void Setnotifylistview ();//Refresh listviewpublic void Refresh (arraylist<string> msgs);//Pull up refresh public void Loadmore ( Arraylist<string> msgs);//pull load more public void Setrefreshorloadmorelistem (Onstartlistener refresh, Onstartlistener loadmore);//Set pull-down refresh to listen on public void firstload ();//auto-Pull refresh for the first time}

Specific UI layer implementation of the specific method, mainly the pull-up refresh, drop-loading more, display error success status, as well as the initialization of the control methods.

Package Com.android.view;import Java.util.arraylist;import Android.support.v4.app.fragmentactivity;import Android.view.view;import Com.android.adapter.listviewadapter;import Com.android.futilssimple.r;import Com.android.utils.toastutils;import Com.android.view.vi.ilistviewferris;import Com.android.widget.listview.ferrislistview;import Com.android.widget.listview.FerrisListView.OnStartListener; Import Com.android.widget.listview.simplefooter;import Com.android.widget.listview.simpleheader;public class Listviewferris implements Ilistviewferris{private fragmentactivity mactivity;private View view;private FerrisListView Listview;private listviewadapter adapter;public listviewferris (fragmentactivity mActivity,View View) {//TODO Auto-generated constructor Stubthis.mactivity=mactivity;this.view=view;} Initialize view@overridepublic void Onfindview () {//TODO auto-generated method stubif (mactivity==null| | View==null) {return;} ListView = (Ferrislistview) View.findviewbyid (R.id.zlistview); Adapter=new ListViewadapter (mactivity);//sets the default offset, which is used primarily to implement transparent title bar functionality.        (optional) Float density = mactivity.getresources (). Getdisplaymetrics (). density;        Listview.setfirsttopoffset ((int) (* density));        Sets the style of the drop-down refresh (optional, but cannot drop-down refresh if no header is available) Simpleheader header = new Simpleheader (mactivity);        Header.settextcolor (0XFF0066AA);        Header.setcirclecolor (0xff33bbee);        Listview.setheadable (header);        Set load more styles (optional) simplefooter footer = new Simplefooter (mactivity);        Footer.setcirclecolor (0xff33bbee); listview.setfootable (footer);} Show load failed @overridepublic void Showloadingerror () {//TODO auto-generated method Stubtoastutils.showtoast (Mactivity, " Load failed ");} Show load succeeded @overridepublic void Showloadingsuccess () {//TODO auto-generated method Stubtoastutils.showtoast (Mactivity, " Loaded successfully ");} Refresh Data @overridepublic void Setnotifylistview () {//TODO auto-generated method stubif (adapter!=null) { Adapter.notifydatasetchanged ();}} Fill Refresh Data @overridepublic void Refresh (arraylist< String> msgs) {//TODO auto-generated method Stubif (Msgs!=null&&msgs.size () >0) {if (Listview.getadapter ( ) {==null) {Listview.setadapter (adapter); Adapter.setrefresh (msgs);} Else{adapter.setrefresh (msgs);} Listview.setrefreshsuccess ("Load succeeded"); Notification loading succeeded Listview.startloadmore (); Turn on the Loadingmore function}else{listview.setrefreshfail ("Load Failed");}} Fill load more data @overridepublic void Loadmore (arraylist<string> msgs) {//TODO auto-generated method Stubif (msgs!=null &&msgs.size () >0) {if (Listview.getadapter () ==null) {Listview.setadapter (adapter); Adapter.setdata (msgs) ;} Else{adapter.setdata (msgs);} Listview.setloadmoresuccess ();} Else{listview.stoploadmore ();}} Set the first automatic pull-up refresh for the ListView @overridepublic void Firstload () {Listview.refresh ();//Active pull-down refresh}// Set the pull-up for the ListView and the drop-down refresh @overridepublic void Setrefreshorloadmorelistem (Onstartlistener refresh,onstartlistener Loadmore) {//TODO auto-generated method Stublistview.setonrefreshstartlistener (refresh); Listview.setonloadmorestartlistener (Loadmore);}}

3, we look at the business logic layer, mainly through the simulation post in the background to request data, and then the obtained data, returned to the P-layer, the P-layer to the data to the V layer to update the UI interface.

Package Com.android.services.si;import Com.android.handlers.listviewhandle;public Interface Ilistviewservice {// Send POST request public void Sendpost (int index,listviewhandle handle);}
The functions that the business logic implements are as follows:

Package Com.android.services;import Java.util.arraylist;import Android.os.handler;import Com.android.futils.task.ftaskpool;import Com.android.futils.task.taskitem;import Com.android.handlers.listviewhandle;import Com.android.services.si.ilistviewservice;public class ListViewService Implements Ilistviewservice {string[] data={"Canada", "Sweden", "Australia", "Switzerland", "New Zealand", "Norway", "Denmark", "Finland", "Austria", "Netherlands", "Germany", "Japan", "Belgium "," Italy "," UK "};p ublic Listviewservice () {//TODO auto-generated constructor stub}private Handler handler=new Handler (); Private arraylist<string> msgs; @Overridepublic void sendpost (int index, listviewhandle handle) {//TODO Auto-generated Method Stubswitch (Index) {case 1:getdata (index,handle); Break;case 2:getdata (index,handle); break;case 3:getdata (index,handle); Break;case 4:getdata (Index,handle); Break;case 5:getdata (Index,handle); Break;case 6: GetData (Index,handle); Break;default:msgs=null;handle.getdata (msgs); break;}} Private synchronized void GetData (final int index, final listviewhandle Handle) {//TODO auto-generated Method Stub handler.postdelayed (new Runnable () {@Override public void R Un () {ftaskpool.getinstance (). Execute (new TaskItem () {@Override public void DoMain () {/        /Show data in foreground//TODO auto-generated Method stub if (handle!=null) {handle.getdata (msgs); }} @Override public void Doback () {//processing data in the background//TODO auto-generated Method St        UB Msgs=new arraylist<string> ();        for (String d:data) {Msgs.add (index+ "," +d ");            }        }        }); }}, 2 * 1000);//delay of 2 seconds after execution}}


4, finally, we look at the fragment inside the Tathagata call this P-layer, to control the MV for data acquisition and update

Package Com.android.fragment;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Com.android.futilssimple.r;import Com.android.presenters.listviewpresenters;public class ListViewFragment extends Fragment {private listviewpresenters listviewpresenters;    Public listviewfragment () {    }      @Override public    View Oncreateview (layoutinflater inflater, ViewGroup Container,            Bundle savedinstancestate) {    View Rootview = inflater.inflate (R.layout.listviewfragment, container, false);    Listviewpresenters=new listviewpresenters (Getactivity (), rootview);//Create a new controller    Listviewpresenters.onfindview () ;//Initialize View    listviewpresenters.setrefreshorloadmorelistem ();//Set Refresh Listener    listviewpresenters.firstload ();// First automatic pull-up refresh        return rootview;}        }


5, the source code demo later upload




Android MVP Design Framework template Beautiful ListView Pull-up Refresh drop-down load more

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.