PullToRefresh

Github https://github.com/chrisbanes/Android-PullToRefresh

学习参考 http://blog.csdn.net/harvic880925/article/details/17680305

 

  刷新前:                    刷新时:                   刷新后:

      

 

  <1>Import Android-PullToRefresh-master/library

  <2>New Android Project. Property---> Library: Add

  <3>Code: new PullToRefreshListView()   then import follow the tips 

         one of the imports --> import com.handmark.pulltorefresh.library.PullToRefreshListView;

    XML:   <com.handmark.pulltorefresh.library.PullToRefreshListView  />

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="mirror.android.pulltorefreshtest.PullToreFreshTest" >

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/pull_fresh_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        
        android:cacheColorHint="#00000000"  
        android:divider="#19000000"  
        android:dividerHeight="4dp"  
        android:fadingEdge="none"  
        android:fastScrollEnabled="false"  
        android:footerDividersEnabled="false"  
        android:headerDividersEnabled="false"  
        android:smoothScrollbar="true"/>

</LinearLayout>

 

package mirror.android.pulltorefreshtest;

import java.util.Arrays;
import java.util.LinkedList;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class PullToreFreshTest extends Activity {
    
    private PullToRefreshListView mPullRefreshListView; 
    
    private String[] mStrings = {"1","2","3","4","5","6","7","8"
                                ,"9","10","11","12","13","14","15","16"};
    
    private LinkedList<String> mListItems;
    
    private ArrayAdapter<String> mAdapter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull_tore_fresh_test);
        
        mPullRefreshListView = (PullToRefreshListView)findViewById(R.id.pull_fresh_list);
        mPullRefreshListView.setMode(Mode.BOTH);
        mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                
                // Update the LastUpdatedLabel  
                String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),  
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);  
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);  
                
                new GetDataTask().execute();
            }
        });
        
        mListItems = new LinkedList<String>();  
        mListItems.addAll(Arrays.asList(mStrings));  
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems); 
        
        // 方法一  
        //mPullRefreshListView.setAdapter(mAdapter);  
        // 方法二  
        ListView actualListView = mPullRefreshListView.getRefreshableView();  
        actualListView.setAdapter(mAdapter);  
    }
    
    private class GetDataTask extends AsyncTask<Void, Void, String>{

        @Override
        protected String doInBackground(Void... params) {
            try{
                Thread.sleep(1000);
            }
            catch(InterruptedException e){
            }
            String str = "Added after refresh";
            return str;
        }

        @Override
        protected void onPostExecute(String result) {
            //头部新增加内容
            mListItems.addFirst(result);  
            //通知程序数据集已经改变,如果不做通知,那么将不会刷新mListItems的集合  
            mAdapter.notifyDataSetChanged();  
            // Call onRefreshComplete when the list has been refreshed.  
            mPullRefreshListView.onRefreshComplete();  
            super.onPostExecute(result);  
        }
    } 
}

 

posted @ 2014-12-12 15:08  Mirrorhanman  阅读(248)  评论(0编辑  收藏  举报