Android MVP design framework template-beautiful ListView pull-up refresh pull-down to load more, androidlistview

Source: Internet
Author: User

Android MVP design framework template-beautiful ListView pull-up refresh pull-down to load more, androidlistview

The mvp is called Model-View-Presenter. The Model provides data, the View is responsible for display, and the Controller/Presenter is responsible for logical processing. There is a major difference between MVPs and MVC: In MVPs, views do not directly use models. Communication between them is implemented through Presenter (Controller in MVC, all interactions occur within the Presenter. In MVC, the View directly reads data from the Model rather than through the Controller.

Most of the projects are interface programming. Through the P layer, all required interface functions can be written in advance for implementation, which greatly improves the High Cohesion and low coupling of the project. Suitable for team development projects.

Today, we use the MVP design framework template to implement more functions of a ListView pull and refresh drop-down.

The project structure is as follows;


1. First, we use the ListViewPresenters. java class to control the M business logic layer, obtain the data we want, and then notify the V layer to update their corresponding UI. As our requirement is clear, it is to achieve the top-up and refresh of Listview, load more from the drop-down list, initialize the control, and set the top-down and pull-down refresh listening. Therefore, you can write all the functional interfaces first, so that ListViewPresenters can implement the functions.

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 extends 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. iListViewFerris = new ListViewFerris (mActivity, view); this. iListViewService = new ListViewService ();}/** initialize the control */public void onFindView () {iListViewFerris. onFindView ();}/** refreshing method */public void listViewRefresh (int index) {// post request data to obtain data, the handler interface is used to notify the View to update the UIiListViewService. sendPost (index, new ListViewHandle () {@ Overridepublic void getData (ArrayList <String> msgs) {// TODO Auto-generated method stub // notify the UI to update data iListViewFerris through the handler interface. refresh (msgs) ;}}) ;}/ ** pull down to load more methods */public void listViewLoadMore (int index) {// post request data to obtain data, the handler interface is used to notify the View to update the UIiListViewService. sendPost (index, new ListViewHandle () {@ Overridepublic void getData (ArrayList <String> msgs) {// TODO Auto-generated method stub // notify the UI to update data through the handler interface if (msgs = null) {iListViewFerris. showLoadingError (); // the error message "iListViewFerris" is displayed when data fails to be obtained. loadMore (msgs); // stop loading more} else {iListViewFerris. loadMore (msgs); // stop loading more});}/** set up and pull for Listview refresh listener */public void setRefreshOrLoadMoreListem () {// TODO Auto-generated method stubiListViewFerris. setRefreshOrLoadMoreListem (new Refresh (), new Loadmore ();}/** is the first time that the Listview is automatically pulled down and refreshed */public void firstload () {// TODO Auto-generated method stubiListViewFerris. firstLoad ();}/** pull up Refresh listener */public class Refresh implements OnStartListener {@ Overridepublic void onStart () {// TODO Auto-generated method stubindex = 1; listViewRefresh (index) ;}}/** load more listeners */public class Loadmore implements OnStartListener {@ Overridepublic void onStart () {// TODO Auto-generated method stublistViewLoadMore (++ index );}}}

2. Next, let's take a look at the UI Layer, which data we need to update and which UI we need to update.

Package com. android. view. vi; import java. util. arrayList; import com. android. widget. listview. ferrisListView. onStartListener; public interface IListViewFerris {public void onFindView (); // initialize the control public void showLoadingError (); // display the loading error public void showLoadingSuccess (); // display the added public void setpolicylistview (); // refresh Listviewpublic void refresh (ArrayList <String> msgs ); // pull up and refresh public void loadMore (ArrayList <String> msgs); // pull down to load more public void setRefreshOrLoadMoreListem (OnStartListener refresh, OnStartListener loadmore ); // set up pull-down refresh listener public void firstLoad (); // The first automatic pull-up refresh}

The specific methods for implementing the UI Layer are mainly pull up and refresh, pull down and load more, display the successful error status, and initialize controls.

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; impor T 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 = vi Ew;} // 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); // you can specify the default offset for transparent title bars. (Optional) float density = mActivity. getResources (). getDisplayMetrics (). density; listView. setFirstTopOffset (int) (50 * density); // you can set the pull-down refresh style (Optional). If no Header is available, you cannot pull it down and refresh it.) SimpleHeader header = new SimpleHeader (mActivity ); header. setTextColor (0xff0066aa); header. setCircleColor (0xff33bbee); listView. setHeadable (header); // set to load more styles (optional) SimpleFooter footer = new SimpleFooter (mActivity); footer. setCircleColor (0 Xff33bbee); listView. setFootable (footer);} // display loading failure @ Overridepublic void showLoadingError () {// TODO Auto-generated method stubToastUtils. showToast (mActivity, "loading failed");} // display Successful loading @ Overridepublic void showLoadingSuccess () {// TODO Auto-generated method stubToastUtils. showToast (mActivity, "loaded successfully");} // refresh data @ Overridepublic void setpolicylistview () {// TODO Auto-generated method stubif (adapter! = Null) {adapter. notifyDataSetChanged () ;}// fill in and refresh the 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 ("loaded successfully"); // notification that listView is loaded successfully. startLoadMore (); // enable LoadingMore function} else {listView. setRefreshFail ("loading failed") ;}// fill in and 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 auto-pull refresh @ Overridepublic void firstLoad () {Listview. refresh (); // actively pull-down refresh} // set up and pull-down refresh for Listview @ Overridepublic void setRefreshOrLoadMoreListem (OnStartListener refresh, OnStartListener loadmore) {// TODO Auto-generated method stublistView. setOnRefreshStartListener (refresh); listView. setOnLoadMoreStartListener (loadmore );}}

3. Let's take a look at the business logic layer, which is mainly through simulating POST request data in the background, and then returning the obtained data to the P layer, the P layer submits the data to the V layer to update the UI.

Package com. android. services. si; import com. android. handlers. ListViewHandle; public interface IListViewService {// send the post request public void sendPost (int index, ListViewHandle handle );}
The specific functions of business logic 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", "Holland", "Germany", "Japan", "Belgium", "Italian Lee "," UK "}; public 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; c Ase 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 run () {ftaskpool.getinstance(.exe cute (new TaskItem () {@ Override public void doMain () {// display data on the foreground // TODO Au To-generated method stub if (handle! = Null) {handle. getData (msgs) ;}@override public void doBack () {// process data in the background // TODO Auto-generated method stub msgs = new ArrayList <String> (); for (String d: data) {msgs. add (index + "," + d) ;}}}}, 2*1000); // execution after a delay of 2 seconds }}


4. Finally, let's take a look at the P layer called in Fragment to control MV 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 extends Fragment {private ListViewPresenters listViewPresenters; public ListViewFragment () {}@ Override public View onCreateView. inflate (R. layout. listviewfragment, container, false); listViewPresenters = new ListViewPresenters (getActivity (), rootView); // create a controller listViewPresenters. onFindView (); // initialize view listViewPresenters. setRefreshOrLoadMoreListem (); // set the refresh listener to listViewPresenters. firstload (); // refresh return rootView automatically pulled up for the first time ;}}


5. Upload the source code demo later




Related Article

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.