Latest Android ListView pull-down refresh slide Loading

Source: Internet
Author: User

Latest Android ListView pull-down refresh slide Loading

In the development project process, the drop-down refresh and slide loading of listView are basically used. Before that, we should use pull to refresh or its variant version, google official on the latest android. support. A new class SwipeRefreshLayout is added to the v4 package. The address class is used to provide official pull-down refresh and the effect is quite good. The custom listview is used to pull and load more resources, it is also quite simple.

Pull-down refresh

A Brief Introduction:

First, it is a viewgroup, but only one sub-control is allowed. The sub-control can be any view. When used, the class implements the OnRefreshListener interface in onRefresh () to complete the task,

After SwipeRefreshLayout is obtained by findviewbyid, SwipeRefreshLayout. setRefreshing (true) is displayed for the refresh animation; SwipeRefreshLayout. setRefreshing (false) for canceling the refresh animation.

In addition, there are some other common methods, such as displaying a refresh animation when judging, using SwipeRefreshLayout. isShown ().

Layout:

 

        
         
     


 

For more ideas about pulling and loading, listen to the sliding event and determine whether the event has reached the bottom. When it reaches the bottom, call back a method we have defined to achieve the purpose of pulling and loading more. But how to monitor and callback? Let's take a look at the execution process of the Click Event of the Button control. The Button inherits textView and textView inherits View 1. implement the OnClickListener interface and override onClick method 2. set Button. the same applies to setOnClickListener (this) or new interfaces.
View the View source code.
The View class has the OnClickOistener interface and the setOnClickListener method.

The steps we need to implement in our own listview are clear. myListView inherits listView 2. implement the onLoadMore Interface 3. declare the onLoadMore interface object in the Class 4. set the click method setOnLoadMore 5. call onLoadMore as needed. loadMore method. The MyListView source code is attached below:
Package com. example. listviewloadmore; import android. annotation. suppressLint; import android. content. context; import android. util. attributeSet; import android. util. log; import android. view. layoutInflater; import android. view. view; import android. widget. absListView; import android. widget. absListView. onScrollListener; import android. widget. listView; public class LoadMoreListView extends ListView implements OnS CrollListener {private View footer; private int totalItem; private int lastItem; private boolean isLoading; private OnLoadMore onLoadMore; private LayoutInflater inflater; public LoadMoreListView (Context context) {super (context ); init (context);} public LoadMoreListView (Context context, AttributeSet attrs) {super (context, attrs); init (context);} public LoadMoreListView (Context context, AttributeSet attrs, int DefStyle) {super (context, attrs, defStyle); init (context) ;}@ SuppressLint ("InflateParams") private void init (Context context) {inflater = LayoutInflater. from (context); footer = inflater. inflate (R. layout. load_more_footer, null, false); footer. setVisibility (View. GONE); this. addFooterView (footer); this. setOnScrollListener (this) ;}@ Overridepublic void onScroll (AbsListView view, int firstVisibleItem, int visib LeItemCount, int totalItemCount) {this. lastItem = firstVisibleItem + visibleItemCount; this. totalItem = totalItemCount;} @ Overridepublic void onScrollStateChanged (AbsListView view, int scrollState) {if (this. totalItem = lastItem & scrollState = SCROLL_STATE_IDLE) {Log. v ("isLoading", "yes"); if (! IsLoading) {isLoading = true; footer. setVisibility (View. VISIBLE); onLoadMore. loadMore () ;}} public void setLoadMoreListen (OnLoadMore onLoadMore) {this. onLoadMore = onLoadMore;}/*** call this method after loading is complete */public void onLoadComplete () {footer. setVisibility (View. GONE); isLoading = false;} public interface OnLoadMore {public void loadMore ();}}


The code is not very complex and can be understood carefully. Loading layout code at the bottom
 
     
          
           
        
    
   
  
 


It is almost the end of pulling and loading more. The following is a combination of pull-down and pull-up. The layout file main. xml is used.
             
          
      
 


MainActivity code
Package com. example. listviewloadmore; import java. util. arrayList; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. support. v4.widget. swipeRefreshLayout; import android. support. v4.widget. swipeRefreshLayout. onRefreshListener; import android. widget. arrayAdapter; import android. widget. toast; import com. example. listviewloadmore. loadMoreListView. onLoadMore; public class MainActivity extends Activity implements OnRefreshListener, OnLoadMore {private LoadMoreListView listView; private SwipeRefreshLayout swip; private int page = 1; ArrayList
 
  
Data1 = new ArrayList
  
   
(); Private ArrayAdapter
   
    
Adapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initView (); initData (1);} private void initView () {listView = (LoadMoreListView) findViewById (R. id. listView); listView. setLoadMoreListen (this); swip = (SwipeRefreshLayout) findViewById (R. id. swip_index); swip. setOnRefreshListener (this); swip. setColorSchemeResources (android. r. color. holo_blue_light, android. r. color. holo_red_light, android. r. color. holo_orange_light, android. r. color. holo_green_light); adapter = new ArrayAdapter
    
     
(This, android. r. layout. simple_list_item_1, data1);}/*** load data * @ param page2 page number */private void initData (int page2) {// TODO Auto-generated method stubif (page2 = 1) {for (int I = 0; I <10; I ++) {data1.add (I + 1) + ""); listView. setAdapter (adapter) ;}} else {data1.add ("newly added" + (9 + page2) + ");}} // pull-down refresh method @ Overridepublic void onRefresh () {Toast. makeText (this, "starting to refresh", Toast. LENGTH_SHORT ). show (); new Handler (). postDelayed (new Runnable () {@ Overridepublic void run () {if (swip. isShown () {swip. setRefreshing (false);} Toast. makeText (MainActivity. this, "refreshed", Toast. LENGTH_SHORT ). show () ;}}, 3000) ;}// load more methods @ Overridepublic void loadMore () {page ++; new Handler (). postDelayed (new Runnable () {@ Overridepublic void run () {initData (page); listView. onLoadComplete (); Toast. makeText (MainActivity. this, "loaded", Toast. LENGTH_SHORT ). show (); adapter. notifyDataSetChanged () ;}, 3000 );}}
    
   
  
 



Next:




Source Code address: Click to download

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.