关闭

pullGridView

101人阅读 评论(0) 收藏 举报
package com.bwie.wanghang.pullToRefresh;

public interface Pullable
{
    
    /**
     * 判断是否可以下拉,如果不需要下拉功能可以直接return false
     *
     * @ return true如果可以下拉否则返回false
     */
    boolean canPullDown();

    /**
     * 判断是否可以上拉,如果不需要上拉功能可以直接return false
     *
     * @ return true如果可以上拉否则返回false
     */
    boolean canPullUp();
}







package com.bwie.wanghang.pullToRefresh;
 
import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;
 
public class PullableGridView extends GridView implements Pullable
{
 
    public PullableGridView(Context context)
    {
        super(context);
    }
 
    public PullableGridView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }
 
    public PullableGridView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }
 
    @Override
    public boolean canPullDown()
    {
        if (getCount() == 0)
        {
            // 没有item的时候也可以下拉刷新
            return true;
        } else if (getFirstVisiblePosition() == 0
                && getChildAt(0).getTop() >= 0)
        {
            // 滑到顶部了
            return true;
        } else
            return false;
    }
 
    @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;
    }
 
}



package com.bwie.wanghang.pullToRefresh;


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

import com.text.text.R;



 
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
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;
 
 
/**
 * 閼奉亜鐣炬稊澶屾畱鐢啫鐪敍宀�鏁ら弶銉ь吀閻炲棔绗佹稉顏勭摍閹貉傛閿涘苯鍙炬稉顓濈娑擃亝妲告稉瀣婢惰揪绱濇稉锟芥稉顏呮Ц閸栧懎鎯堥崘鍛啇閻ㄥ埦ullableView閿涘牆褰叉禒銉︽Ц鐎圭偟骞嘝ullable閹恒儱褰涢惃鍕畱娴犺缍峍iew閿涘绱�
 * 鏉╂ɑ婀佹稉锟芥稉顏冪瑐閹峰銇�
 *
 * @author 闂勫牓娼�
 */
public class PullToRefreshLayout extends RelativeLayout
{
    public static final String TAG = "";
    // 閸掓繂顫愰悩鑸碉拷锟�
    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敍灞肩瑐娑擄拷娑擃亙绨ㄦ禒鍓佸仯Y閸ф劖鐖�
    private float downY, lastY;
 
    // 娑撳濯洪惃鍕獩缁傛眹锟藉倹鏁為幇蹇ョ窗pullDownY閸滃ullUpY娑撳秴褰查懗钘夋倱閺冩湹绗夋稉锟�0
    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;
 
    // 娑撳濯虹粻顓炪仈閻ㄥ嫯娴�180鎺抽崝銊ф暰
    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;
 
    /**
     * 閹笛嗩攽閼奉亜濮╅崶鐐寸泊閻ㄥ埅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();
            }
            if (pullUpY > 0)
            {
                // 瀹告彃鐣幋鎰礀瀵拷
                pullUpY = 0;
                pullUpView.clearAnimation();
                // 闂呮劘妫屾稉瀣婢跺瓨妞傞張澶婂讲閼冲�熺箷閸︺劌鍩涢弬甯礉閸欘亝婀佽ぐ鎾冲閻樿埖锟戒椒绗夐弰顖涱劀閸︺劌鍩涢弬鐗堟閹靛秵鏁奸崣妯煎Ц閹拷
                if (state != REFRESHING && state != LOADING)
                    changeState(INIT);
                timer.cancel();
            }
            // 閸掗攱鏌婄敮鍐ㄧ湰,娴兼俺鍤滈崝銊ㄧ殶閻⑩暙nLayout
            requestLayout();
        }
 
    };
 
    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)
    {
        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("姝e湪鍔犺浇");
            refreshStateImageView
                    .setBackgroundResource(R.drawable.refresh_succeed);
            break;
        case FAIL:
        default:
            // 閸掗攱鏌婃径杈Е
            refreshStateImageView.setVisibility(View.VISIBLE);
            refreshStateTextView.setText("姝e湪鍔犺浇");
            refreshStateImageView
                    .setBackgroundResource(R.drawable.refresh_failed);
            break;
        }
        // 閸掗攱鏌婄紒鎾寸亯閸嬫粎鏆�1缁夛拷
        new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                changeState(DONE);
                hide();
            }
        }.sendEmptyMessageDelayed(0, 1000);
    }
 
    /**
     * 閸旂姾娴囩�瑰本鐦敍灞炬▔缁�鍝勫鏉炵晫绮ㄩ弸婧匡拷鍌涙暈閹板骏绱伴崝鐘烘祰鐎瑰本鍨氶崥搴濈鐎规俺顩︾拫鍐暏鏉╂瑤閲滈弬瑙勭《
     *
     * @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("正在刷新请稍后");
            loadStateImageView.setBackgroundResource(R.drawable.load_succeed);
            break;
        case FAIL:
        default:
            // 閸旂姾娴囨径杈Е
            loadStateImageView.setVisibility(View.VISIBLE);
            loadStateTextView.setText("正在刷新请稍后");
            loadStateImageView.setBackgroundResource(R.drawable.load_failed);
            break;
        }
        // 閸掗攱鏌婄紒鎾寸亯閸嬫粎鏆�1缁夛拷
        new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                changeState(DONE);
                hide();
            }
        }.sendEmptyMessageDelayed(0, 1000);
    }
 
    private void changeState(int to)
    {
        state = to;
        switch (state)
        {
        
        case INIT:
            // 娑撳濯虹敮鍐ㄧ湰閸掓繂顫愰悩鑸碉拷锟�
            refreshStateImageView.setVisibility(View.GONE);
            refreshStateTextView.setText("正在刷新请稍后");
            pullView.clearAnimation();
            pullView.setVisibility(View.VISIBLE);
            // 娑撳﹥濯虹敮鍐ㄧ湰閸掓繂顫愰悩鑸碉拷锟�
            loadStateImageView.setVisibility(View.GONE);
            loadStateTextView.setText("正在刷新请稍后");
            pullUpView.clearAnimation();
            pullUpView.setVisibility(View.VISIBLE);
            break;
        case RELEASE_TO_REFRESH:
            // 闁插﹥鏂侀崚閿嬫煀閻樿埖锟斤拷
            refreshStateTextView.setText("正在刷新请稍后");
            pullView.startAnimation(rotateAnimation);
            break;
        case REFRESHING:
            // 濮濓絽婀崚閿嬫煀閻樿埖锟斤拷
            pullView.clearAnimation();
            refreshingView.setVisibility(View.VISIBLE);
            pullView.setVisibility(View.INVISIBLE);
            refreshingView.startAnimation(refreshingAnimation);
            refreshStateTextView.setText("正在刷新请稍后");
            break;
        case RELEASE_TO_LOAD:
            // 闁插﹥鏂侀崝鐘烘祰閻樿埖锟斤拷
            loadStateTextView.setText("正在刷新请稍后");
            pullUpView.startAnimation(rotateAnimation);
            break;
        case LOADING:
            // 濮濓絽婀崝鐘烘祰閻樿埖锟斤拷
            pullUpView.clearAnimation();
            loadingView.setVisibility(View.VISIBLE);
            pullUpView.setVisibility(View.INVISIBLE);
            loadingView.startAnimation(refreshingAnimation);
            loadStateTextView.setText("正在刷新请稍后");
            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 (((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 (((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))));
            requestLayout();
            if (pullDownY <= refreshDist && state == RELEASE_TO_REFRESH)
            {
                // 婵″倹鐏夋稉瀣鐠烘繄顬囧▽陇鎻崚鏉垮煕閺傛壆娈戠捄婵堫瀲娑撴柨缍嬮崜宥囧Ц閹焦妲搁柌濠冩杹閸掗攱鏌婇敍灞炬暭閸欐濮搁幀浣疯礋娑撳濯洪崚閿嬫煀
                changeState(INIT);
            }
            if (pullDownY >= refreshDist && state == INIT)
            {
                // 婵″倹鐏夋稉瀣鐠烘繄顬囨潏鎯у煂閸掗攱鏌婇惃鍕獩缁傝绗栬ぐ鎾冲閻樿埖锟戒焦妲搁崚婵嗩潗閻樿埖锟戒礁鍩涢弬甯礉閺�鐟板綁閻樿埖锟戒椒璐熼柌濠冩杹閸掗攱鏌�
                changeState(RELEASE_TO_REFRESH);
            }
            // 娑撳娼伴弰顖氬灲閺傤厺绗傞幏澶婂鏉炵晫娈戦敍灞芥倱娑撳绱濆▔銊﹀壈pullUpY閺勵垵绀嬮崐锟�
            if (-pullUpY <= loadmoreDist && state == RELEASE_TO_LOAD)
            {
                changeState(INIT);
            }
            if (-pullUpY >= loadmoreDist && state == INIT)
            {
                changeState(RELEASE_TO_LOAD);
            }
            // 閸ョ姳璐熼崚閿嬫煀閸滃苯濮炴潪鑺ユ惙娴f粈绗夐懗钘夋倱閺冩儼绻樼悰宀嬬礉閹碉拷娴狀櫠ullDownY閸滃ullUpY娑撳秳绱伴崥灞炬娑撳秳璐�0閿涘苯娲滃銈堢箹闁插瞼鏁�(pullDownY +
            // Math.abs(pullUpY))鐏忓崬褰叉禒銉ょ瑝鐎电懓缍嬮崜宥囧Ц閹椒缍旈崠鍝勫瀻娴滐拷
            if ((pullDownY + Math.abs(pullUpY)) > 8)
            {
                // 闂冨弶顒涙稉瀣鏉╁洨鈻兼稉顓☆嚖鐟欙箑褰傞梹鎸庡瘻娴滃娆㈤崪宀�鍋i崙璁崇皑娴狅拷
                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;
    }
 
    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)
    {
        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);
    }
 
}







package com.example.gridviewpull;

import java.util.LinkedList;

import com.bwie.wanghang.pullToRefresh.PullToRefreshLayout;
import com.bwie.wanghang.pullToRefresh.PullToRefreshLayout.OnRefreshListener;
import com.bwie.wanghang.pullToRefresh.PullableGridView;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class MainActivity extends Activity {

    private PullToRefreshLayout layout;
    private PullableGridView lv;
    private LinkedList<String> list;
    private ArrayAdapter<String> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        layout = (PullToRefreshLayout) findViewById(R.id.layout);
        
        lv = (PullableGridView) findViewById(R.id.stview);
        
        list=new LinkedList<String>();
        
        list.add("1");
        
        list.add("2");
        
        list.add("3");
        
        list.add("4");
        
        list.add("5");
        
        adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, list);
        
        lv.setAdapter(adapter);
        
        layout.setOnRefreshListener(new OnRefreshListener() {
            
            /**
             * 刷新操作
             */
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                
                list.addFirst("AA==");
                list.addFirst("BBB===");
                list.addFirst("CCCC====");    
                
                adapter.notifyDataSetChanged();
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }

            /**
             * 加载操作
             */
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                list.add("TTT----");                
                list.add("KKK+++");
                adapter.notifyDataSetChanged();
                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }
        });    
    }
}


anim

reverse_anim

<?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 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>

main

<com.bwie.wanghang.pullToRefresh.PullToRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <include layout="@layout/refresh_head" />

    <com.bwie.wanghang.pullToRefresh.PullableGridView
          android:numColumns="3"
        android:id="@+id/stview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </com.bwie.wanghang.pullToRefresh.PullableGridView>

    <include layout="@layout/load_more" />

</com.bwie.wanghang.pullToRefresh.PullToRefreshLayout>




load_more


<?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="#aaaaaa" >

    <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="#000000"
                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 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="#6593cb" >

    <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="#FFFFFF"
                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>




String

 <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>
    <string name="title_activity_xiang">XiangActivity</string>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:28822次
    • 积分:782
    • 等级:
    • 排名:千里之外
    • 原创:44篇
    • 转载:46篇
    • 译文:0篇
    • 评论:3条
    最新评论