android 下拉刷新控件

Implement the effect of pull to refresh.

Usage

Add it in your root build.gradle at the end of repositories:

allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
        }
    }

Add the dependency

dependencies {
            compile 'com.github.RainbleNi:PullToRefresh:0.0.1'
    }

Usage in xml

For example, add a grid view in layout

    
       

in upper case, we only add the content view in the layout, so header view is in default style.

We can also define your own header layout.



       
    
       

We will automatically identify the first child as header and second child as content view.

You can also define header or content in layout xml:


If you content include a scroll view (ListView, GridView, ScrollView),but the scroll view is not the root of the content. In order to perform currently, you should assign the scroll view in xml:

ptf:scroll_id="@+id/listview"

Usage in java

Register refresh callback

ptfLayout = (PullToRefreshLayout) root.findViewById(R.id.ptf_layout);
ptfLayout.setRefreshCallback(new PullToRefreshLayout.RefreshCallback() {  
  
  @Override    
  public void onRefresh() {
        //do refresh
  }
});

After refresh completed, you should notify the ui:

ptfLayout.refreshComplete();

If your header view is change along with the refreshing state, you should register the HeaderUICallback:

ptfLayout.setHeaderUICallback(new PullToRefreshLayout.HeaderUICallback() {    
    @Override
    public void onStatePullToRefresh() {
        headView.setText("Pull to refresh");    
    }   
    @Override
    public void onStateReleaseToRefresh() {
        headView.setText("Release to refresh");
    }
    @Override
    public void onStateRefreshing() {
        headView.setText("In refreshing");
    }
    @Override
    public void onStateComplete() {
        headView.setText("Refresh completed");
    }
});

Some extend function:

//start auto refresh without MotionEvent
ptflayout.autoRefresh()
//set Animation duration
{@link #setScrollAnimationDuration(int)}
//set refresh critical position
{@link #setRefreshingLine(float)}
//set the coefficient of friction of pull to refresh action
{@link #setCoefficientOfFriction(float)}

Advantage

1 easily to use
2 more effectively and smoothly

Gif

ScreenRecord_2016-05-24-20-05-32.gif

github地址:https://github.com/RainbleNi/PullToRefresh

你可能感兴趣的:(android 下拉刷新控件)