# SCWang SmartRefreshLayout Dependency
###### tags: `Android Dependency`
[TOC]
# Introduction
By using this dependency, we can deal with refresh and load more for recycleviews and large pages.
# How to use it?
There are two versions of the SCWang SmartRefreshLayout, it is advisable to use the new one according to the provider.
**Old Package Name:**
com.scwang.smartrefresh
**New Package Name:**
com.scwang.smart.refresh
## Old Version
```java=
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0'
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0'
```
## New Version
```java=
//Scwang Smart Refresh Layout
def scwang_version = '2.0.3'
implementation "com.scwang.smart:refresh-layout-kernel:${scwang_version}" //core
implementation "com.scwang.smart:refresh-header-classics:${scwang_version}" //ClassicsHeader
implementation "com.scwang.smart:refresh-header-radar:${scwang_version}" //BezierRadarHeader
implementation "com.scwang.smart:refresh-header-falsify:${scwang_version}" //FalsifyHeader
implementation "com.scwang.smart:refresh-header-material:${scwang_version}" //MaterialHeader
implementation "com.scwang.smart:refresh-header-two-level:${scwang_version}" //TwoLevelHeader
implementation "com.scwang.smart:refresh-footer-ball:${scwang_version}" //BallPulseFooter
implementation "com.scwang.smart:refresh-footer-classics:${scwang_version}" //ClassicsFooter
```
## Tips to take into account
**AndroidX:**
Must add below lines in **gradle.properties**
```java=
android.useAndroidX = true
android.enableJetifier = true
```
**Must always:**
```java=
implementation 'androidx.appcompat:appcompat:1.0.0' //Above 1.0.0
```
# XML
## Old Version V.S New Version
In Old Version, there is **no need** to specify **header and footer**. While the new version, this action is needed.
## Attributes
**srlPrimaryColor:** Changes Header background and **++BallPulseFooter++** loading color.
**srlAccentColor:** Changes Header looper color
**srlEnablePreviewInEditMode:** Allows you to preview the header/footer design during development in **split || design view in XML**.
>**Note:** Do **NOT** apply normal background attribute as it will show double background when you are refreshing.[color=red]
## XML Example
```xml=
<?xml version="1.0" encoding="utf-8"?>
<com.scwang.smart.refresh.layout.SmartRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srlPrimaryColor="#2196F3"
app:srlAccentColor="@color/white"
app:srlEnablePreviewInEditMode="true">
<!--srlAccentColor and srlPrimaryColor, Will change the Header and Footer theme colors-->
<!--srlEnablePreviewInEditMode, Can open and close the preview function-->
<com.scwang.smart.refresh.header.BezierRadarHeader
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:background="@android:color/white"
android:text="HI I AM TEXT"/>
<com.scwang.smart.refresh.footer.BallPulseFooter
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.scwang.smart.refresh.layout.SmartRefreshLayout>
```
## XML Example Display

# setListener
There are 3 types of listener:
1. **setOnLoadMoreListener:** Called when reach bottom and try to keep scrolling.
1. **setOnRefreshListener:** Called when reach top and try to keep scrolling.
1. **setOnRefreshLoadMoreListener:** Will override both **onLoadMore & onRefresh**. So do **NOT** use this one with the other two listener, as they will be overwritten.
## setOnLoadMoreListener
```java=
refreshLayout.setOnLoadMoreListener(v -> {
Log.d("schwang", "setOnLoadMoreListener");
refreshLayout.finishLoadMore(true);
});
```
## setOnRefreshListener
```java=
refreshLayout.setOnRefreshListener(v -> {
Log.d("schwang", "setOnRefreshListener");
refreshLayout.finishRefresh(true);
});
```
## setOnRefreshLoadMoreListener
```java=
refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
Log.d("schwang", "onLoadMore");
refreshLayout.finishLoadMore(true);
}
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
Log.d("schwang", "onRefresh");
refreshLayout.finishRefresh(true);
}
});
```
## Enable Load More and Refresh
```java=
refreshLayout.setEnableLoadMore(true);
refreshLayout.setEnableRefresh(true);
```
# Reference
https://github.com/scwang90/SmartRefreshLayout
https://gitee.com/scwang90/SmartRefreshLayout/blob/master/README_EN.md
https://github.com/scwang90/SmartRefreshLayout/blob/master/README.md