Analysis and simple use of Android open-source project pulltorefresh

Source: Internet
Author: User

In Android development, sometimes we need to access the network to refresh data in real time, such as the latest online status information of QQ friends, and more dynamic information of friends needs to be displayed in the QQ space, the EOE Forum client displays more post information. The android-pulltorefresh open-source project provides the function of swiping down to refresh the list. You can apply the project to your project with slight modifications.

 

1.

 

2. Engineering Composition

PullToRefreshListView. java

The OnRefreshListener listens to the refreshing operation interface. The onRefresh () refresh function displays the refreshing progress bar in the list header.

After the onRefreshComplete () Refresh operation is completed, the list is restored to normal

The specific implementation of the above classes and interfaces:

 

public class PullToRefreshListView extends ListView implements OnScrollListener{  /**Interface definition for a callback to be invoked when list should be refreshed.     */    public interface OnRefreshListener {        /**         * Called when the list should be refreshed.         * 

* A call to {@ link PullToRefreshListView # onRefreshComplete ()} is expected to indicate that the refresh has completed. */public void onRefresh ();}/*** Resets the list to a normal state after a refresh. * @ param lastUpdated Last updated. */public void onRefreshComplete (CharSequence lastUpdated) {setLastUpdated (lastUpdated); onRefreshComplete ();}/*** Resets the list to a normal state after a refresh. */public void onRefreshComplete () {Log. d (TAG, onRefreshComplete); resetHeader (); // If refresh view is visible when loading completes, scroll down to // the next item. if (getFirstVisiblePosition () = 0) {invalidateViews (); setSelection (1 );}}}

 

Pull_to_refresh_header.xml

The PullToRefreshListView header displays the refresh progress bar. In PullToRefreshListView. java, call the following statement to add the child layout to the top of the list.

Private LayoutInflater mInflater;

Private RelativeLayout mRefreshView;

MInflater = (LayoutInflater) context. getSystemService (
Context. LAYOUT_INFLATER_SERVICE );
// Header part of PullToRefreshListView
MRefreshView = (RelativeLayout) mInflater. inflate (R. layout. pull_to_refresh_header, this, false );

// Add header part to the ListView
AddHeaderView (mRefreshView );

PullToRefreshActivity. java (MainActivity)

Pull_to_refresh.xml

 

 
     
      
  
 
Note:

LiveActivity inherits many interfaces for List operations. We can easily rewrite the methods required in these operations to implement the functions we need. If you want to use ListActivity, the Layout file of the Activity must contain one (only one) ListView and the ListView id = @ id/android: list.

 

 

3. PullToRefreshActivity. java

Refresh the extra list data String mExtras []. When the extra list data is displayed, the refresh operation is not performed.

For example, after the following example is refreshed three times, No additional data needs to be displayed. The "No More Messages" prompt is displayed when the pull-out list is refreshed"

 

package com.markupartist.android.example.pulltorefresh;import java.util.Arrays;import java.util.LinkedList;import android.annotation.SuppressLint;import android.app.ListActivity;import android.os.AsyncTask;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.Toast;import com.markupartist.android.widget.PullToRefreshListView;import com.markupartist.android.widget.PullToRefreshListView.OnRefreshListener;@SuppressLint(NewApi)public class PullToRefreshActivity extends ListActivity {        private LinkedList
 
   mListItems;    int count = 0;        /** Called when the activity is first created. */    @SuppressLint(NewApi)@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.pull_to_refresh);                // Set a listener to be invoked when the list should be refreshed.        ((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {            @Override            public void onRefresh() {                // Do work to refresh the list here.            if(count < mExtras.length)            new GetDataTask().execute(count++);            else{            Toast.makeText(getApplicationContext(), No More Messages,             Toast.LENGTH_LONG).show();                        //Resets the list to a normal state after a refresh            ((PullToRefreshListView) getListView()).onRefreshComplete();            }                        }        });        mListItems = new LinkedList
  
   ();        mListItems.addAll(Arrays.asList(mStrings));        ArrayAdapter
   
     adapter = new ArrayAdapter
    
     (this,                android.R.layout.simple_list_item_1, mListItems);        setListAdapter(adapter);    }    private class GetDataTask extends AsyncTask
     
       {        private int count;            @Override        protected String[] doInBackground(Integer... params) {        count = params[0];        // Simulates a background job.            try {                Thread.sleep(2000);            } catch (InterruptedException e) {                ;            }            return mStrings;        }        @SuppressLint(NewApi)@Override        protected void onPostExecute(String[] result) {           // mListItems.addFirst(Added after refresh...);        mListItems.addFirst(mExtras[count]);            // Call onRefreshComplete when the list has been refreshed.            ((PullToRefreshListView) getListView()).onRefreshComplete();           // super.onPostExecute(result);        }    }    private String[] mStrings = {            Abbaye de Belloc, Abbaye du Mont des Cats, Abertam,            Abondance, Ackawi, Acorn, Adelost, Affidelice au Chablis,            Afuega'l Pitu, Airag, Airedale, Aisy Cendre,            Allgauer Emmentaler};        private String[] mExtras = {extra1,extra2,extra3};    }
     
    
   
  
 


 

 

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.