Xlistview刷新

一、在本身的項目中按以往的方法寫listview、grilview的佈局、適配器、以及界面顯示內容
二、在項目中引入第三方類庫(這裏以listview爲例)
三、建議爲該類庫單獨創建一個包。
四、導入如圖所示的類到本身的項目中:
五、導入如圖所示的類到本身的項目中,注:標號1必倒,
      標號2,當咱們是listview時爲pullableListView;
                  當咱們是gridview時爲pullablegridview,以此類推
六、導入drawable下的內容:
七、導入anim下的文件
八、導入values下的文件,注:須要將strings.xml下的除第一條外的文件複製到本身的string.xml文件中
        
九、導入layout下的最後兩個文件
十、至此須要導入的類庫完畢,打開報錯的代碼,進行改正(change、更改R包);
十一、修改xml中的文件,如圖
十二、mainactivty的java代碼中的修改
public class MainActivity extends Activity {

    int page = 1;
    private PullableListView lv;
    private Base ba;
    private List<Mydata> list = new ArrayList<Mydata>();
    private String url = "http://api.expoon.com/AppNews/getNewsList/type/1/p/";
    // 建立handler對象
    Handler h = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            String result = msg.obj.toString();
            Gson g = new Gson();
            MySupper ms = g.fromJson(result, MySupper.class);
            ArrayList<Mydata> data = ms.getData();
            if (page==1) {
                list.clear();
            }
            Log.e("111111111111", "11111111111111111111111111111");
            list.addAll(data);
            update();

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 設置隱藏標題
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 加載xml文件
        setContentView(R.layout.activity_main);
        PullToRefreshLayout layout = (PullToRefreshLayout) findViewById(R.id.layout);
        lv = (PullableListView) findViewById(R.id.list);
        setJson();
        layout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {


                h.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        setJson();
                        // 千萬別忘了告訴控件刷新完畢了哦!
                        pullToRefreshLayout
                                .refreshFinish(PullToRefreshLayout.SUCCEED);
                    }

                }, 1000);


            }

            @Override
            public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {

                h.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        page++;
                        setJson();
                        // 千萬別忘了告訴控件加載完畢了哦!
                        pullToRefreshLayout
                                .loadmoreFinish(PullToRefreshLayout.SUCCEED);

                    }
                }, 1000);
            }
        });

    }

    public void setJson() {

        new Thread() {
            public void run() {
                try {
                    String string = MyHttpGet.getjson(url, page);
                    Message message = h.obtainMessage();
                    message.obj = string;
                    h.sendMessage(message);
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }.start();

    }

    // 建立更新適配器的方法
    public void update() {
        // 判斷ba是否爲空
        if (ba == null) {
            ba = new Base(MainActivity.this, list);
            // 爲lv添加適配器
            lv.setAdapter(ba);
        } else {
            // 刷新適配器
            ba.UpDataAdapter(list);
        }
    }

}
1三、適配器要寫刷新適配器的方法
//定義更新適配器的方法
    public void UpDataAdapter(List<Mydata> list){
        this.list=list;
        this.notifyDataSetChanged();
    }
1四、若是頁面是用fragment填充的viewpager,則在viewpager頁面加載佈局前寫一個判斷,以防止fragmen被銷燬而致使的數據加載不出來:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // 填充佈局
        if (v==null) {
            v = View.inflate(getActivity(), R.layout.tuijian, null);
            layout = (PullToRefreshLayout) v
            .findViewById(R.id.layout);

        }
        ViewGroup mParent = (ViewGroup) v.getParent();

        if(mParent!=null){
            mParent.removeView(v);
        }

        lv = (PullableListView) v.findViewById(R.id.listview);

 Pullable.java; java

,直接沾
package com.jingchen.pulltorefresh.pullableview;

public interface Pullable
{
	/**
	 * 判斷是否能夠下拉,若是不須要下拉功能能夠直接return false
	 * 
	 * @return true若是能夠下拉不然返回false
	 */
	boolean canPullDown();

	/**
	 * 判斷是否能夠上拉,若是不須要上拉功能能夠直接return false
	 * 
	 * @return true若是能夠上拉不然返回false
	 */
	boolean canPullUp();
}

  PullableListView.java android

,直接沾

 

package com.chen.listTongyong;json

import com.exaple.tong.Pullable;api

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ListView;app

public class PullableListView extends ListView implements Pullable
{ide

public PullableListView(Context context)
{
super(context);
}佈局

public PullableListView(Context context, AttributeSet attrs)
{
super(context, attrs);
}post

public PullableListView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}ui

@Override
public boolean canPullDown()
{
if (getCount() == 0)
{
// 娌℃湁item鐨勬椂鍊欎篃鍙互涓嬫媺鍒鋒柿
return true;
} else if (getFirstVisiblePosition() == 0
&& getChildAt(0).getTop() >= 0)
{
// 婊戝埌ListView鐨勯《閮ㄤ簡
return true;
} else
return false;
}this

@Override
public boolean canPullUp()
{
if (getCount() == 0)
{
// 娌℃湁item鐨勬椂鍊欎篃鍙互涓婃媺鍔犺浿
return true;
} else if (getLastVisiblePosition() == (getCount() - 1))
{
// 婊戝埌搴曢儴浜�
if (getChildAt(getLastVisiblePosition() - getFirstVisiblePosition()) != null
&& getChildAt(
getLastVisiblePosition()
- getFirstVisiblePosition()).getBottom() <= getMeasuredHeight())
return true;
}
return false;
}
}

  

PullToRefreshLayout.java ,直接沾

package com.exaple.tong;


import java.util.Timer;
import java.util.TimerTask;

import com.exmple.pull.R;


import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
* 閼奉亜鐣炬稊澶屾畱鐢啫鐪敍宀�暏閺夈儳顓擱悶鍡曠瑏娑擃亜鐡欓幒褌嬈㈤敍灞藉従娑擃厺絝存稉顏呮Ц娑撳濯烘徑杈劇礉娑擄拷閲滈弰顖氬瘶閸氼偄鍞寸�鍦畱pullableView閿涘牆褰叉禒銉︽Ц鐎圭偟騫嘝ullable閹恆儱褰溳惃鍕畱嫺犺綞峍iew閿涘鞝�
* 鏉╂ɑ婀佹稉錕介嚋娑撳﹥濯烘徑杈劇礉閺囨潙顦跨拠錒佇掔憴浣稿觸鐎詭晭ttp://blog.csdn.net/zhongkejingwang/article/details/38868463
*
* @author 闂勫牓娼�
*/
public class PullToRefreshLayout extends RelativeLayout
{
public static final String TAG = "PullToRefreshLayout";
// 閸掓繂顫愰悩鑸碉拷
public static final int INIT = 0;
// 闁插﹥鏂侀崚閿嬫煀
public static final int RELEASE_TO_REFRESH = 1;
// 濮濓絽婀崚閿嬫煀
public static final int REFRESHING = 2;
// 闁插﹥鏂侀崝鐘烘祰
public static final int RELEASE_TO_LOAD = 3;
// 濮濓絽婀崝鐘烘祰
public static final int LOADING = 4;
// 閹垮秳綞旂�灞劇槸
public static final int DONE = 5;
// 瑜版挸澧犻悩鑸碉拷
private int state = INIT;
// 閸掗攱鏌婇崶鐐剁殶閹恆儱褰�
private OnRefreshListener mListener;
// 閸掗攱鏌婇幋鎰
public static final int SUCCEED = 0;
// 閸掗攱鏌婃徑杈Е
public static final int FAIL = 1;
// 閹稿絎匶閸ф劖鐖i敍灞肩瑐娑擄拷閲滄禍瀣╂閻愮瓬閸ф劖鐖�
private float downY, lastY;

// 娑撳濯洪惃鍕獩緇傛眹錕藉▔銊﹀壈閿涙ullDownY閸滃ullUpY娑撳秴褰查懗鈃夋倱閺冩湹絎夋稉錕�
public float pullDownY = 0;
// 娑撳﹥濯洪惃鍕獩緇傦拷
private float pullUpY = 0;

// 闁插﹥鏂侀崚閿嬫煀閻ㄥ嫯紿涗粋錕�
private float refreshDist = 200;
// 闁插﹥鏂侀崝鐘烘祰閻ㄥ嫯紿涗粋錕�
private float loadmoreDist = 200;

private MyTimer timer;
// 閸ョ偞綺撮柅鐔峯
public float MOVE_SPEED = 8;
// 緇楊兛絝村▎鈩冨⒔鐞涘苯絝風仦錕�
private boolean isLayout = false;
// 閸︺劌鍩溳弬鎷岀箖緇嬪鑵戝鎴濆З閹垮秳綞�
private boolean isTouch = false;
// 閹靛瀵氬鎴濆З鐠烘繄顬囨稉搴濈瑓閹峯銇旈惃鍕撥閸斻劏紿涗粋緇樼槷閿涘奔鑵戦梻緲犵窗闂呭繑顒滈崚鍥у毐閺佹澘褰夐崠錕�
private float radio = 2;

// 娑撳濯虹粻顓炪仈閻ㄥ嫯嫺�80鎺抽崝銊ф暰
private RotateAnimation rotateAnimation;
// 閸у洤瀵戦弮瀣祮閸斻劎鏁�
private RotateAnimation refreshingAnimation;

// 娑撳濯烘徑錕�
private View refreshView;
// 娑撳濯洪惃鍕唲婢訛拷
private View pullView;
// 濮濓絽婀崚閿嬫煀閻ㄥ嫬嫺橀弽錕�
private View refreshingView;
// 閸掗攱鏌婄紒鎾寸亯閸ョ偓鐖�
private View refreshStateImageView;
// 閸掗攱鏌婄紒鎾寸亯閿涙礙鍨氶崝鐔稿灗婢惰精瑙�
private TextView refreshStateTextView;

// 娑撳﹥濯烘徑錕�
private View loadmoreView;
// 娑撳﹥濯洪惃鍕唲婢訛拷
private View pullUpView;
// 濮濓絽婀崝鐘烘祰閻ㄥ嫬嫺橀弽錕�
private View loadingView;
// 閸旂姾嫺囩紒鎾寸亯閸ョ偓鐖�
private View loadStateImageView;
// 閸旂姾嫺囩紒鎾寸亯閿涙礙鍨氶崝鐔稿灗婢惰精瑙�
private TextView loadStateTextView;

// 鐎圭偟騫囨禍鍝杣llable閹恆儱褰溳惃鍒卛ew
private View pullableView;
// 鏉╁洦鎶ゆ徑姘卞仯鐟欙婦顫�
private int mEvents;
// 鏉╂瑤琚辨稉顏勫綁闁插繒鏁ら弶銉﹀付閸掔ull閻ㄥ嫭鏌熼崥鎴礉嬋″倹鐏夋稉宥呭閹貉冨煑閿涘苯綞嬮幆鍛枌濠娐ゅ喕閸欘墶絎傞幏澶婂嫉閸欘墶絎呴幏澶嬫濞屸剝紜舵稉瀣
private boolean canPullDown = true;
private boolean canPullUp = true;

private Context mContext;

/**
* 閹笛嗩攽閼奉亜濮╅崶鐐寸泊閻ㄥ埅andler
*/
Handler updateHandler = new Handler()
{

@Override
public void handleMessage(Message msg)
{
// 閸ョ偛鑴婇柅鐔峯闂呭繋絎呴幏澶庣獩緇傜北oveDeltaY婢х偛銇囬懓灞筋杻婢訛拷
MOVE_SPEED = (float) (8 + 5 * Math.tan(Math.PI / 2
/ getMeasuredHeight() * (pullDownY + Math.abs(pullUpY))));
if (!isTouch)
{
// 濮濓絽婀崚閿嬫煀閿涘奔絎栧▽鈩冩箒瀵幫拷絎傞幒銊ф畱鐠囨繂鍨幃顒�粻閿涘本妯夌粈錕藉鍀呮躬閸掗攱鏌�.."
if (state == REFRESHING && pullDownY <= refreshDist)
{
pullDownY = refreshDist;
timer.cancel();
} else if (state == LOADING && -pullUpY <= loadmoreDist)
{
pullUpY = -loadmoreDist;
timer.cancel();
}

}
if (pullDownY > 0)
pullDownY -= MOVE_SPEED;
else if (pullUpY < 0)
pullUpY += MOVE_SPEED;
if (pullDownY < 0)
{
// 瀹告彃鐣幋鎰礀瀵拷
pullDownY = 0;
pullView.clearAnimation();
// 闂呮劘嬀屾稉瀣婢跺瓨妞傞張澶婂講閼衝�綣曢崷銊ュ煕閺傚府鞝濋崣顏呮箒瑜版挸澧犻悩鑸碉拷娑撳秵妲稿鍀呮躬閸掗攱鏌婇弮鑸靛閺�懓褰夐悩鑸碉拷
if (state != REFRESHING && state != LOADING)
changeState(INIT);
timer.cancel();
requestLayout();
}
if (pullUpY > 0)
{
// 瀹告彃鐣幋鎰礀瀵拷
pullUpY = 0;
pullUpView.clearAnimation();
// 闂呮劘嬀屾稉濠冨婢跺瓨妞傞張澶婂講閼衝�綣曢崷銊ュ煕閺傚府鞝濋崣顏呮箒瑜版挸澧犻悩鑸碉拷娑撳秵妲稿鍀呮躬閸掗攱鏌婇弮鑸靛閺�懓褰夐悩鑸碉拷
if (state != REFRESHING && state != LOADING)
changeState(INIT);
timer.cancel();
requestLayout();
}
Log.d("handle", "handle");
// 閸掗攱鏌婄敮鍐ㄧ湰,嫺兼俺鍤滈崝銊ㄧ殶閻⑩暙nLayout
requestLayout();
// 濞屸剝婀侀幏鏍ㄥ閹存牞錕介崶鐐茶剨鐎瑰本鍨�
if (pullDownY + Math.abs(pullUpY) == 0)
timer.cancel();
}

};

public void setOnRefreshListener(OnRefreshListener listener)
{
mListener = listener;
}

public PullToRefreshLayout(Context context)
{
super(context);
initView(context);
}

public PullToRefreshLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
initView(context);
}

public PullToRefreshLayout(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
initView(context);
}

private void initView(Context context)
{
mContext = context;
timer = new MyTimer(updateHandler);
rotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation(
context, R.anim.reverse_anim);
refreshingAnimation = (RotateAnimation) AnimationUtils.loadAnimation(
context, R.anim.rotating);
// 濞h濮為崠錕斤拷鏉烆剙濮╅崝銊ф暰
LinearInterpolator lir = new LinearInterpolator();
rotateAnimation.setInterpolator(lir);
refreshingAnimation.setInterpolator(lir);
}

private void hide()
{
timer.schedule(5);
}

/**
* 鐎瑰本鍨氶崚閿嬫煀閹垮秳綞旈敍灞炬▔緇�搫鍩溳弬鎵波閺嬫嚎錕藉▔銊﹀壈閿涙艾鍩溳弬鏉跨暚閹存劕鎮楁稉錕界暰鐟曚澆鐨熼悽銊ㄧ箹娑擃亝鏌熷▔錕�
*/
/**
* @param refreshResult
* PullToRefreshLayout.SUCCEED嫺狅綀銆冮幋鎰閿涘ullToRefreshLayout.FAIL嫺狅綀銆冩徑杈Е
*/
public void refreshFinish(int refreshResult)
{
refreshingView.clearAnimation();
refreshingView.setVisibility(View.GONE);
switch (refreshResult)
{
case SUCCEED:
// 閸掗攱鏌婇幋鎰
refreshStateImageView.setVisibility(View.VISIBLE);
refreshStateTextView.setText(R.string.refresh_succeed);
refreshStateImageView
.setBackgroundResource(R.drawable.refresh_succeed);
break;
case FAIL:
default:
// 閸掗攱鏌婃徑杈Е
refreshStateImageView.setVisibility(View.VISIBLE);
refreshStateTextView.setText(R.string.refresh_fail);
refreshStateImageView
.setBackgroundResource(R.drawable.refresh_failed);
break;
}
if (pullDownY > 0)
{
// 閸掗攱鏌婄紒鎾寸亯閸嬫粎鏆�緇夛拷
new Handler()
{
@Override
public void handleMessage(Message msg)
{
changeState(DONE);
hide();
}
}.sendEmptyMessageDelayed(0, 1000);
} else
{
changeState(DONE);
hide();
}
}

/**
* 閸旂姾嫺囩�灞劇槸閿涘本妯夌粈鍝勫鏉炵晫綺ㄩ弸婧匡拷濞夈劍鍓伴敍姘鏉炶棄鐣幋鎰倵娑擄拷鐣劇憰浣界殶閻€劏綣栨稉顏呮煙濞夛拷
*
* @param refreshResult
* PullToRefreshLayout.SUCCEED嫺狅綀銆冮幋鎰閿涘ullToRefreshLayout.FAIL嫺狅綀銆冩徑杈Е
*/
public void loadmoreFinish(int refreshResult)
{
loadingView.clearAnimation();
loadingView.setVisibility(View.GONE);
switch (refreshResult)
{
case SUCCEED:
// 閸旂姾嫺囬幋鎰
loadStateImageView.setVisibility(View.VISIBLE);
loadStateTextView.setText(R.string.load_succeed);
loadStateImageView.setBackgroundResource(R.drawable.load_succeed);
break;
case FAIL:
default:
// 閸旂姾嫺囨徑杈Е
loadStateImageView.setVisibility(View.VISIBLE);
loadStateTextView.setText(R.string.load_fail);
loadStateImageView.setBackgroundResource(R.drawable.load_failed);
break;
}
if (pullUpY < 0)
{
// 閸掗攱鏌婄紒鎾寸亯閸嬫粎鏆�緇夛拷
new Handler()
{
@Override
public void handleMessage(Message msg)
{
changeState(DONE);
hide();
}
}.sendEmptyMessageDelayed(0, 1000);
} else
{
changeState(DONE);
hide();
}
}

private void changeState(int to)
{
state = to;
switch (state)
{
case INIT:
// 娑撳濯虹敮鍐ㄧ湰閸掓繂顫愰悩鑸碉拷
refreshStateImageView.setVisibility(View.GONE);
refreshStateTextView.setText(R.string.pull_to_refresh);
pullView.clearAnimation();
pullView.setVisibility(View.VISIBLE);
// 娑撳﹥濯虹敮鍐ㄧ湰閸掓繂顫愰悩鑸碉拷
loadStateImageView.setVisibility(View.GONE);
loadStateTextView.setText(R.string.pullup_to_load);
pullUpView.clearAnimation();
pullUpView.setVisibility(View.VISIBLE);
break;
case RELEASE_TO_REFRESH:
// 闁插﹥鏂侀崚閿嬫煀閻樿埖錕�
refreshStateTextView.setText(R.string.release_to_refresh);
pullView.startAnimation(rotateAnimation);
break;
case REFRESHING:
// 濮濓絽婀崚閿嬫煀閻樿埖錕�
pullView.clearAnimation();
refreshingView.setVisibility(View.VISIBLE);
pullView.setVisibility(View.INVISIBLE);
refreshingView.startAnimation(refreshingAnimation);
refreshStateTextView.setText(R.string.refreshing);
break;
case RELEASE_TO_LOAD:
// 闁插﹥鏂侀崝鐘烘祰閻樿埖錕�
loadStateTextView.setText(R.string.release_to_load);
pullUpView.startAnimation(rotateAnimation);
break;
case LOADING:
// 濮濓絽婀崝鐘烘祰閻樿埖錕�
pullUpView.clearAnimation();
loadingView.setVisibility(View.VISIBLE);
pullUpView.setVisibility(View.INVISIBLE);
loadingView.startAnimation(refreshingAnimation);
loadStateTextView.setText(R.string.loading);
break;
case DONE:
// 閸掗攱鏌婇幋鏍у鏉炶棄鐣В鏇礉閸熴儵鍏樻稉宥呬粵
break;
}
}

/**
* 娑撳秹妾洪崚鏈電瑐閹峯鍨ㄦ稉瀣
*/
private void releasePull()
{
canPullDown = true;
canPullUp = true;
}

/*
* 閿涘牓娼�Javadoc閿涘鏁遍悥鑸靛付嫺犺泛鍠呯�姘Ц閸氾箑鍨庨崣鎴滅皚嫺犺鞝濋檮鍙夘剾嫺滃嬈㈤崘鑼崐
*
* @see android.view.ViewGroup#dispatchTouchEvent(android.view.MotionEvent)
*/
@Override
public boolean dispatchTouchEvent(MotionEvent ev)
{
switch (ev.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
downY = ev.getY();
lastY = downY;
timer.cancel();
mEvents = 0;
releasePull();
break;
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_POINTER_UP:
// 鏉╁洦鎶ゆ徑姘卞仯鐟欙婦顫�
mEvents = -1;
break;
case MotionEvent.ACTION_MOVE:
if (mEvents == 0)
{
if (pullDownY > 0
|| (((Pullable) pullableView).canPullDown()
&& canPullDown && state != LOADING))
{
// 閸欘墶浜掓稉瀣閿涘本顒滈崷銊ュ鏉炶姤妞傛稉宥堝厴娑撳濯�
// 鐎電懓鐤勯梽鍛撥閸斻劏紿涗粋璇蹭粵緙傗晛鐨敍宀勶拷閹存劗鏁ら崝娑欏閻ㄥ嫭鍔呯憴錕�
pullDownY = pullDownY + (ev.getY() - lastY) / radio;
if (pullDownY < 0)
{
pullDownY = 0;
canPullDown = false;
canPullUp = true;
}
if (pullDownY > getMeasuredHeight())
pullDownY = getMeasuredHeight();
if (state == REFRESHING)
{
// 濮濓絽婀崚閿嬫煀閻ㄥ嫭妞傞崐嬈捫曢幗鍝バ╅崝錕�
isTouch = true;
}
} else if (pullUpY < 0
|| (((Pullable) pullableView).canPullUp() && canPullUp && state != REFRESHING))
{
// 閸欘墶浜掓稉濠冨閿涘本顒滈崷銊ュ煕閺傜増妞傛稉宥堝厴娑撳﹥濯�
pullUpY = pullUpY + (ev.getY() - lastY) / radio;
if (pullUpY > 0)
{
pullUpY = 0;
canPullDown = true;
canPullUp = false;
}
if (pullUpY < -getMeasuredHeight())
pullUpY = -getMeasuredHeight();
if (state == LOADING)
{
// 濮濓絽婀崝鐘烘祰閻ㄥ嫭妞傞崐嬈捫曢幗鍝バ╅崝錕�
isTouch = true;
}
} else
releasePull();
} else
mEvents = 0;
lastY = ev.getY();
// 閺嶈宓佹稉瀣鐠烘繄顬囬弨鐟板綁濮f柧綬�
radio = (float) (2 + 2 * Math.tan(Math.PI / 2 / getMeasuredHeight()
* (pullDownY + Math.abs(pullUpY))));
if (pullDownY > 0 || pullUpY < 0)
requestLayout();
if (pullDownY > 0)
{
if (pullDownY <= refreshDist
&& (state == RELEASE_TO_REFRESH || state == DONE))
{
// 嬋″倹鐏夋稉瀣鐠烘繄顬囧▽隴鎻崚鏉垮煕閺傛壆孌戠捄嬋堫瀲娑撴柨綞嬮崜宥囧Ц閹焦妲擱柌濠冩杹閸掗攱鏌婇敍灞炬暭閸欐濮擱幀浣瘋礋娑撳濯洪崚閿嬫煀
changeState(INIT);
}
if (pullDownY >= refreshDist && state == INIT)
{
// 嬋″倹鐏夋稉瀣鐠烘繄顬囨潏鎯у煂閸掗攱鏌婇惃鍕獩緇傝絎栬ぐ鎾衝閻樿埖錕介弰顖氬灥嬋濮擱幀浣稿煕閺傚府鞝濋弨鐟板綁閻樿埖錕芥稉媧櫞閺�儳鍩溳弬錕�
changeState(RELEASE_TO_REFRESH);
}
} else if (pullUpY < 0)
{
// 娑撳娼伴弰顖氬灲閺儎厺絎傞幏澶婂鏉炵晫孌戦敍灞芥倱娑撳鞝濆▔銊﹀壈pullUpY閺勵垵紺嬮崐錕�
if (-pullUpY <= loadmoreDist
&& (state == RELEASE_TO_LOAD || state == DONE))
{
changeState(INIT);
}
// 娑撳﹥濯洪幙宥勭稊
if (-pullUpY >= loadmoreDist && state == INIT)
{
changeState(RELEASE_TO_LOAD);
}

}
// 閸ョ姳璐熼崚閿嬫煀閸滃苯濮炴潪鑺ユ惙嫺f粈絎夐懗鈃夋倱閺冩儼綣樼悰宀嬬礉閹碉拷浜抪ullDownY閸滃ullUpY娑撳秳鞝伴崥灞炬娑撳秳璐�閿涘苯媧滃銈堢箹闁插瞼鏁�pullDownY +
// Math.abs(pullUpY))鐏忓崬褰叉禒銉ょ瑝鐎電懓綞嬮崜宥囧Ц閹椒綞旈崠鍝勫瀻嫺滐拷
if ((pullDownY + Math.abs(pullUpY)) > 8)
{
// 闂冨弶顒涙稉瀣鏉╁洨鈻兼稉顓☆嚖鐟欙箑褰傞梹鎸庡瘻嫺滃嬈㈤崪宀�仯閸戣綈ㄦ禒錕�
ev.setAction(MotionEvent.ACTION_CANCEL);
}
break;
case MotionEvent.ACTION_UP:
if (pullDownY > refreshDist || -pullUpY > loadmoreDist)
// 濮濓絽婀崚閿嬫煀閺冭泛綬氭稉瀣閿涘牊顒滈崷銊ュ鏉炶姤妞傚錕界瑐閹峯鞝氶敍宀勫櫞閺�儳鎮楁稉瀣婢惰揪鞝欐稉濠冨婢惰揪鞝氭稉宥夋閽橈拷
{
isTouch = false;
}
if (state == RELEASE_TO_REFRESH)
{
changeState(REFRESHING);
// 閸掗攱鏌婇幙宥勭稊
if (mListener != null)
mListener.onRefresh(this);
} else if (state == RELEASE_TO_LOAD)
{
changeState(LOADING);
// 閸旂姾嫺囬幙宥勭稊
if (mListener != null)
mListener.onLoadMore(this);
}
hide();
default:
break;
}
// 嫺滃嬈㈤崚鍡楀絺嫺溿倗綺伴悥鍓佽
super.dispatchTouchEvent(ev);
return true;
}

/**
* @author chenjing 閼奉亜濮╁Ο鈩冨珯閹靛瀵氬鎴濆З閻ㄥ墖ask
*
*/
private class AutoRefreshAndLoadTask extends
AsyncTask<Integer, Float, String>
{

@Override
protected String doInBackground(Integer... params)
{
while (pullDownY < 4 / 3 * refreshDist)
{
pullDownY += MOVE_SPEED;
publishProgress(pullDownY);
try
{
Thread.sleep(params[0]);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
return null;
}

@Override
protected void onPostExecute(String result)
{
changeState(REFRESHING);
// 閸掗攱鏌婇幙宥勭稊
if (mListener != null)
mListener.onRefresh(PullToRefreshLayout.this);
hide();
}

@Override
protected void onProgressUpdate(Float... values)
{
if (pullDownY > refreshDist)
changeState(RELEASE_TO_REFRESH);
requestLayout();
}

}

/**
* 閼奉亜濮╅崚閿嬫煀
*/
public void autoRefresh()
{
AutoRefreshAndLoadTask task = new AutoRefreshAndLoadTask();
task.execute(20);
}

/**
* 閼奉亜濮╅崝鐘烘祰
*/
public void autoLoad()
{
pullUpY = -loadmoreDist;
requestLayout();
changeState(LOADING);
// 閸旂姾嫺囬幙宥勭稊
if (mListener != null)
mListener.onLoadMore(this);
}

private void initView()
{
// 閸掓繂顫愰崠鏍︾瑓閹峯絝風仦錕�
pullView = refreshView.findViewById(R.id.pull_icon);
refreshStateTextView = (TextView) refreshView
.findViewById(R.id.state_tv);
refreshingView = refreshView.findViewById(R.id.refreshing_icon);
refreshStateImageView = refreshView.findViewById(R.id.state_iv);
// 閸掓繂顫愰崠鏍︾瑐閹峯絝風仦錕�
pullUpView = loadmoreView.findViewById(R.id.pullup_icon);
loadStateTextView = (TextView) loadmoreView
.findViewById(R.id.loadstate_tv);
loadingView = loadmoreView.findViewById(R.id.loading_icon);
loadStateImageView = loadmoreView.findViewById(R.id.loadstate_iv);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
Log.d("Test", "Test");
if (!isLayout)
{
// 鏉╂瑩鍣烽弰顖滎儑娑擄拷顐兼潻娑欐降閻ㄥ嫭妞傞崐嬈忎粵娑擄拷綈洪崚嬋嗩潗閸栵拷
refreshView = getChildAt(0);
pullableView = getChildAt(1);
loadmoreView = getChildAt(2);
isLayout = true;
initView();
refreshDist = ((ViewGroup) refreshView).getChildAt(0)
.getMeasuredHeight();
loadmoreDist = ((ViewGroup) loadmoreView).getChildAt(0)
.getMeasuredHeight();
}
// 閺�懓褰夌�鎰付嫺犲墎孌戠敮鍐ㄧ湰閿涘矁綣栭柌宀�純閹恆儳鏁�pullDownY + pullUpY)嫺f粈璐熼崑蹇曅╅柌蹇ョ礉鏉╂瑦鐗辯嚲鍗炲講嫺犮儰絎夌�鐟扮秼閸撳秶濮擱幀浣風稊閸栧搫鍨�
refreshView.layout(0,
(int) (pullDownY + pullUpY) - refreshView.getMeasuredHeight(),
refreshView.getMeasuredWidth(), (int) (pullDownY + pullUpY));
pullableView.layout(0, (int) (pullDownY + pullUpY),
pullableView.getMeasuredWidth(), (int) (pullDownY + pullUpY)
+ pullableView.getMeasuredHeight());
loadmoreView.layout(0,
(int) (pullDownY + pullUpY) + pullableView.getMeasuredHeight(),
loadmoreView.getMeasuredWidth(),
(int) (pullDownY + pullUpY) + pullableView.getMeasuredHeight()
+ loadmoreView.getMeasuredHeight());
}

class MyTimer
{
private Handler handler;
private Timer timer;
private MyTask mTask;

public MyTimer(Handler handler)
{
this.handler = handler;
timer = new Timer();
}

public void schedule(long period)
{
if (mTask != null)
{
mTask.cancel();
mTask = null;
}
mTask = new MyTask(handler);
timer.schedule(mTask, 0, period);
}

public void cancel()
{
if (mTask != null)
{
mTask.cancel();
mTask = null;
}
}

class MyTask extends TimerTask
{
private Handler handler;

public MyTask(Handler handler)
{
this.handler = handler;
}

@Override
public void run()
{
handler.obtainMessage().sendToTarget();
}

}
}

/**
* 閸掗攱鏌婇崝鐘烘祰閸ョ偠鐨熼幒銉ュ經
*
* @author chenjing
*
*/
public interface OnRefreshListener
{
/**
* 閸掗攱鏌婇幙宥勭稊
*/
void onRefresh(PullToRefreshLayout pullToRefreshLayout);

/**
* 閸旂姾嫺囬幙宥勭稊
*/
void onLoadMore(PullToRefreshLayout pullToRefreshLayout);


}

}

  anima下的兩個文件

reverse_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="100"
    android:fillAfter="true"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="0"
    android:toDegrees="180" >

</rotate>

  rotating.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1500"
    android:fillAfter="true"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="-1"
    android:toDegrees="360" >

</rotate>

  load_more.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loadmore_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingBottom="20dp"
android:paddingTop="20dp" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >

<ImageView
android:id="@+id/pullup_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="60dp"
android:background="@drawable/pullup_icon_big" />

<ImageView
android:id="@+id/loading_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="60dp"
android:background="@drawable/loading"
android:visibility="gone" />

<TextView
android:id="@+id/loadstate_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/pullup_to_load"
android:textColor="@color/black"
android:textSize="16sp" />

<ImageView
android:id="@+id/loadstate_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:layout_toLeftOf="@id/loadstate_tv"
android:visibility="gone" />
</RelativeLayout>
</RelativeLayout>

</RelativeLayout>

  

  refresh_head.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/head_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/light_blue" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingBottom="20dp"
android:paddingTop="20dp" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >

<ImageView
android:id="@+id/pull_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="60dp"
android:background="@drawable/pull_icon_big" />

<ImageView
android:id="@+id/refreshing_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="60dp"
android:background="@drawable/refreshing"
android:visibility="gone" />

<TextView
android:id="@+id/state_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/pull_to_refresh"
android:textColor="@color/white"
android:textSize="16sp" />

<ImageView
android:id="@+id/state_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:layout_toLeftOf="@id/state_tv"
android:visibility="gone" />
</RelativeLayout>
</RelativeLayout>

</RelativeLayout>

  color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#FFFFFF</color>
    <color name="black">#000000</color>
    <color name="gray">#aaaaaa</color>
    <color name="light_blue">#6593cb</color>
</resources>

 Values 下的

  
    <string name="head">這裏是HeadView</string>
    <string name="pull_to_refresh">下拉刷新</string>
    <string name="release_to_refresh">釋放當即刷新</string>
    <string name="refreshing">正在刷新...</string>
    <string name="refresh_succeed">刷新成功</string>
    <string name="refresh_fail">刷新失敗</string>
    <string name="pullup_to_load">上拉加載更多</string>
    <string name="release_to_load">釋放當即加載</string>
    <string name="loading">正在加載...</string>
    <string name="load_succeed">加載成功</string>
    <string name="load_fail">加載失敗</string>
相關文章
相關標籤/搜索