10 |
--------------------------------------------------------------------------------
/lib/src/androidTest/java/com/orangegangsters/github/swiperefreshlayout/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.orangegangsters.github.swiperefreshlayout;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 |
3 | # Built application files
4 | *.apk
5 | *.ap_
6 |
7 | # Files for the Dalvik VM
8 | *.dex
9 |
10 | # Java class files
11 | *.class
12 |
13 | # Generated files
14 | bin/
15 | gen/
16 |
17 | # Gradle files
18 | .gradle/
19 | .idea
20 | build/
21 |
22 | # Local configuration file (sdk path, etc)
23 | local.properties
24 |
25 | # Proguard folder generated by Eclipse
26 | proguard/
27 |
28 | # Log Files
29 | *.log
30 |
31 | # Android Studio
32 | .idea
33 | #.idea/workspace.xml - remove # and delete .idea if it better suit your needs.
34 | .gradle
35 | build/
36 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/oliviergoutay/Documents/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/lib/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/oliviergoutay/Documents/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/listview_cell.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
20 |
21 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/orangegangsters/github/swipyrefreshlayout/library/SwipyRefreshLayoutDirection.java:
--------------------------------------------------------------------------------
1 | package com.orangegangsters.github.swipyrefreshlayout.library;
2 |
3 | /**
4 | * Created by oliviergoutay on 1/23/15.
5 | */
6 | public enum SwipyRefreshLayoutDirection {
7 |
8 | TOP(0),
9 | BOTTOM(1),
10 | BOTH(2);
11 |
12 | private int mValue;
13 |
14 | SwipyRefreshLayoutDirection(int value) {
15 | this.mValue = value;
16 | }
17 |
18 | public static SwipyRefreshLayoutDirection getFromInt(int value) {
19 | for (SwipyRefreshLayoutDirection direction : SwipyRefreshLayoutDirection.values()) {
20 | if (direction.mValue == value) {
21 | return direction;
22 | }
23 | }
24 | return BOTH;
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
10 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/lib/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.2"
6 |
7 | defaultConfig {
8 | minSdkVersion 9
9 | targetSdkVersion 23
10 | versionCode 1
11 | versionName "1.0"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | compile fileTree(dir: 'libs', include: ['*.jar'])
23 | compile 'com.android.support:appcompat-v7:23.3.0'
24 | }
25 |
26 | //gradle clean build uploadArchives
27 | //apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'
28 | apply from: 'https://raw.github.com/omadahealth/omada-nexus/master/gradle-mvn-push/gradle-mvn-push.gradle'
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.2"
6 |
7 | dataBinding {
8 | enabled = true
9 | }
10 |
11 | defaultConfig {
12 | applicationId "com.orangegangsters.github.swiperefreshlayout"
13 | minSdkVersion 9
14 | targetSdkVersion 23
15 | versionCode 1
16 | versionName "1.0"
17 | }
18 | buildTypes {
19 | release {
20 | minifyEnabled false
21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 | }
25 |
26 | dependencies {
27 | compile fileTree(dir: 'libs', include: ['*.jar'])
28 | compile 'com.android.support:appcompat-v7:23.3.0'
29 |
30 | compile project(':lib')
31 |
32 | //Robotium (tests)
33 | androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.5.4'
34 | }
35 |
--------------------------------------------------------------------------------
/SwipyRefreshLayout.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 OrangeGangsters
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
19 | #VERSION_NAME=1.0.0-SNAPSHOT
20 | VERSION_NAME=1.2.3
21 | VERSION_CODE=6
22 | GROUP=com.github.orangegangsters
23 |
24 | POM_DESCRIPTION=A SwipeRefreshLayout extension that allows to swipe in both direction
25 | POM_URL=https://github.com/OrangeGangsters/SwipyRefreshLayout
26 | POM_SCM_URL=https://github.com/OrangeGangsters/SwipyRefreshLayout.git
27 | POM_SCM_CONNECTION=https://github.com/OrangeGangsters/SwipyRefreshLayout.git
28 | POM_SCM_DEV_CONNECTION=https://github.com/OrangeGangsters/SwipyRefreshLayout.git
29 | POM_LICENCE_NAME=The MIT License (MIT)
30 | POM_LICENCE_URL=https://github.com/OrangeGangsters/SwipyRefreshLayoutblob/master/LICENSE
31 | POM_LICENCE_DIST=https://github.com/OrangeGangsters/SwipyRefreshLayoutblob/master/LICENSE
32 | POM_DEVELOPER_ID=olivierg13 & stoyand
33 | POM_DEVELOPER_NAME=Olivier Goutay & Stoyan Dimitrov
34 |
35 |
36 | POM_NAME=SwipyRefreshLayout Library
37 | POM_ARTIFACT_ID=swipy
38 | POM_PACKAGING=aar
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/orangegangsters/github/swiperefreshlayout/AbstractTest.java:
--------------------------------------------------------------------------------
1 | package com.orangegangsters.github.swiperefreshlayout;
2 |
3 | import android.test.ActivityInstrumentationTestCase2;
4 | import android.view.WindowManager;
5 |
6 | import com.orangegangsters.github.swipyrefreshlayout.MainActivity;
7 | import com.robotium.solo.Solo;
8 |
9 | /**
10 | * Testing Fundamentals
11 | */
12 | public class AbstractTest extends ActivityInstrumentationTestCase2 {
13 |
14 | protected Solo solo;
15 |
16 | public AbstractTest() {
17 | super(MainActivity.class);
18 | }
19 |
20 | @Override
21 | protected void setUp() throws Exception {
22 | super.setUp();
23 | solo = new Solo(getInstrumentation(), getActivity());
24 | wakeUpScreen();
25 | }
26 |
27 | @Override
28 | protected void tearDown() throws Exception {
29 | solo.finishOpenedActivities();
30 | super.tearDown();
31 | }
32 |
33 | private void wakeUpScreen() {
34 | getActivity().runOnUiThread(new Runnable() {
35 | @Override
36 | public void run() {
37 | getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
38 | getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
39 | getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
40 | getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
41 | }
42 | });
43 | solo.unlockScreen();
44 | }
45 |
46 | /**
47 | * Click on the specified view id
48 | *
49 | * @param id
50 | */
51 | protected void clickOnView(int id) {
52 | solo.clickOnView(solo.getView(id));
53 | }
54 |
55 | /**
56 | * Drags the screen to the top or down, depending on the boolean.
57 | */
58 | protected void drag(boolean top) {
59 | int screenHeight = getActivity().getWindowManager().getDefaultDisplay().getHeight();
60 |
61 | int fromY, toY;
62 |
63 | // Scroll Down // Drag Up
64 | fromY = top ? (screenHeight / 4) : (screenHeight - (screenHeight / 4));
65 | toY = top ? (screenHeight - (screenHeight / 4)) : (screenHeight / 4);
66 |
67 | solo.drag(100, 100, fromY, toY, 10);
68 | }
69 |
70 | }
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/orangegangsters/github/swiperefreshlayout/SwipyRefreshLayoutTest.java:
--------------------------------------------------------------------------------
1 | package com.orangegangsters.github.swiperefreshlayout;
2 |
3 | import android.view.View;
4 | import android.widget.ImageView;
5 |
6 | import com.orangegangsters.github.swipyrefreshlayout.MainActivity;
7 | import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
8 |
9 | /**
10 | * Created by olivier.goutay on 4/18/16.
11 | */
12 | public class SwipyRefreshLayoutTest extends AbstractTest {
13 |
14 | /**
15 | * Test that the {@link SwipyRefreshLayout#getCircleView()} is shown in the right directions.
16 | */
17 | public void testSwipyRefreshLayoutDirections() {
18 | //Wait for launch
19 | solo.waitForActivity(MainActivity.class);
20 | solo.assertCurrentActivity("MainActivity", MainActivity.class);
21 | solo.sleep(500);
22 |
23 | //Test TOP
24 | SwipyRefreshLayout swipyRefreshLayout = (SwipyRefreshLayout) solo.getView(R.id.swipyrefreshlayout);
25 | assertEquals(View.GONE, ((ImageView) swipyRefreshLayout.getCircleView()).getVisibility());
26 | clickOnView(R.id.button_top);
27 | drag(true);
28 | assertEquals(View.VISIBLE, ((ImageView) swipyRefreshLayout.getCircleView()).getVisibility());
29 | solo.sleep(MainActivity.DISMISS_TIMEOUT + 1000);
30 | assertEquals(View.GONE, ((ImageView) swipyRefreshLayout.getCircleView()).getVisibility());
31 |
32 | //Test BOTTOM
33 | clickOnView(R.id.button_bottom);
34 | drag(true);
35 | assertEquals(View.GONE, ((ImageView) swipyRefreshLayout.getCircleView()).getVisibility());
36 | drag(false);
37 | solo.sleep(500);
38 | drag(false);
39 | assertEquals(View.VISIBLE, ((ImageView) swipyRefreshLayout.getCircleView()).getVisibility());
40 | solo.sleep(MainActivity.DISMISS_TIMEOUT + 1000);
41 | assertEquals(View.GONE, ((ImageView) swipyRefreshLayout.getCircleView()).getVisibility());
42 |
43 | //Test BOTH
44 | clickOnView(R.id.button_both);
45 | drag(false);
46 | assertEquals(View.VISIBLE, ((ImageView) swipyRefreshLayout.getCircleView()).getVisibility());
47 | solo.sleep(MainActivity.DISMISS_TIMEOUT + 1000);
48 | assertEquals(View.GONE, ((ImageView) swipyRefreshLayout.getCircleView()).getVisibility());
49 | drag(true);
50 | solo.sleep(500);
51 | drag(true);
52 | assertEquals(View.VISIBLE, ((ImageView) swipyRefreshLayout.getCircleView()).getVisibility());
53 | solo.sleep(MainActivity.DISMISS_TIMEOUT + 1000);
54 | assertEquals(View.GONE, ((ImageView) swipyRefreshLayout.getCircleView()).getVisibility());
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
12 |
13 |
19 |
20 |
24 |
25 |
26 |
27 |
34 |
35 |
41 |
42 |
43 |
50 |
51 |
52 |
59 |
60 |
61 |
68 |
69 |
70 |
71 |
--------------------------------------------------------------------------------
/app/src/main/java/com/orangegangsters/github/swipyrefreshlayout/DummyListViewAdapter.java:
--------------------------------------------------------------------------------
1 | package com.orangegangsters.github.swipyrefreshlayout;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.view.LayoutInflater;
6 | import android.view.View;
7 | import android.view.ViewGroup;
8 | import android.widget.BaseAdapter;
9 | import android.widget.TextView;
10 |
11 | import com.orangegangsters.github.swiperefreshlayout.R;
12 |
13 | import java.util.ArrayList;
14 | import java.util.List;
15 |
16 | /**
17 | * Created by oliviergoutay on 1/23/15.
18 | */
19 | public class DummyListViewAdapter extends BaseAdapter {
20 |
21 | private Context mContext;
22 | private List mDummyStrings;
23 |
24 | public DummyListViewAdapter(Context mContext) {
25 | this.mContext = mContext;
26 | mDummyStrings = getDummyStrings();
27 | }
28 |
29 | @Override
30 | public int getCount() {
31 | return mDummyStrings.size();
32 | }
33 |
34 | @Override
35 | public Object getItem(int position) {
36 | return mDummyStrings.get(position);
37 | }
38 |
39 | @Override
40 | public long getItemId(int position) {
41 | return 0;
42 | }
43 |
44 | @Override
45 | public View getView(int position, View convertView, ViewGroup parent) {
46 | ViewHolder viewHolder;
47 |
48 | if(convertView==null){
49 | LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
50 |
51 | convertView = inflater.inflate(R.layout.listview_cell, parent, false);
52 | viewHolder = new ViewHolder();
53 | viewHolder.mCellNumber = (TextView) convertView.findViewById(R.id.cell_number);
54 | viewHolder.mCellText = (TextView) convertView.findViewById(R.id.cell_text);
55 | convertView.setTag(viewHolder);
56 | }else{
57 | viewHolder = (ViewHolder) convertView.getTag();
58 | }
59 |
60 | viewHolder.mCellNumber.setText("" + position);
61 | viewHolder.mCellText.setText(mDummyStrings.get(position));
62 |
63 | return convertView;
64 | }
65 |
66 | static class ViewHolder {
67 | TextView mCellNumber;
68 | TextView mCellText;
69 | }
70 |
71 | public List getDummyStrings() {
72 | List dummyStrings = new ArrayList<>();
73 |
74 | dummyStrings.add("You want");
75 | dummyStrings.add("to test");
76 | dummyStrings.add("this library");
77 | dummyStrings.add("from both");
78 | dummyStrings.add("direction.");
79 | dummyStrings.add("You may");
80 | dummyStrings.add("be amazed");
81 | dummyStrings.add("when done");
82 | dummyStrings.add("so!");
83 | dummyStrings.add("I am");
84 | dummyStrings.add("going to");
85 | dummyStrings.add("add a little");
86 | dummyStrings.add("more lines");
87 | dummyStrings.add("for big");
88 | dummyStrings.add("smartphones.");
89 |
90 | return dummyStrings;
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | SwipyRefreshLayout [](https://android-arsenal.com/details/1/1423)
2 | ================
3 |
4 | A SwipeRefreshLayout extension that allows to swipe in both direction (API 9+)
5 |
6 | To include in your project, add this to your build.gradle file:
7 | ```
8 | //SwipyRefreshLayout
9 | compile 'com.github.orangegangsters:swipy:1.2.3@aar'
10 | ```
11 |
12 | Starting from 1.2.3 we are moving to a new package name:
13 | ```
14 | //SwipyRefreshLayout
15 | compile 'com.github.omadahealth:swipy:1.2.3@aar'
16 | ```
17 |
18 | 
19 |
20 | ========
21 |
22 | ### Usage
23 |
24 | If you want an example on how to use it, you can find an example app in this repo.
25 |
26 | ```
27 |
32 |
33 |
37 |
38 |
39 | ```
40 |
41 | ```
42 | mSwipyRefreshLayout.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
43 | @Override
44 | public void onRefresh(SwipyRefreshLayoutDirection direction) {
45 | Log.d("MainActivity", "Refresh triggered at "
46 | + (direction == SwipyRefreshLayoutDirection.TOP ? "top" : "bottom"));
47 | }
48 | });
49 | ```
50 |
51 | ========
52 |
53 | ### Customization
54 |
55 | * XML:
56 | ```
57 | app:srl_direction="top"
58 | ```
59 | OR
60 | ```
61 | app:srl_direction="bottom"
62 | ```
63 | OR
64 | ```
65 | app:srl_direction="both"
66 | ```
67 |
68 | * Programmatically:
69 | ```
70 | mSwipyRefreshLayout.setDirection(SwipyRefreshLayoutDirection.TOP);
71 | ```
72 | OR
73 | ```
74 | mSwipyRefreshLayout.setDirection(SwipyRefreshLayoutDirection.BOTTOM);
75 | ```
76 | OR
77 | ```
78 | mSwipyRefreshLayout.setDirection(SwipyRefreshLayoutDirection.BOTH);
79 | ```
80 |
81 |
82 | ========
83 |
84 | ### Credits
85 |
86 | ### By Developers:
87 | [Olivier Goutay](https://github.com/olivierg13) and [Stoyan Dimitrov](https://github.com/StoyanD)
88 |
89 | ========
90 |
91 | ### License
92 |
93 | ```
94 | The MIT License (MIT)
95 |
96 | Copyright (c) 2015 OrangeGangsters
97 |
98 | Permission is hereby granted, free of charge, to any person obtaining a copy
99 | of this software and associated documentation files (the "Software"), to deal
100 | in the Software without restriction, including without limitation the rights
101 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
102 | copies of the Software, and to permit persons to whom the Software is
103 | furnished to do so, subject to the following conditions:
104 |
105 | The above copyright notice and this permission notice shall be included in all
106 | copies or substantial portions of the Software.
107 |
108 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
109 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
110 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
111 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
112 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
113 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
114 | SOFTWARE.
115 | ```
116 |
--------------------------------------------------------------------------------
/app/src/main/java/com/orangegangsters/github/swipyrefreshlayout/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.orangegangsters.github.swipyrefreshlayout;
2 |
3 | import android.databinding.DataBindingUtil;
4 | import android.os.Bundle;
5 | import android.os.Handler;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.util.Log;
8 | import android.view.View;
9 |
10 | import com.orangegangsters.github.swiperefreshlayout.R;
11 | import com.orangegangsters.github.swiperefreshlayout.databinding.ActivityMainBinding;
12 | import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
13 | import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
14 |
15 | public class MainActivity extends AppCompatActivity implements SwipyRefreshLayout.OnRefreshListener, View.OnClickListener {
16 |
17 | /**
18 | * The dismiss time for {@link SwipyRefreshLayout}
19 | */
20 | public static final int DISMISS_TIMEOUT = 2000;
21 |
22 | /**
23 | * This view binding
24 | */
25 | private ActivityMainBinding mBinding;
26 |
27 | @Override
28 | protected void onCreate(Bundle savedInstanceState) {
29 | super.onCreate(savedInstanceState);
30 | mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
31 |
32 | initLayout();
33 | }
34 |
35 | private void initLayout() {
36 | mBinding.listview.setAdapter(new DummyListViewAdapter(this));
37 |
38 | mBinding.swipyrefreshlayout.setOnRefreshListener(this);
39 |
40 | mBinding.buttonTop.setOnClickListener(this);
41 | mBinding.buttonBottom.setOnClickListener(this);
42 | mBinding.buttonBoth.setOnClickListener(this);
43 | mBinding.buttonRefresh.setOnClickListener(this);
44 | }
45 |
46 | @Override
47 | public void onClick(View v) {
48 | switch (v.getId()) {
49 | case R.id.button_top:
50 | mBinding.swipyrefreshlayout.setDirection(SwipyRefreshLayoutDirection.TOP);
51 | break;
52 | case R.id.button_bottom:
53 | mBinding.swipyrefreshlayout.setDirection(SwipyRefreshLayoutDirection.BOTTOM);
54 | break;
55 | case R.id.button_both:
56 | mBinding.swipyrefreshlayout.setDirection(SwipyRefreshLayoutDirection.BOTH);
57 | break;
58 | case R.id.button_refresh:
59 | mBinding.swipyrefreshlayout.setRefreshing(true);
60 | new Handler().postDelayed(new Runnable() {
61 | @Override
62 | public void run() {
63 | //Hide the refresh after 2sec
64 | MainActivity.this.runOnUiThread(new Runnable() {
65 | @Override
66 | public void run() {
67 | mBinding.swipyrefreshlayout.setRefreshing(false);
68 | }
69 | });
70 | }
71 | }, 2000);
72 | break;
73 | }
74 | }
75 |
76 | /**
77 | * Called when the {@link com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout}
78 | * is in refresh mode. Just for example purpose.
79 | */
80 | @Override
81 | public void onRefresh(SwipyRefreshLayoutDirection direction) {
82 | Log.d("MainActivity", "Refresh triggered at "
83 | + (direction == SwipyRefreshLayoutDirection.TOP ? "top" : "bottom"));
84 | new Handler().postDelayed(new Runnable() {
85 | @Override
86 | public void run() {
87 | //Hide the refresh after 2sec
88 | MainActivity.this.runOnUiThread(new Runnable() {
89 | @Override
90 | public void run() {
91 | mBinding.swipyrefreshlayout.setRefreshing(false);
92 | }
93 | });
94 | }
95 | }, DISMISS_TIMEOUT);
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/orangegangsters/github/swipyrefreshlayout/library/CircleImageView.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.orangegangsters.github.swipyrefreshlayout.library;
18 |
19 | import android.content.Context;
20 | import android.content.res.Resources;
21 | import android.graphics.Canvas;
22 | import android.graphics.Color;
23 | import android.graphics.Paint;
24 | import android.graphics.RadialGradient;
25 | import android.graphics.Shader;
26 | import android.graphics.drawable.ShapeDrawable;
27 | import android.graphics.drawable.shapes.OvalShape;
28 | import android.support.v4.view.ViewCompat;
29 | import android.view.animation.Animation;
30 | import android.widget.ImageView;
31 |
32 | /**
33 | * Private class created to work around issues with AnimationListeners being
34 | * called before the animation is actually complete and support shadows on older
35 | * platforms.
36 | *
37 | * @hide
38 | */
39 | class CircleImageView extends ImageView {
40 |
41 | private static final int KEY_SHADOW_COLOR = 0x1E000000;
42 | private static final int FILL_SHADOW_COLOR = 0x3D000000;
43 | // PX
44 | private static final float X_OFFSET = 0f;
45 | private static final float Y_OFFSET = 1.75f;
46 | private static final float SHADOW_RADIUS = 3.5f;
47 | private static final int SHADOW_ELEVATION = 4;
48 |
49 | private Animation.AnimationListener mListener;
50 | private int mShadowRadius;
51 |
52 | public CircleImageView(Context context, int color, final float radius) {
53 | super(context);
54 | final float density = getContext().getResources().getDisplayMetrics().density;
55 | final int diameter = (int) (radius * density * 2);
56 | final int shadowYOffset = (int) (density * Y_OFFSET);
57 | final int shadowXOffset = (int) (density * X_OFFSET);
58 |
59 | mShadowRadius = (int) (density * SHADOW_RADIUS);
60 |
61 | ShapeDrawable circle;
62 | if (elevationSupported()) {
63 | circle = new ShapeDrawable(new OvalShape());
64 | ViewCompat.setElevation(this, SHADOW_ELEVATION * density);
65 | } else {
66 | OvalShape oval = new OvalShadow(mShadowRadius, diameter);
67 | circle = new ShapeDrawable(oval);
68 | ViewCompat.setLayerType(this, ViewCompat.LAYER_TYPE_SOFTWARE, circle.getPaint());
69 | circle.getPaint().setShadowLayer(mShadowRadius, shadowXOffset, shadowYOffset,
70 | KEY_SHADOW_COLOR);
71 | final int padding = (int) mShadowRadius;
72 | // set padding so the inner image sits correctly within the shadow.
73 | setPadding(padding, padding, padding, padding);
74 | }
75 | circle.getPaint().setColor(color);
76 | setBackgroundDrawable(circle);
77 | }
78 |
79 | private boolean elevationSupported() {
80 | return android.os.Build.VERSION.SDK_INT >= 21;
81 | }
82 |
83 | @Override
84 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
85 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
86 | if (!elevationSupported()) {
87 | setMeasuredDimension(getMeasuredWidth() + mShadowRadius*2, getMeasuredHeight()
88 | + mShadowRadius*2);
89 | }
90 | }
91 |
92 | public void setAnimationListener(Animation.AnimationListener listener) {
93 | mListener = listener;
94 | }
95 |
96 | @Override
97 | public void onAnimationStart() {
98 | super.onAnimationStart();
99 | if (mListener != null) {
100 | mListener.onAnimationStart(getAnimation());
101 | }
102 | }
103 |
104 | @Override
105 | public void onAnimationEnd() {
106 | super.onAnimationEnd();
107 | if (mListener != null) {
108 | mListener.onAnimationEnd(getAnimation());
109 | }
110 | }
111 |
112 | /**
113 | * Update the background color of the circle image view.
114 | */
115 | public void setBackgroundColor(int colorRes) {
116 | if (getBackground() instanceof ShapeDrawable) {
117 | final Resources res = getResources();
118 | ((ShapeDrawable) getBackground()).getPaint().setColor(res.getColor(colorRes));
119 | }
120 | }
121 |
122 | private class OvalShadow extends OvalShape {
123 | private RadialGradient mRadialGradient;
124 | private int mShadowRadius;
125 | private Paint mShadowPaint;
126 | private int mCircleDiameter;
127 |
128 | public OvalShadow(int shadowRadius, int circleDiameter) {
129 | super();
130 | mShadowPaint = new Paint();
131 | mShadowRadius = shadowRadius;
132 | mCircleDiameter = circleDiameter;
133 | mRadialGradient = new RadialGradient(mCircleDiameter / 2, mCircleDiameter / 2,
134 | mShadowRadius, new int[] {
135 | FILL_SHADOW_COLOR, Color.TRANSPARENT
136 | }, null, Shader.TileMode.CLAMP);
137 | mShadowPaint.setShader(mRadialGradient);
138 | }
139 |
140 | @Override
141 | public void draw(Canvas canvas, Paint paint) {
142 | final int viewWidth = CircleImageView.this.getWidth();
143 | final int viewHeight = CircleImageView.this.getHeight();
144 | canvas.drawCircle(viewWidth / 2, viewHeight / 2, (mCircleDiameter / 2 + mShadowRadius),
145 | mShadowPaint);
146 | canvas.drawCircle(viewWidth / 2, viewHeight / 2, (mCircleDiameter / 2), paint);
147 | }
148 | }
149 | }
150 |
--------------------------------------------------------------------------------
/lib/lib.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | generateDebugSources
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
--------------------------------------------------------------------------------
/app/app.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | generateDebugSources
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/orangegangsters/github/swipyrefreshlayout/library/MaterialProgressDrawable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.orangegangsters.github.swipyrefreshlayout.library;
18 |
19 | import android.view.animation.AccelerateDecelerateInterpolator;
20 | import android.view.animation.Interpolator;
21 | import android.view.animation.Animation;
22 | import android.view.animation.LinearInterpolator;
23 | import android.view.animation.Transformation;
24 | import android.content.Context;
25 | import android.content.res.Resources;
26 | import android.graphics.Canvas;
27 | import android.graphics.Color;
28 | import android.graphics.ColorFilter;
29 | import android.graphics.Paint;
30 | import android.graphics.Paint.Style;
31 | import android.graphics.Path;
32 | import android.graphics.PixelFormat;
33 | import android.graphics.Rect;
34 | import android.graphics.RectF;
35 | import android.graphics.drawable.Drawable;
36 | import android.graphics.drawable.Animatable;
37 | import android.support.annotation.IntDef;
38 | import android.support.annotation.NonNull;
39 | import android.util.DisplayMetrics;
40 | import android.view.View;
41 |
42 | import java.lang.annotation.Retention;
43 | import java.lang.annotation.RetentionPolicy;
44 | import java.util.ArrayList;
45 |
46 | /**
47 | * Fancy progress indicator for Material theme.
48 | *
49 | * @hide
50 | */
51 | class MaterialProgressDrawable extends Drawable implements Animatable {
52 | private static final Interpolator LINEAR_INTERPOLATOR = new LinearInterpolator();
53 | private static final Interpolator END_CURVE_INTERPOLATOR = new EndCurveInterpolator();
54 | private static final Interpolator START_CURVE_INTERPOLATOR = new StartCurveInterpolator();
55 | private static final Interpolator EASE_INTERPOLATOR = new AccelerateDecelerateInterpolator();
56 | private boolean stopped;
57 |
58 | @Retention(RetentionPolicy.CLASS)
59 | @IntDef({LARGE, DEFAULT})
60 | public @interface ProgressDrawableSize {}
61 | // Maps to ProgressBar.Large style
62 | static final int LARGE = 0;
63 | // Maps to ProgressBar default style
64 | static final int DEFAULT = 1;
65 |
66 | // Maps to ProgressBar default style
67 | private static final int CIRCLE_DIAMETER = 40;
68 | private static final float CENTER_RADIUS = 8.75f; //should add up to 10 when + stroke_width
69 | private static final float STROKE_WIDTH = 2.5f;
70 |
71 | // Maps to ProgressBar.Large style
72 | private static final int CIRCLE_DIAMETER_LARGE = 56;
73 | private static final float CENTER_RADIUS_LARGE = 12.5f;
74 | private static final float STROKE_WIDTH_LARGE = 3f;
75 |
76 | private final int[] COLORS = new int[] {
77 | Color.BLACK
78 | };
79 |
80 | /** The duration of a single progress spin in milliseconds. */
81 | private static final int ANIMATION_DURATION = 1000 * 80 / 60;
82 |
83 | /** The number of points in the progress "star". */
84 | private static final float NUM_POINTS = 5f;
85 | /** The list of animators operating on this drawable. */
86 | private final ArrayList mAnimators = new ArrayList();
87 |
88 | /** The indicator ring, used to manage animation state. */
89 | private final Ring mRing;
90 |
91 | /** Canvas rotation in degrees. */
92 | private float mRotation;
93 |
94 | /** Layout info for the arrowhead in dp */
95 | private static final int ARROW_WIDTH = 10;
96 | private static final int ARROW_HEIGHT = 5;
97 | private static final float ARROW_OFFSET_ANGLE = 5;
98 |
99 | /** Layout info for the arrowhead for the large spinner in dp */
100 | private static final int ARROW_WIDTH_LARGE = 12;
101 | private static final int ARROW_HEIGHT_LARGE = 6;
102 | private static final float MAX_PROGRESS_ARC = .8f;
103 |
104 | private Resources mResources;
105 | private View mParent;
106 | private Animation mAnimation;
107 | private float mRotationCount;
108 | private double mWidth;
109 | private double mHeight;
110 | private Animation mFinishAnimation;
111 |
112 | public MaterialProgressDrawable(Context context, View parent) {
113 | mParent = parent;
114 | mResources = context.getResources();
115 |
116 | mRing = new Ring(mCallback);
117 | mRing.setColors(COLORS);
118 |
119 | updateSizes(DEFAULT);
120 | setupAnimators();
121 | }
122 |
123 | private void setSizeParameters(double progressCircleWidth, double progressCircleHeight,
124 | double centerRadius, double strokeWidth, float arrowWidth, float arrowHeight) {
125 | final Ring ring = mRing;
126 | final DisplayMetrics metrics = mResources.getDisplayMetrics();
127 | final float screenDensity = metrics.density;
128 |
129 | mWidth = progressCircleWidth * screenDensity;
130 | mHeight = progressCircleHeight * screenDensity;
131 | ring.setStrokeWidth((float) strokeWidth * screenDensity);
132 | ring.setCenterRadius(centerRadius * screenDensity);
133 | ring.setColorIndex(0);
134 | ring.setArrowDimensions(arrowWidth * screenDensity, arrowHeight * screenDensity);
135 | ring.setInsets((int) mWidth, (int) mHeight);
136 | }
137 |
138 | /**
139 | * Set the overall size for the progress spinner. This updates the radius
140 | * and stroke width of the ring.
141 | *
142 | * @param size One of {@link #LARGE} or
143 | * {@link #DEFAULT}
144 | */
145 | public void updateSizes(@ProgressDrawableSize int size) {
146 | if (size == LARGE) {
147 | setSizeParameters(CIRCLE_DIAMETER_LARGE, CIRCLE_DIAMETER_LARGE, CENTER_RADIUS_LARGE,
148 | STROKE_WIDTH_LARGE, ARROW_WIDTH_LARGE, ARROW_HEIGHT_LARGE);
149 | } else {
150 | setSizeParameters(CIRCLE_DIAMETER, CIRCLE_DIAMETER, CENTER_RADIUS, STROKE_WIDTH,
151 | ARROW_WIDTH, ARROW_HEIGHT);
152 | }
153 | }
154 |
155 | /**
156 | * @param show Set to true to display the arrowhead on the progress spinner.
157 | */
158 | public void showArrow(boolean show) {
159 | mRing.setShowArrow(show);
160 | }
161 |
162 | /**
163 | * @param scale Set the scale of the arrowhead for the spinner.
164 | */
165 | public void setArrowScale(float scale) {
166 | mRing.setArrowScale(scale);
167 | }
168 |
169 | /**
170 | * Set the start and end trim for the progress spinner arc.
171 | *
172 | * @param startAngle start angle
173 | * @param endAngle end angle
174 | */
175 | public void setStartEndTrim(float startAngle, float endAngle) {
176 | mRing.setStartTrim(startAngle);
177 | mRing.setEndTrim(endAngle);
178 | }
179 |
180 | /**
181 | * Set the amount of rotation to apply to the progress spinner.
182 | *
183 | * @param rotation Rotation is from [0..1]
184 | */
185 | public void setProgressRotation(float rotation) {
186 | mRing.setRotation(rotation);
187 | }
188 |
189 | /**
190 | * Update the background color of the circle image view.
191 | */
192 | public void setBackgroundColor(int color) {
193 | mRing.setBackgroundColor(color);
194 | }
195 |
196 | /**
197 | * Set the colors used in the progress animation from color resources.
198 | * The first color will also be the color of the bar that grows in response
199 | * to a user swipe gesture.
200 | *
201 | * @param colors
202 | */
203 | public void setColorSchemeColors(int... colors) {
204 | mRing.setColors(colors);
205 | mRing.setColorIndex(0);
206 | }
207 |
208 | @Override
209 | public int getIntrinsicHeight() {
210 | return (int) mHeight;
211 | }
212 |
213 | @Override
214 | public int getIntrinsicWidth() {
215 | return (int) mWidth;
216 | }
217 |
218 | @Override
219 | public void draw(Canvas c) {
220 | final Rect bounds = getBounds();
221 | final int saveCount = c.save();
222 | c.rotate(mRotation, bounds.exactCenterX(), bounds.exactCenterY());
223 | mRing.draw(c, bounds);
224 | c.restoreToCount(saveCount);
225 | }
226 |
227 | @Override
228 | public void setAlpha(int alpha) {
229 | mRing.setAlpha(alpha);
230 | }
231 |
232 | public int getAlpha() {
233 | return mRing.getAlpha();
234 | }
235 |
236 | @Override
237 | public void setColorFilter(ColorFilter colorFilter) {
238 | mRing.setColorFilter(colorFilter);
239 | }
240 |
241 | @SuppressWarnings("unused")
242 | void setRotation(float rotation) {
243 | mRotation = rotation;
244 | invalidateSelf();
245 | }
246 |
247 | @SuppressWarnings("unused")
248 | private float getRotation() {
249 | return mRotation;
250 | }
251 |
252 | @Override
253 | public int getOpacity() {
254 | return PixelFormat.TRANSLUCENT;
255 | }
256 |
257 | @Override
258 | public boolean isRunning() {
259 | final ArrayList animators = mAnimators;
260 | final int N = animators.size();
261 | for (int i = 0; i < N; i++) {
262 | final Animation animator = animators.get(i);
263 | if (animator.hasStarted() && !animator.hasEnded()) {
264 | return true;
265 | }
266 | }
267 | return false;
268 | }
269 |
270 | @Override
271 | public void start() {
272 | stopped = false;
273 | mAnimation.reset();
274 | mRing.storeOriginals();
275 | // Already showing some part of the ring
276 | if (mRing.getEndTrim() != mRing.getStartTrim()) {
277 | mParent.startAnimation(mFinishAnimation);
278 | } else {
279 | mRing.setColorIndex(0);
280 | mRing.resetOriginals();
281 | mParent.startAnimation(mAnimation);
282 | }
283 | }
284 |
285 | @Override
286 | public void stop() {
287 | stopped = true;
288 | mParent.clearAnimation();
289 | setRotation(0);
290 | mRing.setShowArrow(false);
291 | mRing.setColorIndex(0);
292 | mRing.resetOriginals();
293 | }
294 |
295 | private void setupAnimators() {
296 | final Ring ring = mRing;
297 | final Animation finishRingAnimation = new Animation() {
298 | public void applyTransformation(float interpolatedTime, Transformation t) {
299 | // shrink back down and complete a full rotation before starting other circles
300 | // Rotation goes between [0..1].
301 | float targetRotation = (float) (Math.floor(ring.getStartingRotation()
302 | / MAX_PROGRESS_ARC) + 1f);
303 | final float startTrim = ring.getStartingStartTrim()
304 | + (ring.getStartingEndTrim() - ring.getStartingStartTrim())
305 | * interpolatedTime;
306 | ring.setStartTrim(startTrim);
307 | final float rotation = ring.getStartingRotation()
308 | + ((targetRotation - ring.getStartingRotation()) * interpolatedTime);
309 | ring.setRotation(rotation);
310 | ring.setArrowScale(1 - interpolatedTime);
311 | }
312 | };
313 | finishRingAnimation.setInterpolator(EASE_INTERPOLATOR);
314 | finishRingAnimation.setDuration(ANIMATION_DURATION/2);
315 | finishRingAnimation.setAnimationListener(new Animation.AnimationListener() {
316 |
317 | @Override
318 | public void onAnimationStart(Animation animation) {
319 | }
320 |
321 | @Override
322 | public void onAnimationEnd(Animation animation) {
323 | if (stopped) {
324 | return;
325 | }
326 | ring.goToNextColor();
327 | ring.storeOriginals();
328 | ring.setShowArrow(false);
329 | mParent.startAnimation(mAnimation);
330 | }
331 |
332 | @Override
333 | public void onAnimationRepeat(Animation animation) {
334 | }
335 | });
336 | final Animation animation = new Animation() {
337 | @Override
338 | public void applyTransformation(float interpolatedTime, Transformation t) {
339 | // The minProgressArc is calculated from 0 to create an angle that
340 | // matches the stroke width.
341 | final float minProgressArc = (float) Math.toRadians(ring.getStrokeWidth()
342 | / (2 * Math.PI * ring.getCenterRadius()));
343 | final float startingEndTrim = ring.getStartingEndTrim();
344 | final float startingTrim = ring.getStartingStartTrim();
345 | final float startingRotation = ring.getStartingRotation();
346 |
347 | // Offset the minProgressArc to where the endTrim is located.
348 | final float minArc = MAX_PROGRESS_ARC - minProgressArc;
349 | final float endTrim = startingEndTrim
350 | + (minArc * START_CURVE_INTERPOLATOR.getInterpolation(interpolatedTime));
351 | ring.setEndTrim(endTrim);
352 |
353 | final float startTrim = startingTrim
354 | + (MAX_PROGRESS_ARC * END_CURVE_INTERPOLATOR
355 | .getInterpolation(interpolatedTime));
356 | ring.setStartTrim(startTrim);
357 |
358 | final float rotation = startingRotation + (0.25f * interpolatedTime);
359 | ring.setRotation(rotation);
360 |
361 | float groupRotation = ((720.0f / NUM_POINTS) * interpolatedTime)
362 | + (720.0f * (mRotationCount / NUM_POINTS));
363 | setRotation(groupRotation);
364 | }
365 | };
366 | animation.setRepeatCount(Animation.INFINITE);
367 | animation.setRepeatMode(Animation.RESTART);
368 | animation.setInterpolator(LINEAR_INTERPOLATOR);
369 | animation.setDuration(ANIMATION_DURATION);
370 | animation.setAnimationListener(new Animation.AnimationListener() {
371 |
372 | @Override
373 | public void onAnimationStart(Animation animation) {
374 | mRotationCount = 0;
375 | }
376 |
377 | @Override
378 | public void onAnimationEnd(Animation animation) {
379 | // do nothing
380 | }
381 |
382 | @Override
383 | public void onAnimationRepeat(Animation animation) {
384 | ring.storeOriginals();
385 | ring.goToNextColor();
386 | ring.setStartTrim(ring.getEndTrim());
387 | mRotationCount = (mRotationCount + 1) % (NUM_POINTS);
388 | }
389 | });
390 | mFinishAnimation = finishRingAnimation;
391 | mAnimation = animation;
392 | }
393 |
394 | private final Callback mCallback = new Callback() {
395 | @Override
396 | public void invalidateDrawable(Drawable d) {
397 | invalidateSelf();
398 | }
399 |
400 | @Override
401 | public void scheduleDrawable(Drawable d, Runnable what, long when) {
402 | scheduleSelf(what, when);
403 | }
404 |
405 | @Override
406 | public void unscheduleDrawable(Drawable d, Runnable what) {
407 | unscheduleSelf(what);
408 | }
409 | };
410 |
411 | private static class Ring {
412 | private final RectF mTempBounds = new RectF();
413 | private final Paint mPaint = new Paint();
414 | private final Paint mArrowPaint = new Paint();
415 |
416 | private final Callback mCallback;
417 |
418 | private float mStartTrim = 0.0f;
419 | private float mEndTrim = 0.0f;
420 | private float mRotation = 0.0f;
421 | private float mStrokeWidth = 5.0f;
422 | private float mStrokeInset = 2.5f;
423 |
424 | private int[] mColors;
425 | // mColorIndex represents the offset into the available mColors that the
426 | // progress circle should currently display. As the progress circle is
427 | // animating, the mColorIndex moves by one to the next available color.
428 | private int mColorIndex;
429 | private float mStartingStartTrim;
430 | private float mStartingEndTrim;
431 | private float mStartingRotation;
432 | private boolean mShowArrow;
433 | private Path mArrow;
434 | private float mArrowScale;
435 | private double mRingCenterRadius;
436 | private int mArrowWidth;
437 | private int mArrowHeight;
438 | private int mAlpha;
439 | private final Paint mCirclePaint = new Paint();
440 | private int mBackgroundColor;
441 |
442 | public Ring(Callback callback) {
443 | mCallback = callback;
444 |
445 | mPaint.setStrokeCap(Paint.Cap.SQUARE);
446 | mPaint.setAntiAlias(true);
447 | mPaint.setStyle(Style.STROKE);
448 |
449 | mArrowPaint.setStyle(Style.FILL);
450 | mArrowPaint.setAntiAlias(true);
451 | }
452 |
453 | public void setBackgroundColor(int color) {
454 | mBackgroundColor = color;
455 | }
456 |
457 | /**
458 | * Set the dimensions of the arrowhead.
459 | *
460 | * @param width Width of the hypotenuse of the arrow head
461 | * @param height Height of the arrow point
462 | */
463 | public void setArrowDimensions(float width, float height) {
464 | mArrowWidth = (int) width;
465 | mArrowHeight = (int) height;
466 | }
467 |
468 | /**
469 | * Draw the progress spinner
470 | */
471 | public void draw(Canvas c, Rect bounds) {
472 | final RectF arcBounds = mTempBounds;
473 | arcBounds.set(bounds);
474 | arcBounds.inset(mStrokeInset, mStrokeInset);
475 |
476 | final float startAngle = (mStartTrim + mRotation) * 360;
477 | final float endAngle = (mEndTrim + mRotation) * 360;
478 | float sweepAngle = endAngle - startAngle;
479 |
480 | mPaint.setColor(mColors[mColorIndex]);
481 | c.drawArc(arcBounds, startAngle, sweepAngle, false, mPaint);
482 |
483 | drawTriangle(c, startAngle, sweepAngle, bounds);
484 |
485 | if (mAlpha < 255) {
486 | mCirclePaint.setColor(mBackgroundColor);
487 | mCirclePaint.setAlpha(255 - mAlpha);
488 | c.drawCircle(bounds.exactCenterX(), bounds.exactCenterY(), bounds.width() / 2,
489 | mCirclePaint);
490 | }
491 | }
492 |
493 | private void drawTriangle(Canvas c, float startAngle, float sweepAngle, Rect bounds) {
494 | if (mShowArrow) {
495 | if (mArrow == null) {
496 | mArrow = new Path();
497 | mArrow.setFillType(Path.FillType.EVEN_ODD);
498 | } else {
499 | mArrow.reset();
500 | }
501 |
502 | // Adjust the position of the triangle so that it is inset as
503 | // much as the arc, but also centered on the arc.
504 | float inset = (int) mStrokeInset / 2 * mArrowScale;
505 | float x = (float) (mRingCenterRadius * Math.cos(0) + bounds.exactCenterX());
506 | float y = (float) (mRingCenterRadius * Math.sin(0) + bounds.exactCenterY());
507 |
508 | // Update the path each time. This works around an issue in SKIA
509 | // where concatenating a rotation matrix to a scale matrix
510 | // ignored a starting negative rotation. This appears to have
511 | // been fixed as of API 21.
512 | mArrow.moveTo(0, 0);
513 | mArrow.lineTo(mArrowWidth * mArrowScale, 0);
514 | mArrow.lineTo((mArrowWidth * mArrowScale / 2), (mArrowHeight
515 | * mArrowScale));
516 | mArrow.offset(x - inset, y);
517 | mArrow.close();
518 | // draw a triangle
519 | mArrowPaint.setColor(mColors[mColorIndex]);
520 | c.rotate(startAngle + sweepAngle - ARROW_OFFSET_ANGLE, bounds.exactCenterX(),
521 | bounds.exactCenterY());
522 | c.drawPath(mArrow, mArrowPaint);
523 | }
524 | }
525 |
526 | /**
527 | * Set the colors the progress spinner alternates between.
528 | *
529 | * @param colors Array of integers describing the colors. Must be non-null.
530 | */
531 | public void setColors(@NonNull int[] colors) {
532 | mColors = colors;
533 | // if colors are reset, make sure to reset the color index as well
534 | setColorIndex(0);
535 | }
536 |
537 | /**
538 | * @param index Index into the color array of the color to display in
539 | * the progress spinner.
540 | */
541 | public void setColorIndex(int index) {
542 | mColorIndex = index;
543 | }
544 |
545 | /**
546 | * Proceed to the next available ring color. This will automatically
547 | * wrap back to the beginning of colors.
548 | */
549 | public void goToNextColor() {
550 | mColorIndex = (mColorIndex + 1) % (mColors.length);
551 | }
552 |
553 | public void setColorFilter(ColorFilter filter) {
554 | mPaint.setColorFilter(filter);
555 | invalidateSelf();
556 | }
557 |
558 | /**
559 | * @param alpha Set the alpha of the progress spinner and associated arrowhead.
560 | */
561 | public void setAlpha(int alpha) {
562 | mAlpha = alpha;
563 | }
564 |
565 | /**
566 | * @return Current alpha of the progress spinner and arrowhead.
567 | */
568 | public int getAlpha() {
569 | return mAlpha;
570 | }
571 |
572 | /**
573 | * @param strokeWidth Set the stroke width of the progress spinner in pixels.
574 | */
575 | public void setStrokeWidth(float strokeWidth) {
576 | mStrokeWidth = strokeWidth;
577 | mPaint.setStrokeWidth(strokeWidth);
578 | invalidateSelf();
579 | }
580 |
581 | @SuppressWarnings("unused")
582 | public float getStrokeWidth() {
583 | return mStrokeWidth;
584 | }
585 |
586 | @SuppressWarnings("unused")
587 | public void setStartTrim(float startTrim) {
588 | mStartTrim = startTrim;
589 | invalidateSelf();
590 | }
591 |
592 | @SuppressWarnings("unused")
593 | public float getStartTrim() {
594 | return mStartTrim;
595 | }
596 |
597 | public float getStartingStartTrim() {
598 | return mStartingStartTrim;
599 | }
600 |
601 | public float getStartingEndTrim() {
602 | return mStartingEndTrim;
603 | }
604 |
605 | @SuppressWarnings("unused")
606 | public void setEndTrim(float endTrim) {
607 | mEndTrim = endTrim;
608 | invalidateSelf();
609 | }
610 |
611 | @SuppressWarnings("unused")
612 | public float getEndTrim() {
613 | return mEndTrim;
614 | }
615 |
616 | @SuppressWarnings("unused")
617 | public void setRotation(float rotation) {
618 | mRotation = rotation;
619 | invalidateSelf();
620 | }
621 |
622 | @SuppressWarnings("unused")
623 | public float getRotation() {
624 | return mRotation;
625 | }
626 |
627 | public void setInsets(int width, int height) {
628 | final float minEdge = (float) Math.min(width, height);
629 | float insets;
630 | if (mRingCenterRadius <= 0 || minEdge < 0) {
631 | insets = (float) Math.ceil(mStrokeWidth / 2.0f);
632 | } else {
633 | insets = (float) (minEdge / 2.0f - mRingCenterRadius);
634 | }
635 | mStrokeInset = insets;
636 | }
637 |
638 | @SuppressWarnings("unused")
639 | public float getInsets() {
640 | return mStrokeInset;
641 | }
642 |
643 | /**
644 | * @param centerRadius Inner radius in px of the circle the progress
645 | * spinner arc traces.
646 | */
647 | public void setCenterRadius(double centerRadius) {
648 | mRingCenterRadius = centerRadius;
649 | }
650 |
651 | public double getCenterRadius() {
652 | return mRingCenterRadius;
653 | }
654 |
655 | /**
656 | * @param show Set to true to show the arrow head on the progress spinner.
657 | */
658 | public void setShowArrow(boolean show) {
659 | if (mShowArrow != show) {
660 | mShowArrow = show;
661 | invalidateSelf();
662 | }
663 | }
664 |
665 | /**
666 | * @param scale Set the scale of the arrowhead for the spinner.
667 | */
668 | public void setArrowScale(float scale) {
669 | if (scale != mArrowScale) {
670 | mArrowScale = scale;
671 | invalidateSelf();
672 | }
673 | }
674 |
675 | /**
676 | * @return The amount the progress spinner is currently rotated, between [0..1].
677 | */
678 | public float getStartingRotation() {
679 | return mStartingRotation;
680 | }
681 |
682 | /**
683 | * If the start / end trim are offset to begin with, store them so that
684 | * animation starts from that offset.
685 | */
686 | public void storeOriginals() {
687 | mStartingStartTrim = mStartTrim;
688 | mStartingEndTrim = mEndTrim;
689 | mStartingRotation = mRotation;
690 | }
691 |
692 | /**
693 | * Reset the progress spinner to default rotation, start and end angles.
694 | */
695 | public void resetOriginals() {
696 | mStartingStartTrim = 0;
697 | mStartingEndTrim = 0;
698 | mStartingRotation = 0;
699 | setStartTrim(0);
700 | setEndTrim(0);
701 | setRotation(0);
702 | }
703 |
704 | private void invalidateSelf() {
705 | mCallback.invalidateDrawable(null);
706 | }
707 | }
708 |
709 | /**
710 | * Squishes the interpolation curve into the second half of the animation.
711 | */
712 | private static class EndCurveInterpolator extends AccelerateDecelerateInterpolator {
713 | @Override
714 | public float getInterpolation(float input) {
715 | return super.getInterpolation(Math.max(0, (input - 0.5f) * 2.0f));
716 | }
717 | }
718 |
719 | /**
720 | * Squishes the interpolation curve into the first half of the animation.
721 | */
722 | private static class StartCurveInterpolator extends AccelerateDecelerateInterpolator {
723 | @Override
724 | public float getInterpolation(float input) {
725 | return super.getInterpolation(Math.min(1, input * 2.0f));
726 | }
727 | }
728 | }
729 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/orangegangsters/github/swipyrefreshlayout/library/SwipyRefreshLayout.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2013 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.orangegangsters.github.swipyrefreshlayout.library;
18 |
19 |
20 | import android.content.Context;
21 | import android.content.res.Resources;
22 | import android.content.res.TypedArray;
23 | import android.support.v4.view.MotionEventCompat;
24 | import android.support.v4.view.ViewCompat;
25 | import android.util.AttributeSet;
26 | import android.util.DisplayMetrics;
27 | import android.util.Log;
28 | import android.view.MotionEvent;
29 | import android.view.View;
30 | import android.view.ViewConfiguration;
31 | import android.view.ViewGroup;
32 | import android.view.animation.Animation;
33 | import android.view.animation.Animation.AnimationListener;
34 | import android.view.animation.DecelerateInterpolator;
35 | import android.view.animation.Transformation;
36 | import android.widget.AbsListView;
37 |
38 | /**
39 | * The SwipeRefreshLayout should be used whenever the user can refresh the
40 | * contents of a view via a vertical swipe gesture. The activity that
41 | * instantiates this view should add an OnRefreshListener to be notified
42 | * whenever the swipe to refresh gesture is completed. The SwipeRefreshLayout
43 | * will notify the listener each and every time the gesture is completed again;
44 | * the listener is responsible for correctly determining when to actually
45 | * initiate a refresh of its content. If the listener determines there should
46 | * not be a refresh, it must call setRefreshing(false) to cancel any visual
47 | * indication of a refresh. If an activity wishes to show just the progress
48 | * animation, it should call setRefreshing(true). To disable the gesture and
49 | * progress animation, call setEnabled(false) on the view.
50 | *
51 | * This layout should be made the parent of the view that will be refreshed as a
52 | * result of the gesture and can only support one direct child. This view will
53 | * also be made the target of the gesture and will be forced to match both the
54 | * width and the height supplied in this layout. The SwipeRefreshLayout does not
55 | * provide accessibility events; instead, a menu item must be provided to allow
56 | * refresh of the content wherever this gesture is used.
57 | *
58 | */
59 | public class SwipyRefreshLayout extends ViewGroup {
60 |
61 | public static final String TAG = "SwipyRefreshLayout";
62 |
63 | private static final float MAX_SWIPE_DISTANCE_FACTOR = .6f;
64 | private static final int REFRESH_TRIGGER_DISTANCE = 120;
65 |
66 | // Maps to ProgressBar.Large style
67 | public static final int LARGE = MaterialProgressDrawable.LARGE;
68 | // Maps to ProgressBar default style
69 | public static final int DEFAULT = MaterialProgressDrawable.DEFAULT;
70 |
71 | private static final String LOG_TAG = SwipyRefreshLayout.class.getSimpleName();
72 |
73 | private static final int MAX_ALPHA = 255;
74 | private static final int STARTING_PROGRESS_ALPHA = (int) (.3f * MAX_ALPHA);
75 |
76 | private static final int CIRCLE_DIAMETER = 40;
77 | private static final int CIRCLE_DIAMETER_LARGE = 56;
78 |
79 | private static final float DECELERATE_INTERPOLATION_FACTOR = 2f;
80 | private static final int INVALID_POINTER = -1;
81 | private static final float DRAG_RATE = .5f;
82 |
83 | // Max amount of circle that can be filled by progress during swipe gesture,
84 | // where 1.0 is a full circle
85 | private static final float MAX_PROGRESS_ANGLE = .8f;
86 |
87 | private static final int SCALE_DOWN_DURATION = 150;
88 |
89 | private static final int ALPHA_ANIMATION_DURATION = 300;
90 |
91 | private static final int ANIMATE_TO_TRIGGER_DURATION = 200;
92 |
93 | private static final int ANIMATE_TO_START_DURATION = 200;
94 |
95 | // Default background for the progress spinner
96 | private static final int CIRCLE_BG_LIGHT = 0xFFFAFAFA;
97 | // Default offset in dips from the top of the view to where the progress spinner should stop
98 | private static final int DEFAULT_CIRCLE_TARGET = 64;
99 |
100 | private View mTarget; // the target of the gesture
101 | private SwipyRefreshLayoutDirection mDirection;
102 | private boolean mBothDirection;
103 | private OnRefreshListener mListener;
104 | private boolean mRefreshing = false;
105 | private int mTouchSlop;
106 | private float mTotalDragDistance = -1;
107 | private int mMediumAnimationDuration;
108 | private int mCurrentTargetOffsetTop;
109 | // Whether or not the starting offset has been determined.
110 | private boolean mOriginalOffsetCalculated = false;
111 |
112 | private float mInitialMotionY;
113 | private float mInitialDownY;
114 | private boolean mIsBeingDragged;
115 | private int mActivePointerId = INVALID_POINTER;
116 | // Whether this item is scaled up rather than clipped
117 | private boolean mScale;
118 |
119 | // Target is returning to its start offset because it was cancelled or a
120 | // refresh was triggered.
121 | private boolean mReturningToStart;
122 | private final DecelerateInterpolator mDecelerateInterpolator;
123 | private static final int[] LAYOUT_ATTRS = new int[]{
124 | android.R.attr.enabled
125 | };
126 |
127 | private CircleImageView mCircleView;
128 | private int mCircleViewIndex = -1;
129 |
130 | protected int mFrom;
131 |
132 | private float mStartingScale;
133 |
134 | protected int mOriginalOffsetTop;
135 |
136 | private MaterialProgressDrawable mProgress;
137 |
138 | private Animation mScaleAnimation;
139 |
140 | private Animation mScaleDownAnimation;
141 |
142 | private Animation mAlphaStartAnimation;
143 |
144 | private Animation mAlphaMaxAnimation;
145 |
146 | private Animation mScaleDownToStartAnimation;
147 |
148 | private float mSpinnerFinalOffset;
149 |
150 | private boolean mNotify;
151 |
152 | private int mCircleWidth;
153 |
154 | private int mCircleHeight;
155 |
156 | // Whether the client has set a custom starting position;
157 | private boolean mUsingCustomStart;
158 |
159 | private AnimationListener mRefreshListener = new AnimationListener() {
160 | @Override
161 | public void onAnimationStart(Animation animation) {
162 | }
163 |
164 | @Override
165 | public void onAnimationRepeat(Animation animation) {
166 | }
167 |
168 | @Override
169 | public void onAnimationEnd(Animation animation) {
170 | if (mRefreshing) {
171 | // Make sure the progress view is fully visible
172 | mProgress.setAlpha(MAX_ALPHA);
173 | mProgress.start();
174 | if (mNotify) {
175 | if (mListener != null) {
176 | mListener.onRefresh(mDirection);
177 | }
178 | }
179 | } else {
180 | mProgress.stop();
181 | mCircleView.setVisibility(View.GONE);
182 | setColorViewAlpha(MAX_ALPHA);
183 | // Return the circle to its start position
184 | if (mScale) {
185 | setAnimationProgress(0 /* animation complete and view is hidden */);
186 | } else {
187 | setTargetOffsetTopAndBottom(mOriginalOffsetTop - mCurrentTargetOffsetTop,
188 | true /* requires update */);
189 | }
190 | }
191 | mCurrentTargetOffsetTop = mCircleView.getTop();
192 | }
193 | };
194 |
195 | private void setColorViewAlpha(int targetAlpha) {
196 | mCircleView.getBackground().setAlpha(targetAlpha);
197 | mProgress.setAlpha(targetAlpha);
198 | }
199 |
200 | /**
201 | * The refresh indicator starting and resting position is always positioned
202 | * near the top of the refreshing content. This position is a consistent
203 | * location, but can be adjusted in either direction based on whether or not
204 | * there is a toolbar or actionbar present.
205 | *
206 | * @param scale Set to true if there is no view at a higher z-order than
207 | * where the progress spinner is set to appear.
208 | * @param start The offset in pixels from the top of this view at which the
209 | * progress spinner should appear.
210 | * @param end The offset in pixels from the top of this view at which the
211 | * progress spinner should come to rest after a successful swipe
212 | * gesture.
213 | */
214 | /*
215 | public void setProgressViewOffset(boolean scale, int start, int end) {
216 | mScale = scale;
217 | mCircleView.setVisibility(View.GONE);
218 | mOriginalOffsetTop = mCurrentTargetOffsetTop = start;
219 | mSpinnerFinalOffset = end;
220 | mUsingCustomStart = true;
221 | mCircleView.invalidate();
222 | }*/
223 |
224 | /**
225 | * The refresh indicator resting position is always positioned near the top
226 | * of the refreshing content. This position is a consistent location, but
227 | * can be adjusted in either direction based on whether or not there is a
228 | * toolbar or actionbar present.
229 | *
230 | * @param scale Set to true if there is no view at a higher z-order than
231 | * where the progress spinner is set to appear.
232 | * @param end The offset in pixels from the top of this view at which the
233 | * progress spinner should come to rest after a successful swipe
234 | * gesture.
235 | */
236 | /*
237 | public void setProgressViewEndTarget(boolean scale, int end) {
238 | mSpinnerFinalOffset = end;
239 | mScale = scale;
240 | mCircleView.invalidate();
241 | }*/
242 |
243 | /**
244 | * One of DEFAULT, or LARGE.
245 | */
246 | public void setSize(int size) {
247 | if (size != MaterialProgressDrawable.LARGE && size != MaterialProgressDrawable.DEFAULT) {
248 | return;
249 | }
250 | final DisplayMetrics metrics = getResources().getDisplayMetrics();
251 | if (size == MaterialProgressDrawable.LARGE) {
252 | mCircleHeight = mCircleWidth = (int) (CIRCLE_DIAMETER_LARGE * metrics.density);
253 | } else {
254 | mCircleHeight = mCircleWidth = (int) (CIRCLE_DIAMETER * metrics.density);
255 | }
256 | // force the bounds of the progress circle inside the circle view to
257 | // update by setting it to null before updating its size and then
258 | // re-setting it
259 | mCircleView.setImageDrawable(null);
260 | mProgress.updateSizes(size);
261 | mCircleView.setImageDrawable(mProgress);
262 | }
263 |
264 | /**
265 | * Simple constructor to use when creating a SwipeRefreshLayout from code.
266 | *
267 | * @param context
268 | */
269 | public SwipyRefreshLayout(Context context) {
270 | this(context, null);
271 | }
272 |
273 | /**
274 | * Constructor that is called when inflating SwipeRefreshLayout from XML.
275 | *
276 | * @param context
277 | * @param attrs
278 | */
279 | public SwipyRefreshLayout(Context context, AttributeSet attrs) {
280 | super(context, attrs);
281 |
282 | mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
283 |
284 | mMediumAnimationDuration = getResources().getInteger(
285 | android.R.integer.config_mediumAnimTime);
286 |
287 | setWillNotDraw(false);
288 | mDecelerateInterpolator = new DecelerateInterpolator(DECELERATE_INTERPOLATION_FACTOR);
289 |
290 | final TypedArray a = context.obtainStyledAttributes(attrs, LAYOUT_ATTRS);
291 | setEnabled(a.getBoolean(0, true));
292 | a.recycle();
293 |
294 | final TypedArray a2 = context.obtainStyledAttributes(attrs, R.styleable.SwipyRefreshLayout);
295 | SwipyRefreshLayoutDirection direction
296 | = SwipyRefreshLayoutDirection.getFromInt(a2.getInt(R.styleable.SwipyRefreshLayout_srl_direction, 0));
297 | if (direction != SwipyRefreshLayoutDirection.BOTH) {
298 | mDirection = direction;
299 | mBothDirection = false;
300 | } else {
301 | mDirection = SwipyRefreshLayoutDirection.TOP;
302 | mBothDirection = true;
303 | }
304 | a2.recycle();
305 |
306 | final DisplayMetrics metrics = getResources().getDisplayMetrics();
307 | mCircleWidth = (int) (CIRCLE_DIAMETER * metrics.density);
308 | mCircleHeight = (int) (CIRCLE_DIAMETER * metrics.density);
309 |
310 | createProgressView();
311 | ViewCompat.setChildrenDrawingOrderEnabled(this, true);
312 | // the absolute offset has to take into account that the circle starts at an offset
313 | mSpinnerFinalOffset = DEFAULT_CIRCLE_TARGET * metrics.density;
314 | }
315 |
316 | protected int getChildDrawingOrder(int childCount, int i) {
317 | if (mCircleViewIndex < 0) {
318 | return i;
319 | } else if (i == childCount - 1) {
320 | // Draw the selected child last
321 | return mCircleViewIndex;
322 | } else if (i >= mCircleViewIndex) {
323 | // Move the children after the selected child earlier one
324 | return i + 1;
325 | } else {
326 | // Keep the children before the selected child the same
327 | return i;
328 | }
329 | }
330 |
331 | private void createProgressView() {
332 | mCircleView = new CircleImageView(getContext(), CIRCLE_BG_LIGHT, CIRCLE_DIAMETER / 2);
333 | mProgress = new MaterialProgressDrawable(getContext(), this);
334 | mProgress.setBackgroundColor(CIRCLE_BG_LIGHT);
335 | mCircleView.setImageDrawable(mProgress);
336 | mCircleView.setVisibility(View.GONE);
337 | addView(mCircleView);
338 | }
339 |
340 | /**
341 | * Set the listener to be notified when a refresh is triggered via the swipe
342 | * gesture.
343 | */
344 | public void setOnRefreshListener(OnRefreshListener listener) {
345 | mListener = listener;
346 | }
347 |
348 | /**
349 | * Pre API 11, alpha is used to make the progress circle appear instead of scale.
350 | */
351 | private boolean isAlphaUsedForScale() {
352 | return android.os.Build.VERSION.SDK_INT < 11;
353 | }
354 |
355 | /**
356 | * Notify the widget that refresh state has changed. Do not call this when
357 | * refresh is triggered by a swipe gesture.
358 | *
359 | * @param refreshing Whether or not the view should show refresh progress.
360 | */
361 | public void setRefreshing(boolean refreshing) {
362 | if (refreshing && mRefreshing != refreshing) {
363 | // scale and show
364 | mRefreshing = refreshing;
365 | int endTarget = 0;
366 | if (!mUsingCustomStart) {
367 | switch (mDirection) {
368 | case BOTTOM:
369 | endTarget = getMeasuredHeight() - (int) (mSpinnerFinalOffset);
370 | break;
371 | case TOP:
372 | default:
373 | endTarget = (int) (mSpinnerFinalOffset - Math.abs(mOriginalOffsetTop));
374 | break;
375 | }
376 | } else {
377 | endTarget = (int) mSpinnerFinalOffset;
378 | }
379 | setTargetOffsetTopAndBottom(endTarget - mCurrentTargetOffsetTop,
380 | true /* requires update */);
381 | mNotify = false;
382 | startScaleUpAnimation(mRefreshListener);
383 | } else {
384 | setRefreshing(refreshing, false /* notify */);
385 | }
386 | }
387 |
388 | private void startScaleUpAnimation(AnimationListener listener) {
389 | mCircleView.setVisibility(View.VISIBLE);
390 | if (android.os.Build.VERSION.SDK_INT >= 11) {
391 | // Pre API 11, alpha is used in place of scale up to show the
392 | // progress circle appearing.
393 | // Don't adjust the alpha during appearance otherwise.
394 | mProgress.setAlpha(MAX_ALPHA);
395 | }
396 | mScaleAnimation = new Animation() {
397 | @Override
398 | public void applyTransformation(float interpolatedTime, Transformation t) {
399 | setAnimationProgress(interpolatedTime);
400 | }
401 | };
402 | mScaleAnimation.setDuration(mMediumAnimationDuration);
403 | if (listener != null) {
404 | mCircleView.setAnimationListener(listener);
405 | }
406 | mCircleView.clearAnimation();
407 | mCircleView.startAnimation(mScaleAnimation);
408 | }
409 |
410 | /**
411 | * Pre API 11, this does an alpha animation.
412 | *
413 | * @param progress
414 | */
415 | private void setAnimationProgress(float progress) {
416 | if (isAlphaUsedForScale()) {
417 | setColorViewAlpha((int) (progress * MAX_ALPHA));
418 | } else {
419 | ViewCompat.setScaleX(mCircleView, progress);
420 | ViewCompat.setScaleY(mCircleView, progress);
421 | }
422 | }
423 |
424 | private void setRefreshing(boolean refreshing, final boolean notify) {
425 | if (mRefreshing != refreshing) {
426 | mNotify = notify;
427 | ensureTarget();
428 | mRefreshing = refreshing;
429 | if (mRefreshing) {
430 | animateOffsetToCorrectPosition(mCurrentTargetOffsetTop, mRefreshListener);
431 | } else {
432 | startScaleDownAnimation(mRefreshListener);
433 | }
434 | }
435 | }
436 |
437 | private void startScaleDownAnimation(AnimationListener listener) {
438 | mScaleDownAnimation = new Animation() {
439 | @Override
440 | public void applyTransformation(float interpolatedTime, Transformation t) {
441 | setAnimationProgress(1 - interpolatedTime);
442 | }
443 | };
444 | mScaleDownAnimation.setDuration(SCALE_DOWN_DURATION);
445 | mCircleView.setAnimationListener(listener);
446 | mCircleView.clearAnimation();
447 | mCircleView.startAnimation(mScaleDownAnimation);
448 | }
449 |
450 | private void startProgressAlphaStartAnimation() {
451 | mAlphaStartAnimation = startAlphaAnimation(mProgress.getAlpha(), STARTING_PROGRESS_ALPHA);
452 | }
453 |
454 | private void startProgressAlphaMaxAnimation() {
455 | mAlphaMaxAnimation = startAlphaAnimation(mProgress.getAlpha(), MAX_ALPHA);
456 | }
457 |
458 | private Animation startAlphaAnimation(final int startingAlpha, final int endingAlpha) {
459 | // Pre API 11, alpha is used in place of scale. Don't also use it to
460 | // show the trigger point.
461 | if (mScale && isAlphaUsedForScale()) {
462 | return null;
463 | }
464 | Animation alpha = new Animation() {
465 | @Override
466 | public void applyTransformation(float interpolatedTime, Transformation t) {
467 | mProgress
468 | .setAlpha((int) (startingAlpha + ((endingAlpha - startingAlpha)
469 | * interpolatedTime)));
470 | }
471 | };
472 | alpha.setDuration(ALPHA_ANIMATION_DURATION);
473 | // Clear out the previous animation listeners.
474 | mCircleView.setAnimationListener(null);
475 | mCircleView.clearAnimation();
476 | mCircleView.startAnimation(alpha);
477 | return alpha;
478 | }
479 |
480 | /**
481 | * Set the background color of the progress spinner disc.
482 | *
483 | * @param colorRes Resource id of the color.
484 | */
485 | public void setProgressBackgroundColor(int colorRes) {
486 | mCircleView.setBackgroundColor(colorRes);
487 | mProgress.setBackgroundColor(getResources().getColor(colorRes));
488 | }
489 |
490 | /**
491 | * @deprecated Use {@link #setColorSchemeResources(int...)}
492 | */
493 | @Deprecated
494 | public void setColorScheme(int... colors) {
495 | setColorSchemeResources(colors);
496 | }
497 |
498 | /**
499 | * Set the color resources used in the progress animation from color resources.
500 | * The first color will also be the color of the bar that grows in response
501 | * to a user swipe gesture.
502 | *
503 | * @param colorResIds
504 | */
505 | public void setColorSchemeResources(int... colorResIds) {
506 | final Resources res = getResources();
507 | int[] colorRes = new int[colorResIds.length];
508 | for (int i = 0; i < colorResIds.length; i++) {
509 | colorRes[i] = res.getColor(colorResIds[i]);
510 | }
511 | setColorSchemeColors(colorRes);
512 | }
513 |
514 | /**
515 | * Set the colors used in the progress animation. The first
516 | * color will also be the color of the bar that grows in response to a user
517 | * swipe gesture.
518 | *
519 | * @param colors
520 | */
521 | public void setColorSchemeColors(int... colors) {
522 | ensureTarget();
523 | mProgress.setColorSchemeColors(colors);
524 | }
525 |
526 | /**
527 | * @return Whether the SwipeRefreshWidget is actively showing refresh
528 | * progress.
529 | */
530 | public boolean isRefreshing() {
531 | return mRefreshing;
532 | }
533 |
534 | private void ensureTarget() {
535 | // Don't bother getting the parent height if the parent hasn't been laid
536 | // out yet.
537 | if (mTarget == null) {
538 | for (int i = 0; i < getChildCount(); i++) {
539 | View child = getChildAt(i);
540 | if (!child.equals(mCircleView)) {
541 | mTarget = child;
542 | break;
543 | }
544 | }
545 | }
546 | if (mTotalDragDistance == -1) {
547 | if (getParent() != null && ((View) getParent()).getHeight() > 0) {
548 | final DisplayMetrics metrics = getResources().getDisplayMetrics();
549 | mTotalDragDistance = (int) Math.min(
550 | ((View) getParent()).getHeight() * MAX_SWIPE_DISTANCE_FACTOR,
551 | REFRESH_TRIGGER_DISTANCE * metrics.density);
552 | }
553 | }
554 | }
555 |
556 | /**
557 | * Set the distance to trigger a sync in dips
558 | *
559 | * @param distance
560 | */
561 | public void setDistanceToTriggerSync(int distance) {
562 | mTotalDragDistance = distance;
563 | }
564 |
565 | @Override
566 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
567 | final int width = getMeasuredWidth();
568 | final int height = getMeasuredHeight();
569 | if (getChildCount() == 0) {
570 | return;
571 | }
572 | if (mTarget == null) {
573 | ensureTarget();
574 | }
575 | if (mTarget == null) {
576 | return;
577 | }
578 | final View child = mTarget;
579 | final int childLeft = getPaddingLeft();
580 | final int childTop = getPaddingTop();
581 | final int childWidth = width - getPaddingLeft() - getPaddingRight();
582 | final int childHeight = height - getPaddingTop() - getPaddingBottom();
583 | child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
584 | int circleWidth = mCircleView.getMeasuredWidth();
585 | int circleHeight = mCircleView.getMeasuredHeight();
586 | mCircleView.layout((width / 2 - circleWidth / 2), mCurrentTargetOffsetTop,
587 | (width / 2 + circleWidth / 2), mCurrentTargetOffsetTop + circleHeight);
588 | }
589 |
590 | @Override
591 | public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
592 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
593 | if (mTarget == null) {
594 | ensureTarget();
595 | }
596 | if (mTarget == null) {
597 | return;
598 | }
599 | mTarget.measure(MeasureSpec.makeMeasureSpec(
600 | getMeasuredWidth() - getPaddingLeft() - getPaddingRight(),
601 | MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
602 | getMeasuredHeight() - getPaddingTop() - getPaddingBottom(), MeasureSpec.EXACTLY));
603 | mCircleView.measure(MeasureSpec.makeMeasureSpec(mCircleWidth, MeasureSpec.EXACTLY),
604 | MeasureSpec.makeMeasureSpec(mCircleHeight, MeasureSpec.EXACTLY));
605 | if (!mUsingCustomStart && !mOriginalOffsetCalculated) {
606 | mOriginalOffsetCalculated = true;
607 |
608 | switch (mDirection) {
609 | case BOTTOM:
610 | mCurrentTargetOffsetTop = mOriginalOffsetTop = getMeasuredHeight();
611 | break;
612 | case TOP:
613 | default:
614 | mCurrentTargetOffsetTop = mOriginalOffsetTop = -mCircleView.getMeasuredHeight();
615 | break;
616 | }
617 | }
618 | mCircleViewIndex = -1;
619 | // Get the index of the circleview.
620 | for (int index = 0; index < getChildCount(); index++) {
621 | if (getChildAt(index) == mCircleView) {
622 | mCircleViewIndex = index;
623 | break;
624 | }
625 | }
626 | }
627 |
628 | /**
629 | * @return Whether it is possible for the child view of this layout to
630 | * scroll up. Override this if the child view is a custom view.
631 | */
632 | public boolean canChildScrollUp() {
633 | if (android.os.Build.VERSION.SDK_INT < 14) {
634 | if (mTarget instanceof AbsListView) {
635 | final AbsListView absListView = (AbsListView) mTarget;
636 | return absListView.getChildCount() > 0
637 | && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
638 | .getTop() < absListView.getPaddingTop());
639 | } else {
640 | return mTarget.getScrollY() > 0;
641 | }
642 | } else {
643 | return ViewCompat.canScrollVertically(mTarget, -1);
644 | }
645 | }
646 | // public boolean canChildScrollUp() {
647 | // if (android.os.Build.VERSION.SDK_INT < 14) {
648 | // if (mTarget instanceof AbsListView) {
649 | // final AbsListView absListView = (AbsListView) mTarget;
650 | // if (absListView.getLastVisiblePosition() + 1 == absListView.getCount()) {
651 | // int lastIndex = absListView.getLastVisiblePosition() - absListView.getFirstVisiblePosition();
652 | //
653 | // boolean res = absListView.getChildAt(lastIndex).getBottom() == absListView.getPaddingBottom();
654 | //
655 | // return res;
656 | // }
657 | // return true;
658 | // } else {
659 | // return mTarget.getScrollY() > 0;
660 | // }
661 | // } else {
662 | // return ViewCompat.canScrollVertically(mTarget, 1);
663 | // }
664 | // }
665 |
666 |
667 | public boolean canChildScrollDown() {
668 | if (android.os.Build.VERSION.SDK_INT < 14) {
669 | if (mTarget instanceof AbsListView) {
670 | final AbsListView absListView = (AbsListView) mTarget;
671 | try {
672 | if (absListView.getCount() > 0) {
673 | if (absListView.getLastVisiblePosition() + 1 == absListView.getCount()) {
674 | int lastIndex = absListView.getLastVisiblePosition() - absListView.getFirstVisiblePosition();
675 | return absListView.getChildAt(lastIndex).getBottom() == absListView.getPaddingBottom();
676 | }
677 | }
678 | } catch (Exception e) {
679 | e.printStackTrace();
680 | }
681 | return true;
682 | } else {
683 | return true;
684 | }
685 | } else {
686 | return ViewCompat.canScrollVertically(mTarget, 1);
687 | }
688 | }
689 |
690 | @Override
691 | public boolean onInterceptTouchEvent(MotionEvent ev) {
692 | ensureTarget();
693 |
694 | final int action = MotionEventCompat.getActionMasked(ev);
695 |
696 | if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
697 | mReturningToStart = false;
698 | }
699 |
700 | switch (mDirection) {
701 | case BOTTOM:
702 | if (!isEnabled() || mReturningToStart || (!mBothDirection && canChildScrollDown()) || mRefreshing) {
703 | // Fail fast if we're not in a state where a swipe is possible
704 | return false;
705 | }
706 | break;
707 | case TOP:
708 | default:
709 | if (!isEnabled() || mReturningToStart || (!mBothDirection && canChildScrollUp()) || mRefreshing) {
710 | // Fail fast if we're not in a state where a swipe is possible
711 | return false;
712 | }
713 | break;
714 | }
715 |
716 | switch (action) {
717 | case MotionEvent.ACTION_DOWN:
718 | setTargetOffsetTopAndBottom(mOriginalOffsetTop - mCircleView.getTop(), true);
719 | mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
720 | mIsBeingDragged = false;
721 | final float initialDownY = getMotionEventY(ev, mActivePointerId);
722 | if (initialDownY == -1) {
723 | return false;
724 | }
725 | mInitialDownY = initialDownY;
726 |
727 | case MotionEvent.ACTION_MOVE:
728 | if (mActivePointerId == INVALID_POINTER) {
729 | return false;
730 | }
731 |
732 | final float y = getMotionEventY(ev, mActivePointerId);
733 | if (y == -1) {
734 | return false;
735 | }
736 | if (mBothDirection) {
737 | if (y > mInitialDownY) {
738 | setRawDirection(SwipyRefreshLayoutDirection.TOP);
739 | } else if (y < mInitialDownY) {
740 | setRawDirection(SwipyRefreshLayoutDirection.BOTTOM);
741 | }
742 | if ((mDirection == SwipyRefreshLayoutDirection.BOTTOM && canChildScrollDown())
743 | || (mDirection == SwipyRefreshLayoutDirection.TOP && canChildScrollUp())) {
744 | mInitialDownY = y;
745 | return false;
746 | }
747 | }
748 | float yDiff;
749 | switch (mDirection) {
750 | case BOTTOM:
751 | yDiff = mInitialDownY - y;
752 | break;
753 | case TOP:
754 | default:
755 | yDiff = y - mInitialDownY;
756 | break;
757 | }
758 | if (yDiff > mTouchSlop && !mIsBeingDragged) {
759 | switch (mDirection) {
760 | case BOTTOM:
761 | mInitialMotionY = mInitialDownY - mTouchSlop;
762 | break;
763 | case TOP:
764 | default:
765 | mInitialMotionY = mInitialDownY + mTouchSlop;
766 | break;
767 | }
768 | mIsBeingDragged = true;
769 | mProgress.setAlpha(STARTING_PROGRESS_ALPHA);
770 | }
771 | break;
772 |
773 | case MotionEventCompat.ACTION_POINTER_UP:
774 | onSecondaryPointerUp(ev);
775 | break;
776 |
777 | case MotionEvent.ACTION_UP:
778 | case MotionEvent.ACTION_CANCEL:
779 | mIsBeingDragged = false;
780 | mActivePointerId = INVALID_POINTER;
781 | break;
782 | }
783 |
784 | return mIsBeingDragged;
785 | }
786 |
787 | private float getMotionEventY(MotionEvent ev, int activePointerId) {
788 | final int index = MotionEventCompat.findPointerIndex(ev, activePointerId);
789 | if (index < 0) {
790 | return -1;
791 | }
792 | return MotionEventCompat.getY(ev, index);
793 | }
794 |
795 | @Override
796 | public void requestDisallowInterceptTouchEvent(boolean b) {
797 | // Nope.
798 | }
799 |
800 | private boolean isAnimationRunning(Animation animation) {
801 | return animation != null && animation.hasStarted() && !animation.hasEnded();
802 | }
803 |
804 | @Override
805 | public boolean onTouchEvent(MotionEvent ev) {
806 | try {
807 | final int action = MotionEventCompat.getActionMasked(ev);
808 |
809 | if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
810 | mReturningToStart = false;
811 | }
812 |
813 | switch (mDirection) {
814 | case BOTTOM:
815 | if (!isEnabled() || mReturningToStart || canChildScrollDown() || mRefreshing) {
816 | // Fail fast if we're not in a state where a swipe is possible
817 | return false;
818 | }
819 | break;
820 | case TOP:
821 | default:
822 | if (!isEnabled() || mReturningToStart || canChildScrollUp() || mRefreshing) {
823 | // Fail fast if we're not in a state where a swipe is possible
824 | return false;
825 | }
826 | break;
827 | }
828 |
829 | switch (action) {
830 | case MotionEvent.ACTION_DOWN:
831 | mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
832 | mIsBeingDragged = false;
833 | break;
834 |
835 | case MotionEvent.ACTION_MOVE: {
836 | final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
837 | if (pointerIndex < 0) {
838 | return false;
839 | }
840 |
841 | final float y = MotionEventCompat.getY(ev, pointerIndex);
842 |
843 | float overscrollTop;
844 | switch (mDirection) {
845 | case BOTTOM:
846 | overscrollTop = (mInitialMotionY - y) * DRAG_RATE;
847 | break;
848 | case TOP:
849 | default:
850 | overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
851 | break;
852 | }
853 | if (mIsBeingDragged) {
854 | mProgress.showArrow(true);
855 | float originalDragPercent = overscrollTop / mTotalDragDistance;
856 | if (originalDragPercent < 0) {
857 | return false;
858 | }
859 | float dragPercent = Math.min(1f, Math.abs(originalDragPercent));
860 | float adjustedPercent = (float) Math.max(dragPercent - .4, 0) * 5 / 3;
861 | float extraOS = Math.abs(overscrollTop) - mTotalDragDistance;
862 | float slingshotDist = mUsingCustomStart ? mSpinnerFinalOffset
863 | - mOriginalOffsetTop : mSpinnerFinalOffset;
864 | float tensionSlingshotPercent = Math.max(0,
865 | Math.min(extraOS, slingshotDist * 2) / slingshotDist);
866 | float tensionPercent = (float) ((tensionSlingshotPercent / 4) - Math.pow(
867 | (tensionSlingshotPercent / 4), 2)) * 2f;
868 | float extraMove = (slingshotDist) * tensionPercent * 2;
869 |
870 | // int targetY = mOriginalOffsetTop + (int) ((slingshotDist * dragPercent) + extraMove);
871 | int targetY;
872 | if (mDirection == SwipyRefreshLayoutDirection.TOP) {
873 | targetY = mOriginalOffsetTop + (int) ((slingshotDist * dragPercent) + extraMove);
874 | } else {
875 | targetY = mOriginalOffsetTop - (int) ((slingshotDist * dragPercent) + extraMove);
876 | }
877 | // where 1.0f is a full circle
878 | if (mCircleView.getVisibility() != View.VISIBLE) {
879 | mCircleView.setVisibility(View.VISIBLE);
880 | }
881 | if (!mScale) {
882 | ViewCompat.setScaleX(mCircleView, 1f);
883 | ViewCompat.setScaleY(mCircleView, 1f);
884 | }
885 | if (overscrollTop < mTotalDragDistance) {
886 | if (mScale) {
887 | setAnimationProgress(overscrollTop / mTotalDragDistance);
888 | }
889 | if (mProgress.getAlpha() > STARTING_PROGRESS_ALPHA
890 | && !isAnimationRunning(mAlphaStartAnimation)) {
891 | // Animate the alpha
892 | startProgressAlphaStartAnimation();
893 | }
894 | float strokeStart = (float) (adjustedPercent * .8f);
895 | mProgress.setStartEndTrim(0f, Math.min(MAX_PROGRESS_ANGLE, strokeStart));
896 | mProgress.setArrowScale(Math.min(1f, adjustedPercent));
897 | } else {
898 | if (mProgress.getAlpha() < MAX_ALPHA
899 | && !isAnimationRunning(mAlphaMaxAnimation)) {
900 | // Animate the alpha
901 | startProgressAlphaMaxAnimation();
902 | }
903 | }
904 | float rotation = (-0.25f + .4f * adjustedPercent + tensionPercent * 2) * .5f;
905 | mProgress.setProgressRotation(rotation);
906 | setTargetOffsetTopAndBottom(targetY - mCurrentTargetOffsetTop,
907 | true /* requires update */);
908 | }
909 | break;
910 | }
911 | case MotionEventCompat.ACTION_POINTER_DOWN: {
912 | final int index = MotionEventCompat.getActionIndex(ev);
913 | mActivePointerId = MotionEventCompat.getPointerId(ev, index);
914 | break;
915 | }
916 |
917 | case MotionEventCompat.ACTION_POINTER_UP:
918 | onSecondaryPointerUp(ev);
919 | break;
920 |
921 | case MotionEvent.ACTION_UP:
922 | case MotionEvent.ACTION_CANCEL: {
923 | if (mActivePointerId == INVALID_POINTER) {
924 | if (action == MotionEvent.ACTION_UP) {
925 | }
926 | return false;
927 | }
928 | final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
929 | final float y = MotionEventCompat.getY(ev, pointerIndex);
930 |
931 | float overscrollTop;
932 | switch (mDirection) {
933 | case BOTTOM:
934 | overscrollTop = (mInitialMotionY - y) * DRAG_RATE;
935 | break;
936 | case TOP:
937 | default:
938 | overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
939 | break;
940 | }
941 | mIsBeingDragged = false;
942 | if (overscrollTop > mTotalDragDistance) {
943 | setRefreshing(true, true /* notify */);
944 | } else {
945 | // cancel refresh
946 | mRefreshing = false;
947 | mProgress.setStartEndTrim(0f, 0f);
948 | AnimationListener listener = null;
949 | if (!mScale) {
950 | listener = new AnimationListener() {
951 |
952 | @Override
953 | public void onAnimationStart(Animation animation) {
954 | }
955 |
956 | @Override
957 | public void onAnimationEnd(Animation animation) {
958 | if (!mScale) {
959 | startScaleDownAnimation(null);
960 | }
961 | }
962 |
963 | @Override
964 | public void onAnimationRepeat(Animation animation) {
965 | }
966 |
967 | };
968 | }
969 | animateOffsetToStartPosition(mCurrentTargetOffsetTop, listener);
970 | mProgress.showArrow(false);
971 | }
972 | mActivePointerId = INVALID_POINTER;
973 | return false;
974 | }
975 | }
976 | } catch (Exception e) {
977 | Log.e(TAG, "An exception occured during SwipyRefreshLayout onTouchEvent " + e.toString());
978 | }
979 |
980 | return true;
981 | }
982 |
983 | private void animateOffsetToCorrectPosition(int from, AnimationListener listener) {
984 | mFrom = from;
985 | mAnimateToCorrectPosition.reset();
986 | mAnimateToCorrectPosition.setDuration(ANIMATE_TO_TRIGGER_DURATION);
987 | mAnimateToCorrectPosition.setInterpolator(mDecelerateInterpolator);
988 | if (listener != null) {
989 | mCircleView.setAnimationListener(listener);
990 | }
991 | mCircleView.clearAnimation();
992 | mCircleView.startAnimation(mAnimateToCorrectPosition);
993 | }
994 |
995 | private void animateOffsetToStartPosition(int from, AnimationListener listener) {
996 | if (mScale) {
997 | // Scale the item back down
998 | startScaleDownReturnToStartAnimation(from, listener);
999 | } else {
1000 | mFrom = from;
1001 | mAnimateToStartPosition.reset();
1002 | mAnimateToStartPosition.setDuration(ANIMATE_TO_START_DURATION);
1003 | mAnimateToStartPosition.setInterpolator(mDecelerateInterpolator);
1004 | if (listener != null) {
1005 | mCircleView.setAnimationListener(listener);
1006 | }
1007 | mCircleView.clearAnimation();
1008 | mCircleView.startAnimation(mAnimateToStartPosition);
1009 | }
1010 | }
1011 |
1012 | private final Animation mAnimateToCorrectPosition = new Animation() {
1013 | @Override
1014 | public void applyTransformation(float interpolatedTime, Transformation t) {
1015 | int targetTop = 0;
1016 | int endTarget = 0;
1017 | if (!mUsingCustomStart) {
1018 | switch (mDirection) {
1019 | case BOTTOM:
1020 | endTarget = getMeasuredHeight() - (int) (mSpinnerFinalOffset);
1021 | break;
1022 | case TOP:
1023 | default:
1024 | endTarget = (int) (mSpinnerFinalOffset - Math.abs(mOriginalOffsetTop));
1025 | break;
1026 | }
1027 | } else {
1028 | endTarget = (int) mSpinnerFinalOffset;
1029 | }
1030 | targetTop = (mFrom + (int) ((endTarget - mFrom) * interpolatedTime));
1031 | int offset = targetTop - mCircleView.getTop();
1032 | setTargetOffsetTopAndBottom(offset, false /* requires update */);
1033 | }
1034 | };
1035 |
1036 | private void moveToStart(float interpolatedTime) {
1037 | int targetTop = 0;
1038 | targetTop = (mFrom + (int) ((mOriginalOffsetTop - mFrom) * interpolatedTime));
1039 | int offset = targetTop - mCircleView.getTop();
1040 | setTargetOffsetTopAndBottom(offset, false /* requires update */);
1041 | }
1042 |
1043 | private final Animation mAnimateToStartPosition = new Animation() {
1044 | @Override
1045 | public void applyTransformation(float interpolatedTime, Transformation t) {
1046 | moveToStart(interpolatedTime);
1047 | }
1048 | };
1049 |
1050 | private void startScaleDownReturnToStartAnimation(int from,
1051 | AnimationListener listener) {
1052 | mFrom = from;
1053 | if (isAlphaUsedForScale()) {
1054 | mStartingScale = mProgress.getAlpha();
1055 | } else {
1056 | mStartingScale = ViewCompat.getScaleX(mCircleView);
1057 | }
1058 | mScaleDownToStartAnimation = new Animation() {
1059 | @Override
1060 | public void applyTransformation(float interpolatedTime, Transformation t) {
1061 | float targetScale = (mStartingScale + (-mStartingScale * interpolatedTime));
1062 | setAnimationProgress(targetScale);
1063 | moveToStart(interpolatedTime);
1064 | }
1065 | };
1066 | mScaleDownToStartAnimation.setDuration(SCALE_DOWN_DURATION);
1067 | if (listener != null) {
1068 | mCircleView.setAnimationListener(listener);
1069 | }
1070 | mCircleView.clearAnimation();
1071 | mCircleView.startAnimation(mScaleDownToStartAnimation);
1072 | }
1073 |
1074 | private void setTargetOffsetTopAndBottom(int offset, boolean requiresUpdate) {
1075 | mCircleView.bringToFront();
1076 | mCircleView.offsetTopAndBottom(offset);
1077 |
1078 | // switch (mDirection) {
1079 | // case BOTTOM:
1080 | // mCurrentTargetOffsetTop = getMeasuredHeight() - mCircleView.getMeasuredHeight();
1081 | // break;
1082 | // case TOP:
1083 | // default:
1084 | // mCurrentTargetOffsetTop = mCircleView.getTop();
1085 | // break;
1086 | // }
1087 | mCurrentTargetOffsetTop = mCircleView.getTop();
1088 | if (requiresUpdate && android.os.Build.VERSION.SDK_INT < 11) {
1089 | invalidate();
1090 | }
1091 | }
1092 |
1093 | private void onSecondaryPointerUp(MotionEvent ev) {
1094 | final int pointerIndex = MotionEventCompat.getActionIndex(ev);
1095 | final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
1096 | if (pointerId == mActivePointerId) {
1097 | // This was our active pointer going up. Choose a new
1098 | // active pointer and adjust accordingly.
1099 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1100 | mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
1101 | }
1102 | }
1103 |
1104 | /**
1105 | * Classes that wish to be notified when the swipe gesture correctly
1106 | * triggers a refresh should implement this interface.
1107 | */
1108 | public interface OnRefreshListener {
1109 | public void onRefresh(SwipyRefreshLayoutDirection direction);
1110 | }
1111 |
1112 | public SwipyRefreshLayoutDirection getDirection() {
1113 | return mBothDirection ? SwipyRefreshLayoutDirection.BOTH : mDirection;
1114 | }
1115 |
1116 | public void setDirection(SwipyRefreshLayoutDirection direction) {
1117 | if (direction == SwipyRefreshLayoutDirection.BOTH) {
1118 | mBothDirection = true;
1119 | } else {
1120 | mBothDirection = false;
1121 | mDirection = direction;
1122 | }
1123 |
1124 | switch (mDirection) {
1125 | case BOTTOM:
1126 | mCurrentTargetOffsetTop = mOriginalOffsetTop = getMeasuredHeight();
1127 | break;
1128 | case TOP:
1129 | default:
1130 | mCurrentTargetOffsetTop = mOriginalOffsetTop = -mCircleView.getMeasuredHeight();
1131 | break;
1132 | }
1133 | }
1134 |
1135 | // only TOP or Bottom
1136 | private void setRawDirection(SwipyRefreshLayoutDirection direction) {
1137 | if (mDirection == direction) {
1138 | return;
1139 | }
1140 |
1141 | mDirection = direction;
1142 | switch (mDirection) {
1143 | case BOTTOM:
1144 | mCurrentTargetOffsetTop = mOriginalOffsetTop = getMeasuredHeight();
1145 | break;
1146 | case TOP:
1147 | default:
1148 | mCurrentTargetOffsetTop = mOriginalOffsetTop = -mCircleView.getMeasuredHeight();
1149 | break;
1150 | }
1151 | }
1152 |
1153 | public CircleImageView getCircleView() {
1154 | return mCircleView;
1155 | }
1156 | }
1157 |
--------------------------------------------------------------------------------