├── PullToRefresh
├── .gitignore
├── src
│ └── main
│ │ ├── ic_launcher-web.png
│ │ ├── res
│ │ ├── drawable-hdpi
│ │ │ ├── arrow_up.png
│ │ │ ├── arrow_down.png
│ │ │ └── ic_launcher.png
│ │ ├── drawable-mdpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable-xhdpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable-xxhdpi
│ │ │ └── ic_launcher.png
│ │ ├── values
│ │ │ ├── color.xml
│ │ │ ├── dimens.xml
│ │ │ ├── styles.xml
│ │ │ └── strings.xml
│ │ ├── values-sw600dp
│ │ │ └── dimens.xml
│ │ ├── menu
│ │ │ └── main.xml
│ │ ├── values-sw720dp-land
│ │ │ └── dimens.xml
│ │ ├── layout
│ │ │ ├── vw_list_item.xml
│ │ │ ├── act_list_view.xml
│ │ │ ├── act_scroll_view.xml
│ │ │ ├── vw_scroll_view_content.xml
│ │ │ ├── vw_xscrollview_layout.xml
│ │ │ ├── act_main.xml
│ │ │ ├── vw_footer.xml
│ │ │ └── vw_header.xml
│ │ ├── values-v11
│ │ │ └── styles.xml
│ │ └── values-v14
│ │ │ └── styles.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── markmao
│ │ └── pulltorefresh
│ │ ├── ui
│ │ ├── MainActivity.java
│ │ ├── XListViewActivity.java
│ │ └── XScrollViewActivity.java
│ │ └── widget
│ │ ├── XHeaderView.java
│ │ ├── XFooterView.java
│ │ ├── XListView.java
│ │ └── XScrollView.java
└── build.gradle
├── settings.gradle
├── Screenshots
├── 0.png
├── 1.png
├── 2.png
└── 3.png
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradlew.bat
├── README.md
├── gradlew
└── LICENSE
/PullToRefresh/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':PullToRefresh'
2 |
--------------------------------------------------------------------------------
/Screenshots/0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkMjw/PullToRefresh/HEAD/Screenshots/0.png
--------------------------------------------------------------------------------
/Screenshots/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkMjw/PullToRefresh/HEAD/Screenshots/1.png
--------------------------------------------------------------------------------
/Screenshots/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkMjw/PullToRefresh/HEAD/Screenshots/2.png
--------------------------------------------------------------------------------
/Screenshots/3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkMjw/PullToRefresh/HEAD/Screenshots/3.png
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkMjw/PullToRefresh/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/PullToRefresh/src/main/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkMjw/PullToRefresh/HEAD/PullToRefresh/src/main/ic_launcher-web.png
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/drawable-hdpi/arrow_up.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkMjw/PullToRefresh/HEAD/PullToRefresh/src/main/res/drawable-hdpi/arrow_up.png
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/drawable-hdpi/arrow_down.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkMjw/PullToRefresh/HEAD/PullToRefresh/src/main/res/drawable-hdpi/arrow_down.png
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkMjw/PullToRefresh/HEAD/PullToRefresh/src/main/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkMjw/PullToRefresh/HEAD/PullToRefresh/src/main/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkMjw/PullToRefresh/HEAD/PullToRefresh/src/main/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkMjw/PullToRefresh/HEAD/PullToRefresh/src/main/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/values/color.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #00000000
4 | #F7F4F0
5 | #333333
6 |
7 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/values-sw600dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Mar 01 23:19:10 GMT+08:00 2014
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
7 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/menu/main.xml:
--------------------------------------------------------------------------------
1 |
7 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/values-sw720dp-land/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 128dp
5 |
6 |
--------------------------------------------------------------------------------
/PullToRefresh/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 21
5 | buildToolsVersion "21.1.2"
6 | compileOptions.encoding = "UTF-8"
7 |
8 | defaultConfig {
9 | minSdkVersion 8
10 | targetSdkVersion 21
11 | }
12 | }
13 |
14 | dependencies {
15 | compile 'com.android.support:appcompat-v7:21.0.3'
16 | }
17 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/layout/vw_list_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/values-v11/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 | 60dp
7 | 90dp
8 | 10dp
9 |
10 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/values-v14/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # built application files
2 | *.apk
3 | *.ap_
4 |
5 | # files for the dex VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # generated files
12 | bin/
13 | gen/
14 |
15 | # Local configuration file (sdk path, etc)
16 | local.properties
17 |
18 | # Eclipse project files
19 | .classpath
20 | .project
21 |
22 | # Proguard folder generated by Eclipse
23 | proguard/
24 |
25 | # Intellij project files
26 | *.iml
27 | *.ipr
28 | *.iws
29 | .idea/
30 | /.gradle/
31 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/layout/act_list_view.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/layout/act_scroll_view.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/layout/vw_scroll_view_content.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
14 |
15 |
16 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | PullToRefresh
5 | Settings
6 | Hello world!
7 |
8 | ScrollView demo
9 | ListView demo
10 |
11 | Pull to refresh
12 | Release to refresh
13 | Refreshing…
14 | Time:
15 | Load more
16 | Release to load more
17 |
18 |
19 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/layout/vw_xscrollview_layout.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
8 |
14 |
15 |
21 |
22 |
28 |
29 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
25 |
26 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/layout/act_main.xml:
--------------------------------------------------------------------------------
1 |
11 |
12 |
20 |
21 |
28 |
29 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/java/com/markmao/pulltorefresh/ui/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.markmao.pulltorefresh.ui;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.view.Menu;
6 | import android.view.View;
7 | import android.widget.Button;
8 |
9 | import com.markmao.pulltorefresh.R;
10 |
11 | public class MainActivity extends Activity implements View.OnClickListener {
12 |
13 | @Override
14 | protected void onCreate(Bundle savedInstanceState) {
15 | super.onCreate(savedInstanceState);
16 | setContentView(R.layout.act_main);
17 |
18 | Button listBrn = (Button) findViewById(R.id.list_view_btn);
19 | listBrn.setOnClickListener(this);
20 |
21 | Button scrollBrn = (Button) findViewById(R.id.scroll_view_btn);
22 | scrollBrn.setOnClickListener(this);
23 | }
24 |
25 | @Override
26 | public boolean onCreateOptionsMenu(Menu menu) {
27 | // Inflate the menu; this adds items to the action bar if it is present.
28 | getMenuInflater().inflate(R.menu.main, menu);
29 | return true;
30 | }
31 |
32 | @Override
33 | public void onClick(View v) {
34 | switch (v.getId()) {
35 | case R.id.list_view_btn:
36 | XListViewActivity.launch(this);
37 | break;
38 |
39 | case R.id.scroll_view_btn:
40 | XScrollViewActivity.launch(this);
41 | break;
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/layout/vw_footer.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
17 |
18 |
25 |
26 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/res/layout/vw_header.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
12 |
13 |
20 |
21 |
26 |
27 |
31 |
32 |
37 |
38 |
43 |
44 |
45 |
46 |
54 |
55 |
64 |
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/java/com/markmao/pulltorefresh/ui/XListViewActivity.java:
--------------------------------------------------------------------------------
1 | package com.markmao.pulltorefresh.ui;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.os.Bundle;
7 | import android.os.Handler;
8 | import android.widget.ArrayAdapter;
9 |
10 | import com.markmao.pulltorefresh.R;
11 | import com.markmao.pulltorefresh.widget.XListView;
12 |
13 | import java.text.SimpleDateFormat;
14 | import java.util.ArrayList;
15 | import java.util.Date;
16 | import java.util.Locale;
17 |
18 | /**
19 | * XListView demo
20 | *
21 | * @author markmjw
22 | * @date 2013-10-08
23 | */
24 | public class XListViewActivity extends Activity implements XListView.IXListViewListener {
25 | private XListView mListView;
26 |
27 | private ArrayAdapter mAdapter;
28 | private ArrayList items = new ArrayList();
29 | private Handler mHandler;
30 | private int mIndex = 0;
31 | private int mRefreshIndex = 0;
32 |
33 | public static void launch(Context context) {
34 | Intent intent = new Intent();
35 | intent.setClass(context, XListViewActivity.class);
36 | intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
37 |
38 | context.startActivity(intent);
39 | }
40 |
41 | @Override
42 | protected void onCreate(Bundle savedInstanceState) {
43 | super.onCreate(savedInstanceState);
44 | setContentView(R.layout.act_list_view);
45 |
46 | geneItems();
47 | initView();
48 | }
49 |
50 | private void initView() {
51 | mHandler = new Handler();
52 |
53 | mListView = (XListView) findViewById(R.id.list_view);
54 | mListView.setPullRefreshEnable(true);
55 | mListView.setPullLoadEnable(true);
56 | mListView.setAutoLoadEnable(true);
57 | mListView.setXListViewListener(this);
58 | mListView.setRefreshTime(getTime());
59 |
60 | mAdapter = new ArrayAdapter(this, R.layout.vw_list_item, items);
61 | mListView.setAdapter(mAdapter);
62 | }
63 |
64 | @Override
65 | public void onWindowFocusChanged(boolean hasFocus) {
66 | super.onWindowFocusChanged(hasFocus);
67 |
68 | if (hasFocus) {
69 | mListView.autoRefresh();
70 | }
71 | }
72 |
73 | @Override
74 | public void onRefresh() {
75 | mHandler.postDelayed(new Runnable() {
76 | @Override
77 | public void run() {
78 | mIndex = ++mRefreshIndex;
79 | items.clear();
80 | geneItems();
81 | mAdapter = new ArrayAdapter(XListViewActivity.this, R.layout.vw_list_item,
82 | items);
83 | mListView.setAdapter(mAdapter);
84 | onLoad();
85 | }
86 | }, 2500);
87 | }
88 |
89 | @Override
90 | public void onLoadMore() {
91 | mHandler.postDelayed(new Runnable() {
92 | @Override
93 | public void run() {
94 | geneItems();
95 | mAdapter.notifyDataSetChanged();
96 | onLoad();
97 | }
98 | }, 2500);
99 | }
100 |
101 | private void geneItems() {
102 | for (int i = 0; i != 20; ++i) {
103 | items.add("Test XListView item " + (++mIndex));
104 | }
105 | }
106 |
107 | private void onLoad() {
108 | mListView.stopRefresh();
109 | mListView.stopLoadMore();
110 | mListView.setRefreshTime(getTime());
111 | }
112 |
113 | private String getTime() {
114 | return new SimpleDateFormat("MM-dd HH:mm", Locale.CHINA).format(new Date());
115 | }
116 | }
117 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | PullToRefresh
2 | ===========
3 |
4 | *注:本项目使用Android Studio开发*
5 |
6 | ### 根据Maxwin的XListView改造而来: ###
7 | 1.完善下拉刷新上拉加载更多的功能
8 | 2.并增加对ScrollView的支持
9 | 3.增加自动刷新以及滚动到底部自动加载功能
10 |
11 | 原XListView参考链接:https://github.com/Maxwin-z/XListView-Android
12 |
13 | ## **XListView使用示例** ##
14 |
15 | 设置XListView相关属性
16 | ```java
17 | mListView = (XListView) findViewById(R.id.list_view);
18 | mListView.setPullRefreshEnable(true);
19 | mListView.setPullLoadEnable(true);
20 | mListView.setAutoLoadEnable(true);
21 | mListView.setXListViewListener(this);
22 | mListView.setRefreshTime(getTime());
23 |
24 | mAdapter = new ArrayAdapter(this, R.layout.vw_list_item, items);
25 | mListView.setAdapter(mAdapter);
26 | ```
27 |
28 | 下拉刷新,下拉加载更多使用示例
29 | ```java
30 | @Override
31 | public void onRefresh() {
32 | mHandler.postDelayed(new Runnable() {
33 | @Override
34 | public void run() {
35 | mIndex = ++mRefreshIndex;
36 | items.clear();
37 | geneItems();
38 | mAdapter = new ArrayAdapter(XListViewActivity.this, R.layout.vw_list_item,
39 | items);
40 | mListView.setAdapter(mAdapter);
41 | onLoad();
42 | }
43 | }, 2500);
44 | }
45 |
46 | @Override
47 | public void onWindowFocusChanged(boolean hasFocus) {
48 | super.onWindowFocusChanged(hasFocus);
49 | if (hasFocus) {
50 | mListView.autoRefresh();
51 | }
52 | }
53 |
54 | @Override
55 | public void onLoadMore() {
56 | mHandler.postDelayed(new Runnable() {
57 | @Override
58 | public void run() {
59 | geneItems();
60 | mAdapter.notifyDataSetChanged();
61 | onLoad();
62 | }
63 | }, 2500);
64 | }
65 | ```
66 |
67 |
68 | ## **XScrollView使用示例** ##
69 |
70 | 设置XScrollView相关属性
71 | ```java
72 | mScrollView = (XScrollView) findViewById(R.id.scroll_view);
73 | mScrollView.setPullRefreshEnable(true);
74 | mScrollView.setPullLoadEnable(true);
75 | mScrollView.setAutoLoadEnable(true);
76 | mScrollView.setIXScrollViewListener(this);
77 | mScrollView.setRefreshTime(getTime());
78 |
79 | View content = LayoutInflater.from(this).inflate(R.layout.vw_scroll_view_content, null);
80 |
81 | if (null != content) {
82 | mListView = (ListView) content.findViewById(R.id.content_list);
83 | mListView.setFocusable(false);
84 | mListView.setFocusableInTouchMode(false);
85 |
86 | mAdapter = new ArrayAdapter(this, R.layout.vw_list_item, mItems);
87 | mListView.setAdapter(mAdapter);
88 | measureHeight();
89 | }
90 |
91 | mScrollView.setView(content);
92 | ```
93 |
94 | 下拉刷新,下拉加载更多使用示例
95 | ```java
96 | @Override
97 | public void onRefresh() {
98 | mHandler.postDelayed(new Runnable() {
99 | @Override
100 | public void run() {
101 | mIndex = ++mRefreshIndex;
102 | mItems.clear();
103 | geneItems();
104 | mAdapter = new ArrayAdapter(XScrollViewActivity.this,
105 | R.layout.vw_list_item, mItems);
106 | mListView.setAdapter(mAdapter);
107 | measureHeight();
108 | onLoad();
109 | }
110 | }, 2500);
111 | }
112 |
113 | @Override
114 | public void onWindowFocusChanged(boolean hasFocus) {
115 | super.onWindowFocusChanged(hasFocus);
116 |
117 | if (hasFocus) {
118 | mScrollView.autoRefresh();
119 | }
120 | }
121 |
122 | @Override
123 | public void onLoadMore() {
124 | mHandler.postDelayed(new Runnable() {
125 | @Override
126 | public void run() {
127 | geneItems();
128 | mAdapter.notifyDataSetChanged();
129 | measureHeight();
130 | onLoad();
131 | }
132 | }, 2500);
133 | }
134 | ```
135 |
136 | ##Screenshots
137 | 
138 |
139 | 
140 |
141 | 
142 |
143 | 
144 |
145 | License
146 | =======
147 |
148 | Copyright (C) 2014 MarkMjw
149 |
150 | Licensed under the Apache License, Version 2.0 (the "License");
151 | you may not use this file except in compliance with the License.
152 | You may obtain a copy of the License at
153 |
154 | http://www.apache.org/licenses/LICENSE-2.0
155 |
156 | Unless required by applicable law or agreed to in writing, software
157 | distributed under the License is distributed on an "AS IS" BASIS,
158 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
159 | See the License for the specific language governing permissions and
160 | limitations under the License.
161 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/java/com/markmao/pulltorefresh/widget/XHeaderView.java:
--------------------------------------------------------------------------------
1 | package com.markmao.pulltorefresh.widget;
2 |
3 | import android.content.Context;
4 | import android.util.AttributeSet;
5 | import android.view.Gravity;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.animation.Animation;
9 | import android.view.animation.RotateAnimation;
10 | import android.widget.ImageView;
11 | import android.widget.LinearLayout;
12 | import android.widget.ProgressBar;
13 | import android.widget.TextView;
14 |
15 | import com.markmao.pulltorefresh.R;
16 |
17 | /**
18 | * The header view for {@link com.markmao.pulltorefresh.widget.XListView} and
19 | * {@link com.markmao.pulltorefresh.widget.XScrollView}
20 | *
21 | * @author markmjw
22 | * @date 2013-10-08
23 | */
24 | public class XHeaderView extends LinearLayout {
25 | public final static int STATE_NORMAL = 0;
26 | public final static int STATE_READY = 1;
27 | public final static int STATE_REFRESHING = 2;
28 |
29 | private final int ROTATE_ANIM_DURATION = 180;
30 |
31 | private LinearLayout mContainer;
32 |
33 | private ImageView mArrowImageView;
34 |
35 | private ProgressBar mProgressBar;
36 |
37 | private TextView mHintTextView;
38 |
39 | private int mState = STATE_NORMAL;
40 |
41 | private Animation mRotateUpAnim;
42 | private Animation mRotateDownAnim;
43 |
44 | private boolean mIsFirst;
45 |
46 | public XHeaderView(Context context) {
47 | super(context);
48 | initView(context);
49 | }
50 |
51 | public XHeaderView(Context context, AttributeSet attrs) {
52 | super(context, attrs);
53 | initView(context);
54 | }
55 |
56 | private void initView(Context context) {
57 | // Initial set header view height 0
58 | LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0);
59 | mContainer = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.vw_header, null);
60 | addView(mContainer, lp);
61 | setGravity(Gravity.BOTTOM);
62 |
63 | mArrowImageView = (ImageView) findViewById(R.id.header_arrow);
64 | mHintTextView = (TextView) findViewById(R.id.header_hint_text);
65 | mProgressBar = (ProgressBar) findViewById(R.id.header_progressbar);
66 |
67 | mRotateUpAnim = new RotateAnimation(0.0f, -180.0f, Animation.RELATIVE_TO_SELF, 0.5f,
68 | Animation.RELATIVE_TO_SELF, 0.5f);
69 | mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
70 | mRotateUpAnim.setFillAfter(true);
71 |
72 | mRotateDownAnim = new RotateAnimation(-180.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f,
73 | Animation.RELATIVE_TO_SELF, 0.5f);
74 | mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
75 | mRotateDownAnim.setFillAfter(true);
76 | }
77 |
78 | public void setState(int state) {
79 | if (state == mState && mIsFirst) {
80 | mIsFirst = true;
81 | return;
82 | }
83 |
84 | if (state == STATE_REFRESHING) {
85 | // show progress
86 | mArrowImageView.clearAnimation();
87 | mArrowImageView.setVisibility(View.INVISIBLE);
88 | mProgressBar.setVisibility(View.VISIBLE);
89 | } else {
90 | // show arrow image
91 | mArrowImageView.setVisibility(View.VISIBLE);
92 | mProgressBar.setVisibility(View.INVISIBLE);
93 | }
94 |
95 | switch (state) {
96 | case STATE_NORMAL:
97 | if (mState == STATE_READY) {
98 | mArrowImageView.startAnimation(mRotateDownAnim);
99 | }
100 |
101 | if (mState == STATE_REFRESHING) {
102 | mArrowImageView.clearAnimation();
103 | }
104 |
105 | mHintTextView.setText(R.string.header_hint_refresh_normal);
106 | break;
107 |
108 | case STATE_READY:
109 | if (mState != STATE_READY) {
110 | mArrowImageView.clearAnimation();
111 | mArrowImageView.startAnimation(mRotateUpAnim);
112 | mHintTextView.setText(R.string.header_hint_refresh_ready);
113 | }
114 | break;
115 |
116 | case STATE_REFRESHING:
117 | mHintTextView.setText(R.string.header_hint_refresh_loading);
118 | break;
119 |
120 | default:
121 | break;
122 | }
123 |
124 | mState = state;
125 | }
126 |
127 | /**
128 | * Set the header view visible height.
129 | *
130 | * @param height
131 | */
132 | public void setVisibleHeight(int height) {
133 | if (height < 0) height = 0;
134 | LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mContainer.getLayoutParams();
135 | lp.height = height;
136 | mContainer.setLayoutParams(lp);
137 | }
138 |
139 | /**
140 | * Get the header view visible height.
141 | *
142 | * @return
143 | */
144 | public int getVisibleHeight() {
145 | return mContainer.getLayoutParams().height;
146 | }
147 |
148 | }
149 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/java/com/markmao/pulltorefresh/ui/XScrollViewActivity.java:
--------------------------------------------------------------------------------
1 | package com.markmao.pulltorefresh.ui;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.os.Bundle;
7 | import android.os.Handler;
8 | import android.view.LayoutInflater;
9 | import android.view.View;
10 | import android.view.ViewGroup;
11 | import android.widget.ArrayAdapter;
12 | import android.widget.ListAdapter;
13 | import android.widget.ListView;
14 | import com.markmao.pulltorefresh.R;
15 | import com.markmao.pulltorefresh.widget.XScrollView;
16 |
17 | import java.text.SimpleDateFormat;
18 | import java.util.ArrayList;
19 | import java.util.Date;
20 | import java.util.Locale;
21 |
22 | /**
23 | * XScrollView demo
24 | *
25 | * @author markmjw
26 | * @date 2013-10-08
27 | */
28 | public class XScrollViewActivity extends Activity implements XScrollView.IXScrollViewListener {
29 | private XScrollView mScrollView;
30 |
31 | private ListView mListView;
32 |
33 | private ArrayAdapter mAdapter;
34 | private ArrayList mItems = new ArrayList();
35 | private Handler mHandler;
36 | private int mIndex = 0;
37 | private int mRefreshIndex = 0;
38 |
39 | public static void launch(Context context) {
40 | Intent intent = new Intent();
41 | intent.setClass(context, XScrollViewActivity.class);
42 | intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
43 |
44 | context.startActivity(intent);
45 | }
46 |
47 | @Override
48 | protected void onCreate(Bundle savedInstanceState) {
49 | super.onCreate(savedInstanceState);
50 | setContentView(R.layout.act_scroll_view);
51 |
52 | geneItems();
53 | initView();
54 | }
55 |
56 | protected void initView() {
57 | mHandler = new Handler();
58 |
59 | mScrollView = (XScrollView) findViewById(R.id.scroll_view);
60 | mScrollView.setPullRefreshEnable(true);
61 | mScrollView.setPullLoadEnable(true);
62 | mScrollView.setAutoLoadEnable(true);
63 | mScrollView.setIXScrollViewListener(this);
64 | mScrollView.setRefreshTime(getTime());
65 |
66 | View content = LayoutInflater.from(this).inflate(R.layout.vw_scroll_view_content, null);
67 |
68 | if (null != content) {
69 | mListView = (ListView) content.findViewById(R.id.content_list);
70 | mListView.setFocusable(false);
71 | mListView.setFocusableInTouchMode(false);
72 |
73 | mAdapter = new ArrayAdapter(this, R.layout.vw_list_item, mItems);
74 | mListView.setAdapter(mAdapter);
75 | measureHeight();
76 | }
77 |
78 | mScrollView.setView(content);
79 | }
80 |
81 | @Override
82 | public void onWindowFocusChanged(boolean hasFocus) {
83 | super.onWindowFocusChanged(hasFocus);
84 |
85 | if (hasFocus) {
86 | mScrollView.autoRefresh();
87 | }
88 | }
89 |
90 | @Override
91 | public void onRefresh() {
92 | mHandler.postDelayed(new Runnable() {
93 | @Override
94 | public void run() {
95 | mIndex = ++mRefreshIndex;
96 | mItems.clear();
97 | geneItems();
98 | mAdapter = new ArrayAdapter(XScrollViewActivity.this,
99 | R.layout.vw_list_item, mItems);
100 | mListView.setAdapter(mAdapter);
101 | measureHeight();
102 | onLoad();
103 | }
104 | }, 2500);
105 | }
106 |
107 | @Override
108 | public void onLoadMore() {
109 | mHandler.postDelayed(new Runnable() {
110 | @Override
111 | public void run() {
112 | geneItems();
113 | mAdapter.notifyDataSetChanged();
114 | measureHeight();
115 | onLoad();
116 | }
117 | }, 2500);
118 | }
119 |
120 | private void geneItems() {
121 | for (int i = 0; i != 20; ++i) {
122 | mItems.add("Test XScrollView item " + (++mIndex));
123 | }
124 | }
125 |
126 | private void onLoad() {
127 | mScrollView.stopRefresh();
128 | mScrollView.stopLoadMore();
129 | mScrollView.setRefreshTime(getTime());
130 | }
131 |
132 | private String getTime() {
133 | return new SimpleDateFormat("MM-dd HH:mm", Locale.CHINA).format(new Date());
134 | }
135 |
136 | private int measureHeight() {
137 | // get ListView adapter
138 | ListAdapter adapter = mListView.getAdapter();
139 | if (null == adapter) {
140 | return 0;
141 | }
142 |
143 | int totalHeight = 0;
144 |
145 | for (int i = 0, len = adapter.getCount(); i < len; i++) {
146 | View item = adapter.getView(i, null, mListView);
147 | if (null == item) continue;
148 | // measure each item width and height
149 | item.measure(0, 0);
150 | // calculate all height
151 | totalHeight += item.getMeasuredHeight();
152 | }
153 |
154 | ViewGroup.LayoutParams params = mListView.getLayoutParams();
155 |
156 | if (null == params) {
157 | params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
158 | ViewGroup.LayoutParams.WRAP_CONTENT);
159 | }
160 |
161 | // calculate ListView height
162 | params.height = totalHeight + (mListView.getDividerHeight() * (adapter.getCount() - 1));
163 |
164 | mListView.setLayoutParams(params);
165 |
166 | return params.height;
167 | }
168 | }
169 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/java/com/markmao/pulltorefresh/widget/XFooterView.java:
--------------------------------------------------------------------------------
1 | package com.markmao.pulltorefresh.widget;
2 |
3 | import android.content.Context;
4 | import android.util.AttributeSet;
5 | import android.view.LayoutInflater;
6 | import android.view.View;
7 | import android.view.animation.Animation;
8 | import android.view.animation.RotateAnimation;
9 | import android.widget.LinearLayout;
10 | import android.widget.TextView;
11 | import com.markmao.pulltorefresh.R;
12 |
13 | /**
14 | * The footer view for {@link com.markmao.pulltorefresh.widget.XListView} and
15 | * {@link com.markmao.pulltorefresh.widget.XScrollView}
16 | *
17 | * @author markmjw
18 | * @date 2013-10-08
19 | */
20 | public class XFooterView extends LinearLayout {
21 | public final static int STATE_NORMAL = 0;
22 | public final static int STATE_READY = 1;
23 | public final static int STATE_LOADING = 2;
24 |
25 | private final int ROTATE_ANIM_DURATION = 180;
26 |
27 | private View mLayout;
28 |
29 | private View mProgressBar;
30 |
31 | private TextView mHintView;
32 |
33 | // private ImageView mHintImage;
34 |
35 | private Animation mRotateUpAnim;
36 | private Animation mRotateDownAnim;
37 |
38 | private int mState = STATE_NORMAL;
39 |
40 | public XFooterView(Context context) {
41 | super(context);
42 | initView(context);
43 | }
44 |
45 | public XFooterView(Context context, AttributeSet attrs) {
46 | super(context, attrs);
47 | initView(context);
48 | }
49 |
50 | private void initView(Context context) {
51 | mLayout = LayoutInflater.from(context).inflate(R.layout.vw_footer, null);
52 | mLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
53 | LayoutParams.WRAP_CONTENT));
54 | addView(mLayout);
55 |
56 | mProgressBar = mLayout.findViewById(R.id.footer_progressbar);
57 | mHintView = (TextView) mLayout.findViewById(R.id.footer_hint_text);
58 | // mHintImage = (ImageView) mLayout.findViewById(R.id.footer_arrow);
59 |
60 | mRotateUpAnim = new RotateAnimation(0.0f, 180.0f, Animation.RELATIVE_TO_SELF, 0.5f,
61 | Animation.RELATIVE_TO_SELF, 0.5f);
62 | mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
63 | mRotateUpAnim.setFillAfter(true);
64 |
65 | mRotateDownAnim = new RotateAnimation(180.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f,
66 | Animation.RELATIVE_TO_SELF, 0.5f);
67 | mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
68 | mRotateDownAnim.setFillAfter(true);
69 | }
70 |
71 | /**
72 | * Set footer view state
73 | *
74 | * @see #STATE_LOADING
75 | * @see #STATE_NORMAL
76 | * @see #STATE_READY
77 | *
78 | * @param state
79 | */
80 | public void setState(int state) {
81 | if (state == mState) return;
82 |
83 | if (state == STATE_LOADING) {
84 | // mHintImage.clearAnimation();
85 | // mHintImage.setVisibility(View.INVISIBLE);
86 | mProgressBar.setVisibility(View.VISIBLE);
87 | mHintView.setVisibility(View.INVISIBLE);
88 | } else {
89 | mHintView.setVisibility(View.VISIBLE);
90 | // mHintImage.setVisibility(View.VISIBLE);
91 | mProgressBar.setVisibility(View.INVISIBLE);
92 | }
93 |
94 | switch (state) {
95 | case STATE_NORMAL:
96 | // if (mState == STATE_READY) {
97 | // mHintImage.startAnimation(mRotateDownAnim);
98 | // }
99 | // if (mState == STATE_LOADING) {
100 | // mHintImage.clearAnimation();
101 | // }
102 | mHintView.setText(R.string.footer_hint_load_normal);
103 | break;
104 |
105 | case STATE_READY:
106 | if (mState != STATE_READY) {
107 | // mHintImage.clearAnimation();
108 | // mHintImage.startAnimation(mRotateUpAnim);
109 | mHintView.setText(R.string.footer_hint_load_ready);
110 | }
111 | break;
112 |
113 | case STATE_LOADING:
114 | break;
115 | }
116 |
117 | mState = state;
118 | }
119 |
120 | /**
121 | * Set footer view bottom margin.
122 | *
123 | * @param margin
124 | */
125 | public void setBottomMargin(int margin) {
126 | if (margin < 0) return;
127 | LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mLayout.getLayoutParams();
128 | lp.bottomMargin = margin;
129 | mLayout.setLayoutParams(lp);
130 | }
131 |
132 | /**
133 | * Get footer view bottom margin.
134 | *
135 | * @return
136 | */
137 | public int getBottomMargin() {
138 | LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mLayout.getLayoutParams();
139 | return lp.bottomMargin;
140 | }
141 |
142 | /**
143 | * normal status
144 | */
145 | public void normal() {
146 | mHintView.setVisibility(View.VISIBLE);
147 | mProgressBar.setVisibility(View.GONE);
148 | }
149 |
150 | /**
151 | * loading status
152 | */
153 | public void loading() {
154 | mHintView.setVisibility(View.GONE);
155 | mProgressBar.setVisibility(View.VISIBLE);
156 | }
157 |
158 | /**
159 | * hide footer when disable pull load more
160 | */
161 | public void hide() {
162 | LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mLayout.getLayoutParams();
163 | lp.height = 0;
164 | mLayout.setLayoutParams(lp);
165 | }
166 |
167 | /**
168 | * show footer
169 | */
170 | public void show() {
171 | LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mLayout.getLayoutParams();
172 | lp.height = LayoutParams.WRAP_CONTENT;
173 | mLayout.setLayoutParams(lp);
174 | }
175 |
176 | }
177 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction, and
10 | distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by the copyright
13 | owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all other entities
16 | that control, are controlled by, or are under common control with that entity.
17 | For the purposes of this definition, "control" means (i) the power, direct or
18 | indirect, to cause the direction or management of such entity, whether by
19 | contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
20 | outstanding shares, or (iii) beneficial ownership of such entity.
21 |
22 | "You" (or "Your") shall mean an individual or Legal Entity exercising
23 | permissions granted by this License.
24 |
25 | "Source" form shall mean the preferred form for making modifications, including
26 | but not limited to software source code, documentation source, and configuration
27 | files.
28 |
29 | "Object" form shall mean any form resulting from mechanical transformation or
30 | translation of a Source form, including but not limited to compiled object code,
31 | generated documentation, and conversions to other media types.
32 |
33 | "Work" shall mean the work of authorship, whether in Source or Object form, made
34 | available under the License, as indicated by a copyright notice that is included
35 | in or attached to the work (an example is provided in the Appendix below).
36 |
37 | "Derivative Works" shall mean any work, whether in Source or Object form, that
38 | is based on (or derived from) the Work and for which the editorial revisions,
39 | annotations, elaborations, or other modifications represent, as a whole, an
40 | original work of authorship. For the purposes of this License, Derivative Works
41 | shall not include works that remain separable from, or merely link (or bind by
42 | name) to the interfaces of, the Work and Derivative Works thereof.
43 |
44 | "Contribution" shall mean any work of authorship, including the original version
45 | of the Work and any modifications or additions to that Work or Derivative Works
46 | thereof, that is intentionally submitted to Licensor for inclusion in the Work
47 | by the copyright owner or by an individual or Legal Entity authorized to submit
48 | on behalf of the copyright owner. For the purposes of this definition,
49 | "submitted" means any form of electronic, verbal, or written communication sent
50 | to the Licensor or its representatives, including but not limited to
51 | communication on electronic mailing lists, source code control systems, and
52 | issue tracking systems that are managed by, or on behalf of, the Licensor for
53 | the purpose of discussing and improving the Work, but excluding communication
54 | that is conspicuously marked or otherwise designated in writing by the copyright
55 | owner as "Not a Contribution."
56 |
57 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf
58 | of whom a Contribution has been received by Licensor and subsequently
59 | incorporated within the Work.
60 |
61 | 2. Grant of Copyright License.
62 |
63 | Subject to the terms and conditions of this License, each Contributor hereby
64 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
65 | irrevocable copyright license to reproduce, prepare Derivative Works of,
66 | publicly display, publicly perform, sublicense, and distribute the Work and such
67 | Derivative Works in Source or Object form.
68 |
69 | 3. Grant of Patent License.
70 |
71 | Subject to the terms and conditions of this License, each Contributor hereby
72 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
73 | irrevocable (except as stated in this section) patent license to make, have
74 | made, use, offer to sell, sell, import, and otherwise transfer the Work, where
75 | such license applies only to those patent claims licensable by such Contributor
76 | that are necessarily infringed by their Contribution(s) alone or by combination
77 | of their Contribution(s) with the Work to which such Contribution(s) was
78 | submitted. If You institute patent litigation against any entity (including a
79 | cross-claim or counterclaim in a lawsuit) alleging that the Work or a
80 | Contribution incorporated within the Work constitutes direct or contributory
81 | patent infringement, then any patent licenses granted to You under this License
82 | for that Work shall terminate as of the date such litigation is filed.
83 |
84 | 4. Redistribution.
85 |
86 | You may reproduce and distribute copies of the Work or Derivative Works thereof
87 | in any medium, with or without modifications, and in Source or Object form,
88 | provided that You meet the following conditions:
89 |
90 | You must give any other recipients of the Work or Derivative Works a copy of
91 | this License; and
92 | You must cause any modified files to carry prominent notices stating that You
93 | changed the files; and
94 | You must retain, in the Source form of any Derivative Works that You distribute,
95 | all copyright, patent, trademark, and attribution notices from the Source form
96 | of the Work, excluding those notices that do not pertain to any part of the
97 | Derivative Works; and
98 | If the Work includes a "NOTICE" text file as part of its distribution, then any
99 | Derivative Works that You distribute must include a readable copy of the
100 | attribution notices contained within such NOTICE file, excluding those notices
101 | that do not pertain to any part of the Derivative Works, in at least one of the
102 | following places: within a NOTICE text file distributed as part of the
103 | Derivative Works; within the Source form or documentation, if provided along
104 | with the Derivative Works; or, within a display generated by the Derivative
105 | Works, if and wherever such third-party notices normally appear. The contents of
106 | the NOTICE file are for informational purposes only and do not modify the
107 | License. You may add Your own attribution notices within Derivative Works that
108 | You distribute, alongside or as an addendum to the NOTICE text from the Work,
109 | provided that such additional attribution notices cannot be construed as
110 | modifying the License.
111 | You may add Your own copyright statement to Your modifications and may provide
112 | additional or different license terms and conditions for use, reproduction, or
113 | distribution of Your modifications, or for any such Derivative Works as a whole,
114 | provided Your use, reproduction, and distribution of the Work otherwise complies
115 | with the conditions stated in this License.
116 |
117 | 5. Submission of Contributions.
118 |
119 | Unless You explicitly state otherwise, any Contribution intentionally submitted
120 | for inclusion in the Work by You to the Licensor shall be under the terms and
121 | conditions of this License, without any additional terms or conditions.
122 | Notwithstanding the above, nothing herein shall supersede or modify the terms of
123 | any separate license agreement you may have executed with Licensor regarding
124 | such Contributions.
125 |
126 | 6. Trademarks.
127 |
128 | This License does not grant permission to use the trade names, trademarks,
129 | service marks, or product names of the Licensor, except as required for
130 | reasonable and customary use in describing the origin of the Work and
131 | reproducing the content of the NOTICE file.
132 |
133 | 7. Disclaimer of Warranty.
134 |
135 | Unless required by applicable law or agreed to in writing, Licensor provides the
136 | Work (and each Contributor provides its Contributions) on an "AS IS" BASIS,
137 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
138 | including, without limitation, any warranties or conditions of TITLE,
139 | NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
140 | solely responsible for determining the appropriateness of using or
141 | redistributing the Work and assume any risks associated with Your exercise of
142 | permissions under this License.
143 |
144 | 8. Limitation of Liability.
145 |
146 | In no event and under no legal theory, whether in tort (including negligence),
147 | contract, or otherwise, unless required by applicable law (such as deliberate
148 | and grossly negligent acts) or agreed to in writing, shall any Contributor be
149 | liable to You for damages, including any direct, indirect, special, incidental,
150 | or consequential damages of any character arising as a result of this License or
151 | out of the use or inability to use the Work (including but not limited to
152 | damages for loss of goodwill, work stoppage, computer failure or malfunction, or
153 | any and all other commercial damages or losses), even if such Contributor has
154 | been advised of the possibility of such damages.
155 |
156 | 9. Accepting Warranty or Additional Liability.
157 |
158 | While redistributing the Work or Derivative Works thereof, You may choose to
159 | offer, and charge a fee for, acceptance of support, warranty, indemnity, or
160 | other liability obligations and/or rights consistent with this License. However,
161 | in accepting such obligations, You may act only on Your own behalf and on Your
162 | sole responsibility, not on behalf of any other Contributor, and only if You
163 | agree to indemnify, defend, and hold each Contributor harmless for any liability
164 | incurred by, or claims asserted against, such Contributor by reason of your
165 | accepting any such warranty or additional liability.
166 |
167 | END OF TERMS AND CONDITIONS
168 |
169 | APPENDIX: How to apply the Apache License to your work
170 |
171 | To apply the Apache License to your work, attach the following boilerplate
172 | notice, with the fields enclosed by brackets "[]" replaced with your own
173 | identifying information. (Don't include the brackets!) The text should be
174 | enclosed in the appropriate comment syntax for the file format. We also
175 | recommend that a file or class name and description of purpose be included on
176 | the same "printed page" as the copyright notice for easier identification within
177 | third-party archives.
178 |
179 | Copyright [yyyy] [name of copyright owner]
180 |
181 | Licensed under the Apache License, Version 2.0 (the "License");
182 | you may not use this file except in compliance with the License.
183 | You may obtain a copy of the License at
184 |
185 | http://www.apache.org/licenses/LICENSE-2.0
186 |
187 | Unless required by applicable law or agreed to in writing, software
188 | distributed under the License is distributed on an "AS IS" BASIS,
189 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
190 | See the License for the specific language governing permissions and
191 | limitations under the License.
192 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/java/com/markmao/pulltorefresh/widget/XListView.java:
--------------------------------------------------------------------------------
1 | package com.markmao.pulltorefresh.widget;
2 |
3 | import android.annotation.TargetApi;
4 | import android.content.Context;
5 | import android.os.Build;
6 | import android.util.AttributeSet;
7 | import android.view.Gravity;
8 | import android.view.MotionEvent;
9 | import android.view.View;
10 | import android.view.ViewTreeObserver;
11 | import android.view.ViewTreeObserver.OnGlobalLayoutListener;
12 | import android.view.animation.DecelerateInterpolator;
13 | import android.widget.AbsListView;
14 | import android.widget.AbsListView.OnScrollListener;
15 | import android.widget.LinearLayout;
16 | import android.widget.ListAdapter;
17 | import android.widget.ListView;
18 | import android.widget.RelativeLayout;
19 | import android.widget.Scroller;
20 | import android.widget.TextView;
21 |
22 | import com.markmao.pulltorefresh.R;
23 |
24 | /**
25 | * XListView, it's based on XListView(Maxwin)
26 | *
27 | * @author markmjw
28 | * @date 2013-10-08
29 | */
30 | public class XListView extends ListView implements OnScrollListener {
31 | // private static final String TAG = "XListView";
32 |
33 | private final static int SCROLL_BACK_HEADER = 0;
34 | private final static int SCROLL_BACK_FOOTER = 1;
35 |
36 | private final static int SCROLL_DURATION = 400;
37 |
38 | // when pull up >= 50px
39 | private final static int PULL_LOAD_MORE_DELTA = 50;
40 |
41 | // support iOS like pull
42 | private final static float OFFSET_RADIO = 1.8f;
43 |
44 | private float mLastY = -1;
45 |
46 | // used for scroll back
47 | private Scroller mScroller;
48 | // user's scroll listener
49 | private OnScrollListener mScrollListener;
50 | // for mScroller, scroll back from header or footer.
51 | private int mScrollBack;
52 |
53 | // the interface to trigger refresh and load more.
54 | private IXListViewListener mListener;
55 |
56 | private XHeaderView mHeader;
57 | // header view content, use it to calculate the Header's height. And hide it when disable pull refresh.
58 | private RelativeLayout mHeaderContent;
59 | private TextView mHeaderTime;
60 | private int mHeaderHeight;
61 |
62 | private LinearLayout mFooterLayout;
63 | private XFooterView mFooterView;
64 | private boolean mIsFooterReady = false;
65 |
66 | private boolean mEnablePullRefresh = true;
67 | private boolean mPullRefreshing = false;
68 |
69 | private boolean mEnablePullLoad = true;
70 | private boolean mEnableAutoLoad = false;
71 | private boolean mPullLoading = false;
72 |
73 | // total list items, used to detect is at the bottom of ListView
74 | private int mTotalItemCount;
75 |
76 | public XListView(Context context) {
77 | super(context);
78 | initWithContext(context);
79 | }
80 |
81 | public XListView(Context context, AttributeSet attrs) {
82 | super(context, attrs);
83 | initWithContext(context);
84 | }
85 |
86 | public XListView(Context context, AttributeSet attrs, int defStyle) {
87 | super(context, attrs, defStyle);
88 | initWithContext(context);
89 | }
90 |
91 | private void initWithContext(Context context) {
92 | mScroller = new Scroller(context, new DecelerateInterpolator());
93 | super.setOnScrollListener(this);
94 |
95 | // init header view
96 | mHeader = new XHeaderView(context);
97 | mHeaderContent = (RelativeLayout) mHeader.findViewById(R.id.header_content);
98 | mHeaderTime = (TextView) mHeader.findViewById(R.id.header_hint_time);
99 | addHeaderView(mHeader);
100 |
101 | // init footer view
102 | mFooterView = new XFooterView(context);
103 | mFooterLayout = new LinearLayout(context);
104 | LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout
105 | .LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
106 | params.gravity = Gravity.CENTER;
107 | mFooterLayout.addView(mFooterView, params);
108 |
109 | // init header height
110 | ViewTreeObserver observer = mHeader.getViewTreeObserver();
111 | if (null != observer) {
112 | observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
113 | @SuppressWarnings("deprecation")
114 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
115 | @Override
116 | public void onGlobalLayout() {
117 | mHeaderHeight = mHeaderContent.getHeight();
118 | ViewTreeObserver observer = getViewTreeObserver();
119 |
120 | if (null != observer) {
121 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
122 | observer.removeGlobalOnLayoutListener(this);
123 | } else {
124 | observer.removeOnGlobalLayoutListener(this);
125 | }
126 | }
127 | }
128 | });
129 | }
130 | }
131 |
132 | @Override
133 | public void setAdapter(ListAdapter adapter) {
134 | // make sure XFooterView is the last footer view, and only add once.
135 | if (!mIsFooterReady) {
136 | mIsFooterReady = true;
137 | addFooterView(mFooterLayout);
138 | }
139 |
140 | super.setAdapter(adapter);
141 | }
142 |
143 | /**
144 | * Enable or disable pull down refresh feature.
145 | *
146 | * @param enable
147 | */
148 | public void setPullRefreshEnable(boolean enable) {
149 | mEnablePullRefresh = enable;
150 |
151 | // disable, hide the content
152 | mHeaderContent.setVisibility(enable ? View.VISIBLE : View.INVISIBLE);
153 | }
154 |
155 | /**
156 | * Enable or disable pull up load more feature.
157 | *
158 | * @param enable
159 | */
160 | public void setPullLoadEnable(boolean enable) {
161 | mEnablePullLoad = enable;
162 |
163 | if (!mEnablePullLoad) {
164 | mFooterView.setBottomMargin(0);
165 | mFooterView.hide();
166 | mFooterView.setPadding(0, 0, 0, 0);
167 | mFooterView.setOnClickListener(null);
168 |
169 | } else {
170 | mPullLoading = false;
171 | mFooterView.setPadding(0, 0, 0, 0);
172 | mFooterView.show();
173 | mFooterView.setState(XFooterView.STATE_NORMAL);
174 | // both "pull up" and "click" will invoke load more.
175 | mFooterView.setOnClickListener(new OnClickListener() {
176 | @Override
177 | public void onClick(View v) {
178 | startLoadMore();
179 | }
180 | });
181 | }
182 | }
183 |
184 | /**
185 | * Enable or disable auto load more feature when scroll to bottom.
186 | *
187 | * @param enable
188 | */
189 | public void setAutoLoadEnable(boolean enable) {
190 | mEnableAutoLoad = enable;
191 | }
192 |
193 | /**
194 | * Stop refresh, reset header view.
195 | */
196 | public void stopRefresh() {
197 | if (mPullRefreshing) {
198 | mPullRefreshing = false;
199 | resetHeaderHeight();
200 | }
201 | }
202 |
203 | /**
204 | * Stop load more, reset footer view.
205 | */
206 | public void stopLoadMore() {
207 | if (mPullLoading) {
208 | mPullLoading = false;
209 | mFooterView.setState(XFooterView.STATE_NORMAL);
210 | }
211 | }
212 |
213 | /**
214 | * Set last refresh time
215 | *
216 | * @param time
217 | */
218 | public void setRefreshTime(String time) {
219 | mHeaderTime.setText(time);
220 | }
221 |
222 | /**
223 | * Set listener.
224 | *
225 | * @param listener
226 | */
227 | public void setXListViewListener(IXListViewListener listener) {
228 | mListener = listener;
229 | }
230 |
231 | /**
232 | * Auto call back refresh.
233 | */
234 | public void autoRefresh() {
235 | mHeader.setVisibleHeight(mHeaderHeight);
236 |
237 | if (mEnablePullRefresh && !mPullRefreshing) {
238 | // update the arrow image not refreshing
239 | if (mHeader.getVisibleHeight() > mHeaderHeight) {
240 | mHeader.setState(XHeaderView.STATE_READY);
241 | } else {
242 | mHeader.setState(XHeaderView.STATE_NORMAL);
243 | }
244 | }
245 |
246 | mPullRefreshing = true;
247 | mHeader.setState(XHeaderView.STATE_REFRESHING);
248 | refresh();
249 | }
250 |
251 | private void invokeOnScrolling() {
252 | if (mScrollListener instanceof OnXScrollListener) {
253 | OnXScrollListener listener = (OnXScrollListener) mScrollListener;
254 | listener.onXScrolling(this);
255 | }
256 | }
257 |
258 | private void updateHeaderHeight(float delta) {
259 | mHeader.setVisibleHeight((int) delta + mHeader.getVisibleHeight());
260 |
261 | if (mEnablePullRefresh && !mPullRefreshing) {
262 | // update the arrow image unrefreshing
263 | if (mHeader.getVisibleHeight() > mHeaderHeight) {
264 | mHeader.setState(XHeaderView.STATE_READY);
265 | } else {
266 | mHeader.setState(XHeaderView.STATE_NORMAL);
267 | }
268 | }
269 |
270 | // scroll to top each time
271 | setSelection(0);
272 | }
273 |
274 | private void resetHeaderHeight() {
275 | int height = mHeader.getVisibleHeight();
276 | if (height == 0) return;
277 |
278 | // refreshing and header isn't shown fully. do nothing.
279 | if (mPullRefreshing && height <= mHeaderHeight) return;
280 |
281 | // default: scroll back to dismiss header.
282 | int finalHeight = 0;
283 | // is refreshing, just scroll back to show all the header.
284 | if (mPullRefreshing && height > mHeaderHeight) {
285 | finalHeight = mHeaderHeight;
286 | }
287 |
288 | mScrollBack = SCROLL_BACK_HEADER;
289 | mScroller.startScroll(0, height, 0, finalHeight - height, SCROLL_DURATION);
290 |
291 | // trigger computeScroll
292 | invalidate();
293 | }
294 |
295 | private void updateFooterHeight(float delta) {
296 | int height = mFooterView.getBottomMargin() + (int) delta;
297 |
298 | if (mEnablePullLoad && !mPullLoading) {
299 | if (height > PULL_LOAD_MORE_DELTA) {
300 | // height enough to invoke load more.
301 | mFooterView.setState(XFooterView.STATE_READY);
302 | } else {
303 | mFooterView.setState(XFooterView.STATE_NORMAL);
304 | }
305 | }
306 |
307 | mFooterView.setBottomMargin(height);
308 |
309 | // scroll to bottom
310 | // setSelection(mTotalItemCount - 1);
311 | }
312 |
313 | private void resetFooterHeight() {
314 | int bottomMargin = mFooterView.getBottomMargin();
315 |
316 | if (bottomMargin > 0) {
317 | mScrollBack = SCROLL_BACK_FOOTER;
318 | mScroller.startScroll(0, bottomMargin, 0, -bottomMargin, SCROLL_DURATION);
319 | invalidate();
320 | }
321 | }
322 |
323 | private void startLoadMore() {
324 | mPullLoading = true;
325 | mFooterView.setState(XFooterView.STATE_LOADING);
326 | loadMore();
327 | }
328 |
329 | @Override
330 | public boolean onTouchEvent(MotionEvent ev) {
331 | if (mLastY == -1) {
332 | mLastY = ev.getRawY();
333 | }
334 |
335 | switch (ev.getAction()) {
336 | case MotionEvent.ACTION_DOWN:
337 | mLastY = ev.getRawY();
338 | break;
339 |
340 | case MotionEvent.ACTION_MOVE:
341 | final float deltaY = ev.getRawY() - mLastY;
342 | mLastY = ev.getRawY();
343 |
344 | if (getFirstVisiblePosition() == 0 && (mHeader.getVisibleHeight() > 0 ||
345 | deltaY > 0)) {
346 | // the first item is showing, header has shown or pull down.
347 | updateHeaderHeight(deltaY / OFFSET_RADIO);
348 | invokeOnScrolling();
349 |
350 | } else if (getLastVisiblePosition() == mTotalItemCount - 1 && (mFooterView
351 | .getBottomMargin() > 0 || deltaY < 0)) {
352 | // last item, already pulled up or want to pull up.
353 | updateFooterHeight(-deltaY / OFFSET_RADIO);
354 | }
355 | break;
356 |
357 | default:
358 | // reset
359 | mLastY = -1;
360 | if (getFirstVisiblePosition() == 0) {
361 | // invoke refresh
362 | if (mEnablePullRefresh && mHeader.getVisibleHeight() > mHeaderHeight) {
363 | mPullRefreshing = true;
364 | mHeader.setState(XHeaderView.STATE_REFRESHING);
365 | refresh();
366 | }
367 |
368 | resetHeaderHeight();
369 |
370 | } else if (getLastVisiblePosition() == mTotalItemCount - 1) {
371 | // invoke load more.
372 | if (mEnablePullLoad && mFooterView.getBottomMargin() > PULL_LOAD_MORE_DELTA) {
373 | startLoadMore();
374 | }
375 | resetFooterHeight();
376 | }
377 | break;
378 | }
379 | return super.onTouchEvent(ev);
380 | }
381 |
382 | @Override
383 | public void computeScroll() {
384 | if (mScroller.computeScrollOffset()) {
385 | if (mScrollBack == SCROLL_BACK_HEADER) {
386 | mHeader.setVisibleHeight(mScroller.getCurrY());
387 | } else {
388 | mFooterView.setBottomMargin(mScroller.getCurrY());
389 | }
390 |
391 | postInvalidate();
392 | invokeOnScrolling();
393 | }
394 |
395 | super.computeScroll();
396 | }
397 |
398 | @Override
399 | public void setOnScrollListener(OnScrollListener l) {
400 | mScrollListener = l;
401 | }
402 |
403 | @Override
404 | public void onScrollStateChanged(AbsListView view, int scrollState) {
405 | if (mScrollListener != null) {
406 | mScrollListener.onScrollStateChanged(view, scrollState);
407 | }
408 |
409 | if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
410 | if (mEnableAutoLoad && getLastVisiblePosition() == getCount() - 1) {
411 | startLoadMore();
412 | }
413 | }
414 | }
415 |
416 | @Override
417 | public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
418 | int totalItemCount) {
419 | // send to user's listener
420 | mTotalItemCount = totalItemCount;
421 | if (mScrollListener != null) {
422 | mScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
423 | }
424 | }
425 |
426 | private void refresh() {
427 | if (mEnablePullRefresh && null != mListener) {
428 | mListener.onRefresh();
429 | }
430 | }
431 |
432 | private void loadMore() {
433 | if (mEnablePullLoad && null != mListener) {
434 | mListener.onLoadMore();
435 | }
436 | }
437 |
438 | /**
439 | * You can listen ListView.OnScrollListener or this one. it will invoke
440 | * onXScrolling when header/footer scroll back.
441 | */
442 | public interface OnXScrollListener extends OnScrollListener {
443 | public void onXScrolling(View view);
444 | }
445 |
446 | /**
447 | * Implements this interface to get refresh/load more event.
448 | *
449 | * @author markmjw
450 | */
451 | public interface IXListViewListener {
452 | public void onRefresh();
453 |
454 | public void onLoadMore();
455 | }
456 | }
457 |
--------------------------------------------------------------------------------
/PullToRefresh/src/main/java/com/markmao/pulltorefresh/widget/XScrollView.java:
--------------------------------------------------------------------------------
1 | package com.markmao.pulltorefresh.widget;
2 |
3 | import android.annotation.TargetApi;
4 | import android.content.Context;
5 | import android.os.Build;
6 | import android.util.AttributeSet;
7 | import android.util.Log;
8 | import android.view.Gravity;
9 | import android.view.MotionEvent;
10 | import android.view.View;
11 | import android.view.ViewGroup;
12 | import android.view.ViewTreeObserver;
13 | import android.view.ViewTreeObserver.OnGlobalLayoutListener;
14 | import android.view.animation.DecelerateInterpolator;
15 | import android.widget.AbsListView;
16 | import android.widget.AbsListView.OnScrollListener;
17 | import android.widget.LinearLayout;
18 | import android.widget.RelativeLayout;
19 | import android.widget.ScrollView;
20 | import android.widget.Scroller;
21 | import android.widget.TextView;
22 |
23 | import com.markmao.pulltorefresh.R;
24 |
25 | /**
26 | * XScrollView, modified from {@link com.markmao.pulltorefresh.widget.XListView}
27 | *
28 | * @author markmjw
29 | * @date 2013-10-08
30 | * @see com.markmao.pulltorefresh.widget.XListView
31 | */
32 | public class XScrollView extends ScrollView implements OnScrollListener {
33 | // private static final String TAG = "XScrollView";
34 |
35 | private final static int SCROLL_BACK_HEADER = 0;
36 | private final static int SCROLL_BACK_FOOTER = 1;
37 |
38 | private final static int SCROLL_DURATION = 400;
39 |
40 | // when pull up >= 50px
41 | private final static int PULL_LOAD_MORE_DELTA = 50;
42 |
43 | // support iOS like pull
44 | private final static float OFFSET_RADIO = 1.8f;
45 |
46 | private float mLastY = -1;
47 |
48 | // used for scroll back
49 | private Scroller mScroller;
50 | // user's scroll listener
51 | private OnScrollListener mScrollListener;
52 | // for mScroller, scroll back from header or footer.
53 | private int mScrollBack;
54 |
55 | // the interface to trigger refresh and load more.
56 | private IXScrollViewListener mListener;
57 |
58 | private LinearLayout mLayout;
59 | private LinearLayout mContentLayout;
60 |
61 | private XHeaderView mHeader;
62 | // header view content, use it to calculate the Header's height. And hide it when disable pull refresh.
63 | private RelativeLayout mHeaderContent;
64 | private TextView mHeaderTime;
65 | private int mHeaderHeight;
66 |
67 | private XFooterView mFooterView;
68 |
69 | private boolean mEnablePullRefresh = true;
70 | private boolean mPullRefreshing = false;
71 |
72 | private boolean mEnablePullLoad = true;
73 | private boolean mEnableAutoLoad = false;
74 | private boolean mPullLoading = false;
75 |
76 | public XScrollView(Context context) {
77 | super(context);
78 | initWithContext(context);
79 | }
80 |
81 | public XScrollView(Context context, AttributeSet attrs) {
82 | super(context, attrs);
83 | initWithContext(context);
84 | }
85 |
86 | public XScrollView(Context context, AttributeSet attrs, int defStyle) {
87 | super(context, attrs, defStyle);
88 | initWithContext(context);
89 | }
90 |
91 | private void initWithContext(Context context) {
92 | mLayout = (LinearLayout) View.inflate(context, R.layout.vw_xscrollview_layout, null);
93 | mContentLayout = (LinearLayout) mLayout.findViewById(R.id.content_layout);
94 |
95 | mScroller = new Scroller(context, new DecelerateInterpolator());
96 | // XScrollView need the scroll event, and it will dispatch the event to user's listener (as a proxy).
97 | this.setOnScrollListener(this);
98 |
99 | // init header view
100 | mHeader = new XHeaderView(context);
101 | mHeaderContent = (RelativeLayout) mHeader.findViewById(R.id.header_content);
102 | mHeaderTime = (TextView) mHeader.findViewById(R.id.header_hint_time);
103 | LinearLayout headerLayout = (LinearLayout) mLayout.findViewById(R.id.header_layout);
104 | headerLayout.addView(mHeader);
105 |
106 | // init footer view
107 | mFooterView = new XFooterView(context);
108 | LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
109 | LinearLayout.LayoutParams.MATCH_PARENT,
110 | LinearLayout.LayoutParams.MATCH_PARENT);
111 | params.gravity = Gravity.CENTER;
112 | LinearLayout footLayout = (LinearLayout) mLayout.findViewById(R.id.footer_layout);
113 | footLayout.addView(mFooterView, params);
114 |
115 | // init header height
116 | ViewTreeObserver observer = mHeader.getViewTreeObserver();
117 | if (null != observer) {
118 | observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
119 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
120 | @SuppressWarnings("deprecation")
121 | @Override
122 | public void onGlobalLayout() {
123 | mHeaderHeight = mHeaderContent.getHeight();
124 | ViewTreeObserver observer = getViewTreeObserver();
125 | if (null != observer) {
126 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
127 | observer.removeGlobalOnLayoutListener(this);
128 | } else {
129 | observer.removeOnGlobalLayoutListener(this);
130 | }
131 | }
132 | }
133 | });
134 | }
135 |
136 | this.addView(mLayout);
137 | }
138 |
139 | /**
140 | * Set the content ViewGroup for XScrollView.
141 | *
142 | * @param content
143 | */
144 | public void setContentView(ViewGroup content) {
145 | if (mLayout == null) {
146 | return;
147 | }
148 |
149 | if (mContentLayout == null) {
150 | mContentLayout = (LinearLayout) mLayout.findViewById(R.id.content_layout);
151 | }
152 |
153 | if (mContentLayout.getChildCount() > 0) {
154 | mContentLayout.removeAllViews();
155 | }
156 | mContentLayout.addView(content);
157 | }
158 |
159 | /**
160 | * Set the content View for XScrollView.
161 | *
162 | * @param content
163 | */
164 | public void setView(View content) {
165 | if (mLayout == null) {
166 | return;
167 | }
168 |
169 | if (mContentLayout == null) {
170 | mContentLayout = (LinearLayout) mLayout.findViewById(R.id.content_layout);
171 | }
172 | mContentLayout.addView(content);
173 | }
174 |
175 | /**
176 | * Enable or disable pull down refresh feature.
177 | *
178 | * @param enable
179 | */
180 | public void setPullRefreshEnable(boolean enable) {
181 | mEnablePullRefresh = enable;
182 |
183 | // disable, hide the content
184 | mHeaderContent.setVisibility(enable ? View.VISIBLE : View.INVISIBLE);
185 | }
186 |
187 | /**
188 | * Enable or disable pull up load more feature.
189 | *
190 | * @param enable
191 | */
192 | public void setPullLoadEnable(boolean enable) {
193 | mEnablePullLoad = enable;
194 |
195 | if (!mEnablePullLoad) {
196 | mFooterView.setBottomMargin(0);
197 | mFooterView.hide();
198 | mFooterView.setPadding(0, 0, 0, mFooterView.getHeight() * (-1));
199 | mFooterView.setOnClickListener(null);
200 |
201 | } else {
202 | mPullLoading = false;
203 | mFooterView.setPadding(0, 0, 0, 0);
204 | mFooterView.show();
205 | mFooterView.setState(XFooterView.STATE_NORMAL);
206 | // both "pull up" and "click" will invoke load more.
207 | mFooterView.setOnClickListener(new OnClickListener() {
208 | @Override
209 | public void onClick(View v) {
210 | startLoadMore();
211 | }
212 | });
213 | }
214 | }
215 |
216 | /**
217 | * Enable or disable auto load more feature when scroll to bottom.
218 | *
219 | * @param enable
220 | */
221 | public void setAutoLoadEnable(boolean enable) {
222 | mEnableAutoLoad = enable;
223 | }
224 |
225 | /**
226 | * Stop refresh, reset header view.
227 | */
228 | public void stopRefresh() {
229 | if (mPullRefreshing) {
230 | mPullRefreshing = false;
231 | resetHeaderHeight();
232 | }
233 | }
234 |
235 | /**
236 | * Stop load more, reset footer view.
237 | */
238 | public void stopLoadMore() {
239 | if (mPullLoading) {
240 | mPullLoading = false;
241 | mFooterView.setState(XFooterView.STATE_NORMAL);
242 | }
243 | }
244 |
245 | /**
246 | * Set last refresh time
247 | *
248 | * @param time
249 | */
250 | public void setRefreshTime(String time) {
251 | mHeaderTime.setText(time);
252 | }
253 |
254 | /**
255 | * Set listener.
256 | *
257 | * @param listener
258 | */
259 | public void setIXScrollViewListener(IXScrollViewListener listener) {
260 | mListener = listener;
261 | }
262 |
263 | /**
264 | * Auto call back refresh.
265 | */
266 | public void autoRefresh() {
267 | mHeader.setVisibleHeight(mHeaderHeight);
268 |
269 | if (mEnablePullRefresh && !mPullRefreshing) {
270 | // update the arrow image not refreshing
271 | if (mHeader.getVisibleHeight() > mHeaderHeight) {
272 | mHeader.setState(XHeaderView.STATE_READY);
273 | } else {
274 | mHeader.setState(XHeaderView.STATE_NORMAL);
275 | }
276 | }
277 |
278 | mPullRefreshing = true;
279 | mHeader.setState(XHeaderView.STATE_REFRESHING);
280 | refresh();
281 | }
282 |
283 | private void invokeOnScrolling() {
284 | if (mScrollListener instanceof OnXScrollListener) {
285 | OnXScrollListener l = (OnXScrollListener) mScrollListener;
286 | l.onXScrolling(this);
287 | }
288 | }
289 |
290 | private void updateHeaderHeight(float delta) {
291 | mHeader.setVisibleHeight((int) delta + mHeader.getVisibleHeight());
292 |
293 | if (mEnablePullRefresh && !mPullRefreshing) {
294 | // update the arrow image unrefreshing
295 | if (mHeader.getVisibleHeight() > mHeaderHeight) {
296 | mHeader.setState(XHeaderView.STATE_READY);
297 | } else {
298 | mHeader.setState(XHeaderView.STATE_NORMAL);
299 | }
300 | }
301 |
302 | // scroll to top each time
303 | post(new Runnable() {
304 | @Override
305 | public void run() {
306 | XScrollView.this.fullScroll(ScrollView.FOCUS_UP);
307 | }
308 | });
309 | }
310 |
311 | private void resetHeaderHeight() {
312 | int height = mHeader.getVisibleHeight();
313 | if (height == 0) return;
314 |
315 | // refreshing and header isn't shown fully. do nothing.
316 | if (mPullRefreshing && height <= mHeaderHeight) return;
317 |
318 | // default: scroll back to dismiss header.
319 | int finalHeight = 0;
320 | // is refreshing, just scroll back to show all the header.
321 | if (mPullRefreshing && height > mHeaderHeight) {
322 | finalHeight = mHeaderHeight;
323 | }
324 |
325 | mScrollBack = SCROLL_BACK_HEADER;
326 | mScroller.startScroll(0, height, 0, finalHeight - height, SCROLL_DURATION);
327 |
328 | // trigger computeScroll
329 | invalidate();
330 | }
331 |
332 | private void updateFooterHeight(float delta) {
333 | int height = mFooterView.getBottomMargin() + (int) delta;
334 |
335 | if (mEnablePullLoad && !mPullLoading) {
336 | if (height > PULL_LOAD_MORE_DELTA) {
337 | // height enough to invoke load more.
338 | mFooterView.setState(XFooterView.STATE_READY);
339 | } else {
340 | mFooterView.setState(XFooterView.STATE_NORMAL);
341 | }
342 | }
343 |
344 | mFooterView.setBottomMargin(height);
345 |
346 | // scroll to bottom
347 | post(new Runnable() {
348 | @Override
349 | public void run() {
350 | XScrollView.this.fullScroll(ScrollView.FOCUS_DOWN);
351 | }
352 | });
353 | }
354 |
355 | private void resetFooterHeight() {
356 | int bottomMargin = mFooterView.getBottomMargin();
357 |
358 | if (bottomMargin > 0) {
359 | mScrollBack = SCROLL_BACK_FOOTER;
360 | mScroller.startScroll(0, bottomMargin, 0, -bottomMargin, SCROLL_DURATION);
361 | invalidate();
362 | }
363 | }
364 |
365 | private void startLoadMore() {
366 | if (!mPullLoading) {
367 | mPullLoading = true;
368 | mFooterView.setState(XFooterView.STATE_LOADING);
369 | loadMore();
370 | }
371 | }
372 |
373 | @Override
374 | public boolean onTouchEvent(MotionEvent ev) {
375 | if (mLastY == -1) {
376 | mLastY = ev.getRawY();
377 | }
378 |
379 | switch (ev.getAction()) {
380 | case MotionEvent.ACTION_DOWN:
381 | mLastY = ev.getRawY();
382 | break;
383 |
384 | case MotionEvent.ACTION_MOVE:
385 | final float deltaY = ev.getRawY() - mLastY;
386 | mLastY = ev.getRawY();
387 |
388 | if (isTop() && (mHeader.getVisibleHeight() > 0 || deltaY > 0)) {
389 | // the first item is showing, header has shown or pull down.
390 | updateHeaderHeight(deltaY / OFFSET_RADIO);
391 | invokeOnScrolling();
392 |
393 | } else if (isBottom() && (mFooterView.getBottomMargin() > 0 || deltaY < 0)) {
394 | // last item, already pulled up or want to pull up.
395 | updateFooterHeight(-deltaY / OFFSET_RADIO);
396 |
397 | }
398 | break;
399 |
400 | default:
401 | // reset
402 | mLastY = -1;
403 |
404 | resetHeaderOrBottom();
405 | break;
406 | }
407 |
408 | return super.onTouchEvent(ev);
409 | }
410 |
411 | private void resetHeaderOrBottom() {
412 | if (isTop()) {
413 | // invoke refresh
414 | if (mEnablePullRefresh && mHeader.getVisibleHeight() > mHeaderHeight) {
415 | mPullRefreshing = true;
416 | mHeader.setState(XHeaderView.STATE_REFRESHING);
417 | refresh();
418 | }
419 | resetHeaderHeight();
420 |
421 | } else if (isBottom()) {
422 | // invoke load more.
423 | if (mEnablePullLoad && mFooterView.getBottomMargin() > PULL_LOAD_MORE_DELTA) {
424 | startLoadMore();
425 | }
426 | resetFooterHeight();
427 | }
428 | }
429 |
430 | private boolean isTop() {
431 | return getScrollY() <= 0 || mHeader.getVisibleHeight() > mHeaderHeight || mContentLayout.getTop() > 0;
432 | }
433 |
434 | private boolean isBottom() {
435 | return Math.abs(getScrollY() + getHeight() - computeVerticalScrollRange()) <= 5 ||
436 | (getScrollY() > 0 && null != mFooterView && mFooterView.getBottomMargin() > 0);
437 | }
438 |
439 | @Override
440 | public void computeScroll() {
441 | if (mScroller.computeScrollOffset()) {
442 | if (mScrollBack == SCROLL_BACK_HEADER) {
443 | mHeader.setVisibleHeight(mScroller.getCurrY());
444 | } else {
445 | mFooterView.setBottomMargin(mScroller.getCurrY());
446 | }
447 |
448 | postInvalidate();
449 | invokeOnScrolling();
450 | }
451 | super.computeScroll();
452 | }
453 |
454 | public void setOnScrollListener(OnScrollListener l) {
455 | mScrollListener = l;
456 | }
457 |
458 | @Override
459 | public void onScrollStateChanged(AbsListView view, int scrollState) {
460 | if (mScrollListener != null) {
461 | mScrollListener.onScrollStateChanged(view, scrollState);
462 | }
463 | }
464 |
465 | @Override
466 | protected void onScrollChanged(int l, int t, int oldl, int oldt) {
467 | // Grab the last child placed in the ScrollView, we need it to determinate the bottom position.
468 | View view = getChildAt(getChildCount() - 1);
469 |
470 | if (null != view) {
471 | // Calculate the scroll diff
472 | int diff = (view.getBottom() - (view.getHeight() + view.getScrollY()));
473 |
474 | // if diff is zero, then the bottom has been reached
475 | if (diff == 0 && mEnableAutoLoad) {
476 | // notify that we have reached the bottom
477 | startLoadMore();
478 | }
479 | }
480 |
481 | super.onScrollChanged(l, t, oldl, oldt);
482 | }
483 |
484 | @Override
485 | public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
486 | int totalItemCount) {
487 | // send to user's listener
488 | if (mScrollListener != null) {
489 | mScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
490 | }
491 | }
492 |
493 | private void refresh() {
494 | if (mEnablePullRefresh && null != mListener) {
495 | mListener.onRefresh();
496 | }
497 | }
498 |
499 | private void loadMore() {
500 | if (mEnablePullLoad && null != mListener) {
501 | mListener.onLoadMore();
502 | }
503 | }
504 |
505 | /**
506 | * You can listen ListView.OnScrollListener or this one. it will invoke
507 | * onXScrolling when header/footer scroll back.
508 | */
509 | public interface OnXScrollListener extends OnScrollListener {
510 | public void onXScrolling(View view);
511 | }
512 |
513 | /**
514 | * Implements this interface to get refresh/load more event.
515 | */
516 | public interface IXScrollViewListener {
517 | public void onRefresh();
518 |
519 | public void onLoadMore();
520 | }
521 | }
522 |
--------------------------------------------------------------------------------