Skip to content

the recyclerview which is able to pull to refresh and pull to load more

Notifications You must be signed in to change notification settings

whichname/PTLRecyclerView

Repository files navigation

PTLRecyclerView

the recyclerview which is able to pull to refresh and pull to load more

中文博客:http://blog.csdn.net/anyfive/article/details/53020321

Version

--1.1.1

  • bug fixed:
    • EmptyView will be invalid in PullToLoadRecyclerView;
    • there maybe some touch-event bug when use PTLRecyclerView with ViewPager;
  • extend:
    • when the items can't fill the RecyclerView, the LoadFooter will be invisible;
    • PullToLoadRecyclerView's loading-footer add nomore style;

--1.0.5

  • bug fixed:
    • sometimes there will has two refreshHeaders in recyclerview when init;
    • when add/remove header/footer or load more, the items will blink;
  • code tuning:
    • use completeLoad(int loadItemCount) instead of completeLoad() in PullToLoadRecyclerView and AutoLoadRecyclerView

How to use

in android-studio :

compile 'com.jimi_wu:PTLRecyclerView:1.1.1'

add/remove HeaderView or FooterView

use HeaderAndFooterRecyclerView :

HeaderAndFooterRecyclerView.addHeaderView(HeaderView);//add header
HeaderAndFooterRecyclerView.removeHeaderView(HeaderView);//remove header

HeaderAndFooterRecyclerView.addFooterView(FooterView);//add footer
HeaderAndFooterRecyclerView.removeFooterView(FooterView);//remove footer

HeaderAndFooterRecyclerView.setOnItemClickListener(OnItemClickListener onItemClickListener);//add onItemClickListener
HeaderAndFooterRecyclerView.setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener);//add onItemLongClickListener

头尾部

Grid头尾部

Staggred头尾部

PullToRefresh

use PullToRefreshRecyclerView :

PullToRefreshRecyclerView.setOnRefreshListener(OnRefreshListener onRefreshListener);//add OnRefreshListener

PullToRefreshRecyclerView.setRefreshViewCreator(RefreshHeaderCreator refreshHeaderCreator);//use your own RefreshHeaderView

PullToRefreshRecyclerView.setRefreshEnable(boolean refreshEnable);//set pull to refresh able/disable

PullToRefreshRecyclerView.setPullRatio(float pullRatio);

PullToRefreshRecyclerView.completeRefresh();//complete refresh

Generally,u just need to set OnRefreshListener ,and use '.completeRefresh()' when refresh completed.

PullToLoad

use PullToLoadRecyclerView :

PullToLoadRecyclerView.setOnLoadListener(OnLoadListener onLoadListener);//add OnLoadListener

PullToLoadRecyclerView.setLoadViewCreator(LoadFooterCreator loadViewCreator);//use your own LoadFooterView

PullToLoadRecyclerView.setLoadEnable(boolean loadMoreEnable);//set pull to load more able/disable

PullToLoadRecyclerView.setPullLoadRatio(float loadRatio);

PullToLoadRecyclerView.completeLoad(int loadItemCount);//complete load

U can use it just like use PullToRefreshRecyclerView.

Notice: if u want to use PullToLoadRecyclerView,use PTLLinearLayoutManager instead of LinearLayoutManager,use PTLGridLayoutManager instead of GridLayoutManager.

刷新加载

Grid刷新加载

Staggred刷新加载

AutoLoad

use AutoLoadRecyclerView :

AutoLoadRecyclerView.setOnLoadListener(OnLoadListener onLoadListener);//add OnLoadListener

AutoLoadRecyclerView.setAutoLoadViewCreator(AutoLoadFooterCreator autoLoadFooterCreator);//use your own LoadFooterView

AutoLoadRecyclerView.completeLoad(int loadItemCount);//complete load

AutoLoadRecyclerView.setNoMore(boolean noMore);//set has no more data

Grid自动加载

use your own Refresh-Header-View

  1. New a class extends RefreshHeaderCreator
  2. Override five functions:
/**
* @param distance:the distance of pull
* @param lastState
* @return able to continue pull?
*/
public abstract boolean onStartPull(float distance, int lastState);

/**
* @param distance:the distance of pull
* @param lastState
* @return able to continue release?
*/
public abstract boolean onReleaseToRefresh(float distance,int lastState);

/**
*call back when start refresh
*/
public abstract void onStartRefreshing();

/**
*callback when complete refresh
*/
public abstract void onStopRefresh();

/**
*return the refresh headerview
*/
public abstract View getRefreshView(Context context,RecyclerView recyclerView);

  1. Call PullToRefreshRecyclerView.setOnRefreshViewCreator.

Example:

public class DefaultRefreshHeaderCreator extends RefreshHeaderCreator {

    private View mRefreshView;
    private ImageView iv;
    private TextView tv;

    private int rotationDuration = 200;

    private int loadingDuration = 1000;
    private ValueAnimator ivAnim;

    @Override
    public boolean onStartPull(float distance,int lastState) {
        if (lastState == PullToRefreshRecyclerView.STATE_DEFAULT ) {
            iv.setImageResource(R.drawable.arrow_down);
            iv.setRotation(0f);
            tv.setText("pull to refresh");
        } else if (lastState == PullToRefreshRecyclerView.STATE_RELEASE_TO_REFRESH) {
            startArrowAnim(0);
            tv.setText("pull to refresh");
        }
        return true;
    }

    @Override
    public void onStopRefresh() {
        if (ivAnim != null) {
            ivAnim.cancel();
        }
    }

    @Override
    public boolean onReleaseToRefresh(float distance,int lastState) {
        if (lastState == PullToRefreshRecyclerView.STATE_DEFAULT ) {
            iv.setImageResource(R.drawable.arrow_down);
            iv.setRotation(-180f);
            tv.setText("release to refresh");
        } else if (lastState == PullToRefreshRecyclerView.STATE_PULLING) {
            startArrowAnim(-180f);
            tv.setText("release to refresh");
        }
        return true;
    }

    @Override
    public void onStartRefreshing() {
        iv.setImageResource(R.drawable.loading);
        startLoadingAnim();
        tv.setText("Refreshing...");
    }

    @Override
    public View getRefreshView(Context context, RecyclerView recyclerView) {
        mRefreshView = LayoutInflater.from(context).inflate(R.layout.layout_ptr_ptl,recyclerView,false);
        iv = (ImageView) mRefreshView.findViewById(R.id.iv);
        tv = (TextView) mRefreshView.findViewById(R.id.tv);
        return mRefreshView;
    }

    private void startArrowAnim(float roration) {
        if (ivAnim != null) {
            ivAnim.cancel();
        }
        float startRotation = iv.getRotation();
        ivAnim = ObjectAnimator.ofFloat(startRotation,roration).setDuration(rotationDuration);
        ivAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                iv.setRotation((Float) animation.getAnimatedValue());
            }
        });
        ivAnim.start();
    }

    private void startLoadingAnim() {
        if (ivAnim != null) {
            ivAnim.cancel();
        }
        ivAnim = ObjectAnimator.ofFloat(0,360).setDuration(loadingDuration);
        ivAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                iv.setRotation((Float) animation.getAnimatedValue());
            }
        });
        ivAnim.setRepeatMode(ObjectAnimator.RESTART);
        ivAnim.setRepeatCount(ObjectAnimator.INFINITE);
        ivAnim.setInterpolator(new LinearInterpolator());
        ivAnim.start();
    }

}

user your own Load-Footer-View:

  1. New a class extends LoadFooterCreator;
  2. Override six fuctions:
/**
* @param distance:the distance of pull
* @param lastState
* @return able to continue pull?
*/
public abstract boolean onStartPull(float distance, int lastState);

/**
* @param distance:the distance of pull
* @param lastState
* @return able to continue release?
*/
public abstract boolean onReleaseToRefresh(float distance,int lastState);

/**
*call back when start load
*/
protected abstract void onStartLoading();

/**
*callback when complete load
*/
public abstract void onStopRefresh();

/**
*return the load footerview
*/
protected abstract View getLoadView(Context context, RecyclerView recyclerView);

/**
*return the nomore footerview
*/
protected View getNoMoreView(Context context, RecyclerView recyclerView) {return null;}

  1. Call PullToLoadRecyclerView.setLoadViewCreator.

Example:

public class DefaultLoadFooterCreator extends LoadFooterCreator {

    private View mLoadView;
    private ImageView iv;
    private TextView tv;

    private int rotationDuration = 200;

    private int loadingDuration = 1000;
    private ValueAnimator ivAnim;

    @Override
    public boolean onStartPull(float distance, int lastState) {
        if (lastState == PullToLoadRecyclerView.STATE_DEFAULT) {
            iv.setImageResource(R.drawable.arrow_down);
            iv.setRotation(-180f);
            tv.setText("pull to load");
        } else if (lastState == PullToLoadRecyclerView.STATE_RELEASE_TO_LOAD) {
            startArrowAnim(-180f);
            tv.setText("pull to load");
        }
        return true;
    }

    @Override
    public boolean onReleaseToLoad(float distance, int lastState) {
        if (lastState == PullToLoadRecyclerView.STATE_DEFAULT ) {
            iv.setImageResource(R.drawable.arrow_down);
            iv.setRotation(0f);
            tv.setText("release to load more");
        } else if (lastState == PullToLoadRecyclerView.STATE_PULLING) {
            startArrowAnim(0f);
            tv.setText("release to load more");
        }
        return true;
    }

    @Override
    public void onStartLoading() {
        iv.setImageResource(R.drawable.loading);
        startLoadingAnim();
        tv.setText("Loading...");
    }

    @Override
    public void onStopLoad() {
        if (ivAnim != null) {
            ivAnim.cancel();
        }
    }

    @Override
    public View getLoadView(Context context, RecyclerView recyclerView) {
        mLoadView = LayoutInflater.from(context).inflate(R.layout.layout_ptr_ptl,recyclerView,false);
        iv = (ImageView) mLoadView.findViewById(R.id.iv);
        tv = (TextView) mLoadView.findViewById(R.id.tv);
        return mLoadView;
    }

    private void startArrowAnim(float roration) {
        if (ivAnim != null) {
            ivAnim.cancel();
        }
        float startRotation = iv.getRotation();
        ivAnim = ObjectAnimator.ofFloat(startRotation, roration).setDuration(rotationDuration);
        ivAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                iv.setRotation((Float) animation.getAnimatedValue());
            }
        });
        ivAnim.start();
    }

    private void startLoadingAnim() {
        if (ivAnim != null) {
            ivAnim.cancel();
        }
        ivAnim = ObjectAnimator.ofFloat(0,360).setDuration(loadingDuration);
        ivAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                iv.setRotation((Float) animation.getAnimatedValue());
            }
        });
        ivAnim.setRepeatMode(ObjectAnimator.RESTART);
        ivAnim.setRepeatCount(ObjectAnimator.INFINITE);
        ivAnim.setInterpolator(new LinearInterpolator());
        ivAnim.start();
    }

}

use your own AutoLoad-Footer-View

  1. New a class extends AutoLoadFooterCreator;
  2. Override two fuctions:
/***
 * return the autoload footer view
 */
protected abstract View getLoadView(Context context, RecyclerView recyclerView);

/***
 * return the nomore footer view
 */
protected abstract View getNoMoreView(Context context,RecyclerView recyclerView);

  1. Call AutoLoadRecyclerView.setAutoLoadViewCreator.

Example:

public class DefaultAutoLoadFooterCreator extends AutoLoadFooterCreator {

    protected View mAutoLoadFooter;
    protected ImageView iv;
    protected ValueAnimator ivAnim;
    private int loadingDuration = 1000;

    @Override
    protected View getLoadView(Context context, RecyclerView recyclerView) {
        if (mAutoLoadFooter == null) {
            mAutoLoadFooter = LayoutInflater.from(context).inflate(R.layout.layout_auto_load_footer,recyclerView,false);
            iv = (ImageView) mAutoLoadFooter.findViewById(R.id.iv);
            startLoadingAnim();
        }
        return mAutoLoadFooter;
    }

    @Override
    protected View getNoMoreView(Context context, RecyclerView recyclerView) {
        return null;
    }

    private void startLoadingAnim() {
        if (ivAnim != null) {
            ivAnim.cancel();
        }
        ivAnim = ObjectAnimator.ofFloat(0, 360).setDuration(loadingDuration);
        ivAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                iv.setRotation((Float) animation.getAnimatedValue());
            }
        });
        ivAnim.setRepeatMode(ObjectAnimator.RESTART);
        ivAnim.setRepeatCount(ObjectAnimator.INFINITE);
        ivAnim.setInterpolator(new LinearInterpolator());
        ivAnim.start();
    }

}

About

the recyclerview which is able to pull to refresh and pull to load more

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages