Here you go:
Step 1: Gradle dependency
compile 'com.reginald.swiperefresh: library: 1.1.1'
Step 2: xml config
<com.reginald.swiperefresh.CustomSwipeRefreshLayout
xmlns:swiperefresh="http://schemas.android.com/apk/res-auto"
android:id="@+id/swipelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
swiperefresh:refresh_mode="pull_mode"
swiperefresh:keep_refresh_head="true"
swiperefresh:enable_top_progress_bar="true"
swiperefresh:time_out_refresh_complete="2000"
swiperefresh:time_out_return_to_top="1000"
swiperefresh:return_to_top_duration="500"
swiperefresh:return_to_header_duration="500"
swiperefresh:top_progress_bar_color_1="@color/common_red"
swiperefresh:top_progress_bar_color_2="#ee5522"
swiperefresh:top_progress_bar_color_3="#ffa600"
swiperefresh:top_progress_bar_color_4="@color/common_yellow">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:id="@+id/listview">
</ListView>
</com.reginald.swiperefresh.CustomSwipeRefreshLayout>
Step 3: Java Code:
// Set a custom HeadView. use default HeadView if not provided
mCustomSwipeRefreshLayout.setCustomHeadview(new MyCustomHeadView(this));
// Set refresh mode to swipe mode
// (CustomSwipeRefreshLayout.REFRESH_MODE_PULL or CustomSwipeRefreshLayout.REFRESH_MODE_SWIPE)
mSwipeRefreshLayout.setRefreshMode(CustomSwipeRefreshLayout.REFRESH_MODE_SWIPE);
// Enable the top progress bar
mSwipeRefreshLayout.enableTopProgressBar(true);
// Keep the refreshing head movable(true stands for fixed) on the top
mSwipeRefreshLayout.enableTopRefreshingHead(false);
// Timeout to return to original state when the swipe motion stay in the same position
mSwipeRefreshLayout.setmReturnToOriginalTimeout(200);
// Timeout to show the refresh complete information on the refreshing head.
mSwipeRefreshLayout.setmRefreshCompleteTimeout(1000);
// Duration of the animation from the top of the content view to parent top.(e.g. when refresh complete)
mCustomSwipeRefreshLayout.setReturnToTopDuration(500);
// Duration of the animation from the top of the content view to the height of header.(e.g. when content view is released)
mCustomSwipeRefreshLayout.setReturnToHeaderDuration(500);
// Set progress bar colors
mSwipeRefreshLayout.setProgressBarColor(color1, color2,color3, color4);
// Set the height of Progress bar, in dp. Default is 4dp
mCustomSwipeRefreshLayout.setProgressBarHeight(4);
// Set the resistance factor. Default is 0.5f
mCustomSwipeRefreshLayout.setResistanceFactor(0.5f);
// Set the trigger distance. in dp. Default is 100dp
// (pull -> release distance for PULL mode or swipe refresh distance for SWIPE mode)
mCustomSwipeRefreshLayout.setTriggerDistance(100);
Step 4: Handle the Update Event
CustomSwipeRefreshLayout mSwipeRefreshLayout;
mSwipeRefreshLayout.setOnRefreshListener(new CustomSwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
}
});
mCustomSwipeRefreshLayout.setRefreshCheckHandler(new CustomSwipeRefreshLayout.RefreshCheckHandler() {
@Override
public boolean canRefresh() {
}
});
mSwipeRefreshLayout.refreshComplete();
Step 5: Handle the scroll event
mCustomSwipeRefreshLayout.setScrollUpHandler(new CustomSwipeRefreshLayout.ScrollUpHandler() {
@Override
public boolean canScrollUp(View view) {
if (view == mRecyclerView){
return ((GridLayoutManager)mLayoutManager).findFirstCompletelyVisibleItemPosition() != 0;
}
return false;
}
});
mCustomSwipeRefreshLayout.setScrollLeftOrRightHandler(new CustomSwipeRefreshLayout.ScrollLeftOrRightHandler() {
@Override
public boolean canScrollLeftOrRight(View view, int direction) {
if (view == myCustomView){
return myCustomView.canScrollHorizontal(direction);
}
return false;
}
});
I just wanted to post the link, but SOF doesn't let me do this. Here is the link anyway https://github.com/xyxyLiu/SwipeRefreshLayout
source
share