The company has been using the official SwipeRefreshLayout, which works well but has poor scalability. After searching for the drop-down refresh control on the market, the company has two pages that need to support Nested scrolling. More than once, the product manager had the idea of modifying the drop-down refresh animation, which was reluctantly postponed because the following pull refresh control was not good. However, the problem was always solved. Without wheels, we built one by ourselves. Github address: github.com/zhangxq/QRe… .

Introduction to the

QRefreshLayout references SwipeRefreshLayout, so it is used in the same way as SwipeRefreshLayout, and adds more functions of pull-up loading, supports refreshing of all View subclasses, and supports Nested scrolling. Support custom header and footer, support to pull down to the second floor, friendly interface, convenient and simple.

Download the demo

Download the demo

Results show

use

	allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io'}}}Copy the code
	dependencies {
		implementation 'com. Making. Zhangxq: QRefreshLayout: 1.0.8'
	}
Copy the code
<? xml version="1.0" encoding="utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.zhangxq.refreshlayout.QRefreshLayout
        android:id="@+id/refreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </com.zhangxq.refreshlayout.RefreshLayout>
</LinearLayout>
Copy the code
  refreshLayout.setOnRefreshListener(this);
  refreshLayout.setOnLoadListener(this);
Copy the code

For more interface usage, please see demo source code.

Interface specification

The name of the function
setOnRefreshListener Set the drop-down refresh listener
setOnLoadListener Set up to load more listeners
setRefreshing Turn on or off the drop-down refresh animation
setLoading Load more animations on or off
setColorSchemeResources setColorSchemeColors Set the default pull-down refresh progress circle color
setProgressBackgroundColorSchemeResource setProgressBackgroundColorSchemeResource Set the default drop-down refresh progress circle background color
setRefreshView Set the drop-down refresh view
setLoadView Set to load more views
setLoadEnable To set the load more switch, setOnLoadListener is enabled by default
setAutoLoad Set more automatic loading switches. SetOnLoadListener is enabled by default
setListViewScrollListener Set the ListView’s scrolllistener.
setPullToRefreshHeight Set drop down to “Free to Update” height (default 170px)
setLoadToRefreshHeight Set drop to “release to load more” height (default 170px)
setRefreshHeight Set drop-down refresh animation height (default: 150px, need to be called before setRefreshing)
setLoadHeight Set the loading height of more animations (default: 110px)
setIsCanSecondFloor Set whether you can reach the second floor
setSecondFloorView Set the second-floor view only if the default header is used
isSecondFloor Is it currently on the second floor
setBackToFirstFloor Back to the first floor
setPullToSecondFloorHeight Set drop down to “release to second floor” height (default 500px)

Custom header and footer

The setRefreshView and setLoadView methods receive user-defined headers and footers. SetRefreshView receives a view inherited from RefreshView. SetLoadView receives a view inherited from LoadView. The difference between RefreshView and LoadView is that there are three more virtual methods for RefreshView than LoadView.

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.RelativeLayout;

import com.zhangxq.refreshlayout.defaultview.Refresh;

/**
 * Created by zhangxiaoqi on 2019/4/22.
 */

public abstract class RefreshView extends RelativeLayout implements Refresh {
    public RefreshView(Context context) {
        this(context, null);
    }

    public RefreshView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setGravity(Gravity.CENTER); } /** * public void */ public voidsetReleaseToSecondFloor(); /** * public voidsetToSecondFloor(); /** * public voidsetToFirstFloor();
}
Copy the code

Look at the annotations for the method to see the corresponding functionality. RefreshView and LoadView both implement a Refresh interface as follows:

Public interface Refresh {/** ** drag ** @param height display area height * @param refreshHeight drop down to trigger the Refresh location * @param TotalHeight Total display area height */ voidsetHeight(float height, float refreshHeight, floattotalHeight); /** * trigger refresh */ voidsetRefresh(); /** * drop down to refresh */ voidsetPullToRefresh(); /** * to refresh */ voidsetRefeaseToRefresh();
}
Copy the code

As you can see, the interface provides three callback methods corresponding to the three points in time commonly used in the pull-down or pull-up process, and a setHeight method provides the distance of the finger drag to facilitate the user to process the drag animation. Inherit RefreshView or LoadView to override these four methods, and you can easily achieve the animation you want.

Pull down to the second floor function

With the default header, the following two lines will pull you down to the second floor. The view argument in the second line is the view you want to display on the second floor.

qRefreshLayout.setIsCanSecondFloor(true);
qRefreshLayout.setSecondFloorView(view);
Copy the code

With a custom header, you can remove the second line, and then use the three callback methods in the custom header to achieve your own second-floor effect.

onemore

If you just want to add more loading functions to SwipeRefreshLayout, check out my other blog post: Inherit SwipeRefreshLayout for more loading. If this post helps you, please give my Github a little star, thank you.