();
16 | }
17 |
18 | /**
19 | * This allows you to add extra LoadingLayout instances to this proxy. This
20 | * is only necessary if you keep your own instances, and want to have them
21 | * included in any
22 | */
23 | public void addLayout(LoadingLayout layout) {
24 | if (null != layout) {
25 | mLoadingLayouts.add(layout);
26 | }
27 | }
28 |
29 | @Override
30 | public void setLastUpdatedLabel(CharSequence label) {
31 | for (LoadingLayout layout : mLoadingLayouts) {
32 | layout.setLastUpdatedLabel(label);
33 | }
34 | }
35 |
36 | @Override
37 | public void setLoadingDrawable(Drawable drawable) {
38 | for (LoadingLayout layout : mLoadingLayouts) {
39 | layout.setLoadingDrawable(drawable);
40 | }
41 | }
42 |
43 | @Override
44 | public void setRefreshingLabel(CharSequence refreshingLabel) {
45 | for (LoadingLayout layout : mLoadingLayouts) {
46 | layout.setRefreshingLabel(refreshingLabel);
47 | }
48 | }
49 |
50 | @Override
51 | public void setPullLabel(CharSequence label) {
52 | for (LoadingLayout layout : mLoadingLayouts) {
53 | layout.setPullLabel(label);
54 | }
55 | }
56 |
57 | @Override
58 | public void setReleaseLabel(CharSequence label) {
59 | for (LoadingLayout layout : mLoadingLayouts) {
60 | layout.setReleaseLabel(label);
61 | }
62 | }
63 |
64 | public void setTextTypeface(Typeface tf) {
65 | for (LoadingLayout layout : mLoadingLayouts) {
66 | layout.setTextTypeface(tf);
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/SwipeMenuItem.java:
--------------------------------------------------------------------------------
1 | package com.yinglan.swiperefresh;
2 |
3 |
4 | import android.content.Context;
5 | import android.graphics.drawable.Drawable;
6 |
7 |
8 | public class SwipeMenuItem {
9 |
10 | private int id;
11 | private Context mContext;
12 | private String title;
13 | private Drawable icon;
14 | private Drawable background;
15 | private int titleColor;
16 | private int titleSize;
17 | private int width;
18 |
19 | public SwipeMenuItem(Context context) {
20 | mContext = context;
21 | }
22 |
23 | public int getId() {
24 | return id;
25 | }
26 |
27 | public void setId(int id) {
28 | this.id = id;
29 | }
30 |
31 | public int getTitleColor() {
32 | return titleColor;
33 | }
34 |
35 | public int getTitleSize() {
36 | return titleSize;
37 | }
38 |
39 | public void setTitleSize(int titleSize) {
40 | this.titleSize = titleSize;
41 | }
42 |
43 | public void setTitleColor(int titleColor) {
44 | this.titleColor = titleColor;
45 | }
46 |
47 | public String getTitle() {
48 | return title;
49 | }
50 |
51 | public void setTitle(String title) {
52 | this.title = title;
53 | }
54 |
55 | public void setTitle(int resId) {
56 | setTitle(mContext.getString(resId));
57 | }
58 |
59 | public Drawable getIcon() {
60 | return icon;
61 | }
62 |
63 | public void setIcon(Drawable icon) {
64 | this.icon = icon;
65 | }
66 |
67 | public void setIcon(int resId) {
68 | this.icon = mContext.getResources().getDrawable(resId);
69 | }
70 |
71 | public Drawable getBackground() {
72 | return background;
73 | }
74 |
75 | public void setBackground(Drawable background) {
76 | this.background = background;
77 | }
78 |
79 | public void setBackground(int resId) {
80 | this.background = mContext.getResources().getDrawable(resId);
81 | }
82 |
83 | public int getWidth() {
84 | return width;
85 | }
86 |
87 | public void setWidth(int width) {
88 | this.width = width;
89 | }
90 |
91 | }
92 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/internal/ViewCompat.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011, 2012 Chris Banes.
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 | package com.yinglan.swiperefresh.internal;
17 |
18 | import android.annotation.TargetApi;
19 | import android.graphics.drawable.Drawable;
20 | import android.os.Build.VERSION;
21 | import android.os.Build.VERSION_CODES;
22 | import android.view.View;
23 |
24 | @SuppressWarnings("deprecation")
25 | public class ViewCompat {
26 |
27 | public static void postOnAnimation(View view, Runnable runnable) {
28 | if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
29 | SDK16.postOnAnimation(view, runnable);
30 | } else {
31 | view.postDelayed(runnable, 16);
32 | }
33 | }
34 |
35 | public static void setBackground(View view, Drawable background) {
36 | if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
37 | SDK16.setBackground(view, background);
38 | } else {
39 | view.setBackgroundDrawable(background);
40 | }
41 | }
42 |
43 | public static void setLayerType(View view, int layerType) {
44 | if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
45 | SDK11.setLayerType(view, layerType);
46 | }
47 | }
48 |
49 | @TargetApi(11)
50 | static class SDK11 {
51 |
52 | public static void setLayerType(View view, int layerType) {
53 | view.setLayerType(layerType, null);
54 | }
55 | }
56 |
57 | @TargetApi(16)
58 | static class SDK16 {
59 |
60 | public static void postOnAnimation(View view, Runnable runnable) {
61 | view.postOnAnimation(runnable);
62 | }
63 |
64 | public static void setBackground(View view, Drawable background) {
65 | view.setBackground(background);
66 | }
67 |
68 | }
69 |
70 | }
71 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/SwipeMenuHighListView.java:
--------------------------------------------------------------------------------
1 | package com.yinglan.swiperefresh;
2 |
3 | import android.content.Context;
4 | import android.util.AttributeSet;
5 | import android.view.MotionEvent;
6 | import android.view.View;
7 | import android.view.ViewParent;
8 |
9 | public class SwipeMenuHighListView extends SwipeMenuListView {
10 |
11 | private int DRAG_X_THROD = 0;
12 |
13 | public SwipeMenuHighListView(Context context) {
14 | super(context);
15 | }
16 |
17 | public SwipeMenuHighListView(Context context, AttributeSet attrs, int defStyle) {
18 | super(context, attrs, defStyle);
19 | }
20 |
21 | public SwipeMenuHighListView(Context context, AttributeSet attrs) {
22 | super(context, attrs);
23 | }
24 |
25 | @Override
26 | public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
27 | int expandSpec = View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
28 | super.onMeasure(widthMeasureSpec, expandSpec);
29 | }
30 |
31 | int downX, downY;
32 |
33 | @Override
34 | public boolean onTouchEvent(MotionEvent ev) {
35 | try {
36 | switch (ev.getAction()) {
37 | case MotionEvent.ACTION_DOWN:
38 | downX = (int) ev.getX();
39 | downY = (int) ev.getY();
40 | break;
41 |
42 | case MotionEvent.ACTION_MOVE:
43 |
44 | int dx = (int) Math.abs(ev.getX() - downX);
45 | int dy = (int) Math.abs(ev.getY() - downY);
46 | if (dx > dy && dx > DRAG_X_THROD) {
47 | setParentInterceptTouchEvent(true);
48 | } else if (dy > dx) {
49 |
50 | }
51 | break;
52 | case MotionEvent.ACTION_UP:
53 | case MotionEvent.ACTION_CANCEL:
54 | setParentInterceptTouchEvent(false);
55 |
56 | break;
57 | }
58 | } catch (Exception e) {
59 | e.printStackTrace();
60 | }
61 | return super.onTouchEvent(ev);
62 | }
63 |
64 | /* 这个函数很重要,请求禁止父容器拦截触摸事件 */
65 | public void setParentInterceptTouchEvent(boolean disallow) {
66 | ViewParent parent = getParent();
67 | if (parent != null) {
68 | parent.requestDisallowInterceptTouchEvent(disallow);
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/library/src/main/res/layout/pull_to_refresh_header_vertical.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
12 |
13 |
17 |
18 |
23 |
24 |
32 |
33 |
34 |
40 |
41 |
48 |
49 |
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/PullToRefreshGridView.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011, 2012 Chris Banes.
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 | package com.yinglan.swiperefresh;
17 |
18 | import android.content.Context;
19 | import android.util.AttributeSet;
20 | import android.view.View;
21 | import android.widget.GridView;
22 |
23 | import com.yinglan.swiperefresh.internal.EmptyViewMethodAccessor;
24 |
25 | public class PullToRefreshGridView extends PullToRefreshAdapterViewBase {
26 |
27 | public PullToRefreshGridView(Context context) {
28 | super(context);
29 | }
30 |
31 | public PullToRefreshGridView(Context context, AttributeSet attrs) {
32 | super(context, attrs);
33 | }
34 |
35 | public PullToRefreshGridView(Context context, Mode mode) {
36 | super(context, mode);
37 | }
38 |
39 | public PullToRefreshGridView(Context context, Mode mode, AnimationStyle style) {
40 | super(context, mode, style);
41 | }
42 |
43 | @Override
44 | public final Orientation getPullToRefreshScrollDirection() {
45 | return Orientation.VERTICAL;
46 | }
47 |
48 | @Override
49 | protected final GridView createRefreshableView(Context context, AttributeSet attrs) {
50 | final GridView gv;
51 | gv = new InternalGridView(context, attrs);
52 | // Use Generated ID (from res/values/ids.xml)
53 | gv.setId(R.id.gridview);
54 | return gv;
55 | }
56 |
57 | class InternalGridView extends GridView implements EmptyViewMethodAccessor {
58 |
59 | public InternalGridView(Context context, AttributeSet attrs) {
60 | super(context, attrs);
61 | }
62 |
63 | @Override
64 | public void setEmptyView(View emptyView) {
65 | PullToRefreshGridView.this.setEmptyView(emptyView);
66 | }
67 |
68 | @Override
69 | public void setEmptyViewInternal(View emptyView) {
70 | super.setEmptyView(emptyView);
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/PullToRefreshExpandableListView.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011, 2012 Chris Banes.
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 | package com.yinglan.swiperefresh;
17 |
18 | import android.content.Context;
19 | import android.util.AttributeSet;
20 | import android.view.View;
21 | import android.widget.ExpandableListView;
22 |
23 | import com.yinglan.swiperefresh.internal.EmptyViewMethodAccessor;
24 |
25 | public class PullToRefreshExpandableListView extends PullToRefreshAdapterViewBase {
26 |
27 | public PullToRefreshExpandableListView(Context context) {
28 | super(context);
29 | }
30 |
31 | public PullToRefreshExpandableListView(Context context, AttributeSet attrs) {
32 | super(context, attrs);
33 | }
34 |
35 | public PullToRefreshExpandableListView(Context context, Mode mode) {
36 | super(context, mode);
37 | }
38 |
39 | public PullToRefreshExpandableListView(Context context, Mode mode, AnimationStyle style) {
40 | super(context, mode, style);
41 | }
42 |
43 | @Override
44 | public final Orientation getPullToRefreshScrollDirection() {
45 | return Orientation.VERTICAL;
46 | }
47 |
48 | @Override
49 | protected ExpandableListView createRefreshableView(Context context, AttributeSet attrs) {
50 | final ExpandableListView lv;
51 | lv = new InternalExpandableListView(context, attrs);
52 |
53 | // Set it to this so it can be used in ListActivity/ListFragment
54 | lv.setId(android.R.id.list);
55 | return lv;
56 | }
57 |
58 | class InternalExpandableListView extends ExpandableListView implements EmptyViewMethodAccessor {
59 |
60 | public InternalExpandableListView(Context context, AttributeSet attrs) {
61 | super(context, attrs);
62 | }
63 |
64 | @Override
65 | public void setEmptyView(View emptyView) {
66 | PullToRefreshExpandableListView.this.setEmptyView(emptyView);
67 | }
68 |
69 | @Override
70 | public void setEmptyViewInternal(View emptyView) {
71 | super.setEmptyView(emptyView);
72 | }
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yinglan/demo/adapter/ListLessAdapter.java:
--------------------------------------------------------------------------------
1 | package com.yinglan.demo.adapter;
2 |
3 | import android.content.Context;
4 | import android.content.pm.ApplicationInfo;
5 | import android.view.LayoutInflater;
6 | import android.view.View;
7 | import android.view.ViewGroup;
8 | import android.widget.BaseAdapter;
9 | import android.widget.ImageView;
10 | import android.widget.TextView;
11 | import android.widget.Toast;
12 |
13 | import com.yinglan.demo.R;
14 | import com.yinglan.swiperefresh.SwipeMenu;
15 | import com.yinglan.swiperefresh.SwipeMenuListView;
16 |
17 | import java.util.List;
18 |
19 | /**
20 | * Created by yinglan
21 | */
22 |
23 | public class ListLessAdapter extends BaseAdapter implements SwipeMenuListView.OnMenuItemClickListener {
24 |
25 | private List mAppList;
26 | private Context mContext;
27 |
28 | public ListLessAdapter(Context context) {
29 | mContext = context;
30 | mAppList = context.getPackageManager().getInstalledApplications(0);
31 | mAppList = mAppList.subList(0, 5);
32 | }
33 |
34 | @Override
35 | public int getCount() {
36 | return mAppList.size();
37 | }
38 |
39 | @Override
40 | public ApplicationInfo getItem(int position) {
41 | return mAppList.get(position);
42 | }
43 |
44 | @Override
45 | public long getItemId(int position) {
46 | return position;
47 | }
48 |
49 | @Override
50 | public View getView(int position, View convertView, ViewGroup parent) {
51 | ViewHolder holder = null;
52 | if (convertView == null) {
53 | convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_app, null);
54 | holder = new ViewHolder(convertView);
55 | convertView.setTag(holder);
56 | } else {
57 | holder = (ViewHolder) convertView.getTag();
58 | }
59 | ApplicationInfo item = getItem(position);
60 | holder.iv_icon.setImageDrawable(item.loadIcon(mContext.getPackageManager()));
61 | holder.tv_name.setText(item.loadLabel(mContext.getPackageManager()));
62 | return convertView;
63 | }
64 |
65 | @Override
66 | public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
67 | switch (index) {
68 | case 0:
69 | Toast.makeText(mContext, "click~", Toast.LENGTH_SHORT).show();
70 | break;
71 | case 1:
72 | mAppList.remove(position);
73 | notifyDataSetChanged();
74 | Toast.makeText(mContext, "delete~", Toast.LENGTH_SHORT).show();
75 | break;
76 | }
77 | return false;
78 | }
79 |
80 | class ViewHolder {
81 | ImageView iv_icon;
82 | TextView tv_name;
83 |
84 | public ViewHolder(View view) {
85 | iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
86 | tv_name = (TextView) view.findViewById(R.id.tv_name);
87 | }
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/SwipeMenuView.java:
--------------------------------------------------------------------------------
1 | package com.yinglan.swiperefresh;
2 |
3 | import android.text.TextUtils;
4 | import android.view.Gravity;
5 | import android.view.View;
6 | import android.view.View.OnClickListener;
7 | import android.widget.ImageView;
8 | import android.widget.LinearLayout;
9 | import android.widget.TextView;
10 |
11 | import java.util.List;
12 |
13 | public class SwipeMenuView extends LinearLayout implements OnClickListener {
14 |
15 | private SwipeMenuListView mListView;
16 | private SwipeMenuLayout mLayout;
17 | private SwipeMenu mMenu;
18 | private OnSwipeItemClickListener onItemClickListener;
19 | private int position;
20 |
21 | public int getPosition() {
22 | return position;
23 | }
24 |
25 | public void setPosition(int position) {
26 | this.position = position;
27 | }
28 |
29 | public SwipeMenuView(SwipeMenu menu, SwipeMenuListView listView) {
30 | super(menu.getContext());
31 | mListView = listView;
32 | mMenu = menu;
33 | List items = menu.getMenuItems();
34 | int id = 0;
35 | for (SwipeMenuItem item : items) {
36 | addItem(item, id++);
37 | }
38 | }
39 |
40 | private void addItem(SwipeMenuItem item, int id) {
41 | LayoutParams params = new LayoutParams(item.getWidth(),
42 | LayoutParams.MATCH_PARENT);
43 | LinearLayout parent = new LinearLayout(getContext());
44 | parent.setId(id);
45 | parent.setGravity(Gravity.CENTER);
46 | parent.setOrientation(LinearLayout.VERTICAL);
47 | parent.setLayoutParams(params);
48 | parent.setBackgroundDrawable(item.getBackground());
49 | parent.setOnClickListener(this);
50 | addView(parent);
51 |
52 | if (item.getIcon() != null) {
53 | parent.addView(createIcon(item));
54 | }
55 | if (!TextUtils.isEmpty(item.getTitle())) {
56 | parent.addView(createTitle(item));
57 | }
58 |
59 | }
60 |
61 | private ImageView createIcon(SwipeMenuItem item) {
62 | ImageView iv = new ImageView(getContext());
63 | iv.setImageDrawable(item.getIcon());
64 | return iv;
65 | }
66 |
67 | private TextView createTitle(SwipeMenuItem item) {
68 | TextView tv = new TextView(getContext());
69 | tv.setText(item.getTitle());
70 | tv.setGravity(Gravity.CENTER);
71 | tv.setTextSize(item.getTitleSize());
72 | tv.setTextColor(item.getTitleColor());
73 | return tv;
74 | }
75 |
76 | @Override
77 | public void onClick(View v) {
78 | if (onItemClickListener != null && mLayout.isOpen()) {
79 | onItemClickListener.onItemClick(this, mMenu, v.getId());
80 | }
81 | }
82 |
83 | public OnSwipeItemClickListener getOnSwipeItemClickListener() {
84 | return onItemClickListener;
85 | }
86 |
87 | public void setOnSwipeItemClickListener(OnSwipeItemClickListener onItemClickListener) {
88 | this.onItemClickListener = onItemClickListener;
89 | }
90 |
91 | public void setLayout(SwipeMenuLayout mLayout) {
92 | this.mLayout = mLayout;
93 | }
94 |
95 | public static interface OnSwipeItemClickListener {
96 | void onItemClick(SwipeMenuView view, SwipeMenu menu, int index);
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # SwipeMenuAndRefresh_Library
2 | ## Abstract 摘要
3 | 集下拉刷新与侧滑删除于一身的库文件,类似于QQ聊天界面。解决了侧滑与下拉的事件冲突,向下兼容至8,并增加了swipmenu侧滑在特殊场景下scrollview及listview中的嵌套支持。(停止维护)
4 |
5 | ## Gif 动画
6 | 
7 |
8 | ## Image 图片
9 | 
10 | 
11 | 
12 | 
13 | 
14 | 
15 | 
16 |
17 | ## Demo 下载体验
18 | [Download Demo](https://github.com/yingLanNull/PullToRefreshSwipeMenu_Library/blob/master/show/app-debug.apk)
19 |
20 | ## Usage 使用方法
21 | ### Step 1
22 | #### Gradle 配置
23 | ```
24 | dependencies {
25 | compile 'com.yinglan.swiperefresh:library:1.0.0'
26 | }
27 | ```
28 |
29 | ### Step 2
30 |
31 | #### PullToRefreshSwipeListView 可侧滑及刷新
32 | ```
33 | //模式
44 | ```
45 |
46 | #### SwipeMenuHighListView 特殊场景可在ScrollView中嵌套侧滑
47 |
48 | ```
49 |
52 |
53 |
56 |
57 |
58 |
59 |
60 | ```
61 | ### More Usage 更多用法
62 |
63 | [SwipeMenu](https://github.com/baoyongzhang/SwipeMenuListView/blob/master/README.md)
64 |
65 | [PullToRefresh](https://github.com/chrisbanes/Android-PullToRefresh/blob/master/README.md)
66 |
67 | ## Thanks
68 |
69 | 本库在以下基础上进行了整合调整,增强稳定性,并添加了部分内容。
70 |
71 | [Android-PullToRefresh](https://github.com/chrisbanes/Android-PullToRefresh)
72 |
73 | [SwipeMenuListView](https://github.com/baoyongzhang/SwipeMenuListView)
74 |
75 | ## LICENSE 开源许可
76 |
77 | Apache License Version 2.0
78 |
79 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/extras/SoundPullEventListener.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011, 2012 Chris Banes.
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 | package com.yinglan.swiperefresh.extras;
17 |
18 | import android.content.Context;
19 | import android.media.MediaPlayer;
20 | import android.view.View;
21 |
22 | import com.yinglan.swiperefresh.PullToRefreshBase;
23 | import com.yinglan.swiperefresh.PullToRefreshBase.Mode;
24 | import com.yinglan.swiperefresh.PullToRefreshBase.State;
25 |
26 | import java.util.HashMap;
27 |
28 | public class SoundPullEventListener implements PullToRefreshBase.OnPullEventListener {
29 |
30 | private final Context mContext;
31 | private final HashMap mSoundMap;
32 |
33 | private MediaPlayer mCurrentMediaPlayer;
34 |
35 | /**
36 | * Constructor
37 | *
38 | */
39 | public SoundPullEventListener(Context context) {
40 | mContext = context;
41 | mSoundMap = new HashMap();
42 | }
43 |
44 | @Override
45 | public final void onPullEvent(PullToRefreshBase refreshView, State event, Mode direction) {
46 | Integer soundResIdObj = mSoundMap.get(event);
47 | if (null != soundResIdObj) {
48 | playSound(soundResIdObj.intValue());
49 | }
50 | }
51 |
52 | /**
53 | * Set the Sounds to be played when a Pull Event happens. You specify which
54 | * sound plays for which events by calling this method multiple times for
55 | * each event.
56 | * If you've already set a sound for a certain event, and add another sound
57 | * for that event, only the new sound will be played.
58 | *
59 | */
60 | public void addSoundEvent(State event, int resId) {
61 | mSoundMap.put(event, resId);
62 | }
63 |
64 | /**
65 | * Clears all of the previously set sounds and events.
66 | */
67 | public void clearSounds() {
68 | mSoundMap.clear();
69 | }
70 |
71 | /**
72 | * Gets the current (or last) MediaPlayer instance.
73 | */
74 | public MediaPlayer getCurrentMediaPlayer() {
75 | return mCurrentMediaPlayer;
76 | }
77 |
78 | private void playSound(int resId) {
79 | // Stop current player, if there's one playing
80 | if (null != mCurrentMediaPlayer) {
81 | mCurrentMediaPlayer.stop();
82 | mCurrentMediaPlayer.release();
83 | }
84 |
85 | mCurrentMediaPlayer = MediaPlayer.create(mContext, resId);
86 | if (null != mCurrentMediaPlayer) {
87 | mCurrentMediaPlayer.start();
88 | }
89 | }
90 |
91 | }
92 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/extras/PullToRefreshWebView2.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011, 2012 Chris Banes.
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 | package com.yinglan.swiperefresh.extras;
17 |
18 | import android.content.Context;
19 | import android.util.AttributeSet;
20 | import android.webkit.WebView;
21 |
22 | import com.yinglan.swiperefresh.PullToRefreshWebView;
23 |
24 | import java.util.concurrent.atomic.AtomicBoolean;
25 |
26 | public class PullToRefreshWebView2 extends PullToRefreshWebView {
27 |
28 | static final String JS_INTERFACE_PKG = "ptr";
29 | static final String DEF_JS_READY_PULL_DOWN_CALL = "javascript:isReadyForPullDown();";
30 | static final String DEF_JS_READY_PULL_UP_CALL = "javascript:isReadyForPullUp();";
31 |
32 | public PullToRefreshWebView2(Context context) {
33 | super(context);
34 | }
35 |
36 | public PullToRefreshWebView2(Context context, AttributeSet attrs) {
37 | super(context, attrs);
38 | }
39 |
40 | public PullToRefreshWebView2(Context context, Mode mode) {
41 | super(context, mode);
42 | }
43 |
44 | private JsValueCallback mJsCallback;
45 | private final AtomicBoolean mIsReadyForPullDown = new AtomicBoolean(false);
46 | private final AtomicBoolean mIsReadyForPullUp = new AtomicBoolean(false);
47 |
48 | @Override
49 | protected WebView createRefreshableView(Context context, AttributeSet attrs) {
50 | WebView webView = super.createRefreshableView(context, attrs);
51 |
52 | // Need to add JS Interface so we can get the response back
53 | mJsCallback = new JsValueCallback();
54 | webView.addJavascriptInterface(mJsCallback, JS_INTERFACE_PKG);
55 |
56 | return webView;
57 | }
58 |
59 | @Override
60 | protected boolean isReadyForPullStart() {
61 | // Call Javascript...
62 | getRefreshableView().loadUrl(DEF_JS_READY_PULL_DOWN_CALL);
63 |
64 | // Response will be given to JsValueCallback, which will update
65 | // mIsReadyForPullDown
66 |
67 | return mIsReadyForPullDown.get();
68 | }
69 |
70 | @Override
71 | protected boolean isReadyForPullEnd() {
72 | // Call Javascript...
73 | getRefreshableView().loadUrl(DEF_JS_READY_PULL_UP_CALL);
74 |
75 | // Response will be given to JsValueCallback, which will update
76 | // mIsReadyForPullUp
77 |
78 | return mIsReadyForPullUp.get();
79 | }
80 |
81 | /**
82 | * Used for response from Javascript
83 | *
84 | */
85 | final class JsValueCallback {
86 |
87 | public void isReadyForPullUpResponse(boolean response) {
88 | mIsReadyForPullUp.set(response);
89 | }
90 |
91 | public void isReadyForPullDownResponse(boolean response) {
92 | mIsReadyForPullDown.set(response);
93 | }
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yinglan/demo/adapter/ListAdapter.java:
--------------------------------------------------------------------------------
1 | package com.yinglan.demo.adapter;
2 |
3 | import android.content.Context;
4 | import android.content.pm.ApplicationInfo;
5 | import android.view.LayoutInflater;
6 | import android.view.View;
7 | import android.view.ViewGroup;
8 | import android.widget.BaseAdapter;
9 | import android.widget.ImageView;
10 | import android.widget.TextView;
11 | import android.widget.Toast;
12 |
13 | import com.yinglan.demo.R;
14 | import com.yinglan.swiperefresh.BaseSwipListAdapter;
15 | import com.yinglan.swiperefresh.SwipeMenu;
16 | import com.yinglan.swiperefresh.SwipeMenuListView;
17 |
18 | import java.util.List;
19 |
20 | /**
21 | * Created by yinglan
22 | */
23 |
24 | public class ListAdapter extends BaseAdapter implements SwipeMenuListView.OnMenuItemClickListener {
25 |
26 | private List mAppList;
27 | private Context mContext;
28 |
29 | public ListAdapter(Context context) {
30 | mContext = context;
31 | mAppList = context.getPackageManager().getInstalledApplications(0);
32 | }
33 |
34 | @Override
35 | public int getCount() {
36 | return mAppList.size() / 3;
37 | }
38 |
39 | @Override
40 | public ApplicationInfo getItem(int position) {
41 | return mAppList.get(position);
42 | }
43 |
44 | @Override
45 | public long getItemId(int position) {
46 | return position;
47 | }
48 |
49 | @Override
50 | public int getViewTypeCount() {
51 | return 3;
52 | }
53 |
54 | @Override
55 | public int getItemViewType(int position) {
56 | return position % 3;
57 | }
58 |
59 | @Override
60 | public View getView(int position, View convertView, ViewGroup parent) {
61 | ViewHolder holder = null;
62 | if (convertView == null) {
63 | convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_app, null);
64 | holder = new ViewHolder(convertView);
65 | convertView.setTag(holder);
66 | } else {
67 | holder = (ViewHolder) convertView.getTag();
68 | }
69 | ApplicationInfo item = getItem(position);
70 | holder.iv_icon.setImageDrawable(item.loadIcon(mContext.getPackageManager()));
71 | holder.tv_name.setText(item.loadLabel(mContext.getPackageManager()));
72 | return convertView;
73 | }
74 |
75 | @Override
76 | public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
77 | switch (index) {
78 | case 0:
79 | Toast.makeText(mContext,"click~",Toast.LENGTH_SHORT).show();
80 | break;
81 | case 1:
82 | mAppList.remove(position);
83 | notifyDataSetChanged();
84 | Toast.makeText(mContext,"delete~",Toast.LENGTH_SHORT).show();
85 | break;
86 | case 3:
87 | Toast.makeText(mContext,"click~",Toast.LENGTH_SHORT).show();
88 | break;
89 | }
90 | return false;
91 | }
92 |
93 | class ViewHolder {
94 | ImageView iv_icon;
95 | TextView tv_name;
96 |
97 | public ViewHolder(View view) {
98 | iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
99 | tv_name = (TextView) view.findViewById(R.id.tv_name);
100 | }
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/library/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
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 |
66 |
67 |
68 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/PullToRefreshScrollView.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011, 2012 Chris Banes.
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 | package com.yinglan.swiperefresh;
17 |
18 | import android.annotation.TargetApi;
19 | import android.content.Context;
20 | import android.os.Build.VERSION;
21 | import android.os.Build.VERSION_CODES;
22 | import android.util.AttributeSet;
23 | import android.view.View;
24 | import android.widget.ScrollView;
25 |
26 | public class PullToRefreshScrollView extends PullToRefreshBase {
27 |
28 | public PullToRefreshScrollView(Context context) {
29 | super(context);
30 | }
31 |
32 | public PullToRefreshScrollView(Context context, AttributeSet attrs) {
33 | super(context, attrs);
34 | }
35 |
36 | public PullToRefreshScrollView(Context context, Mode mode) {
37 | super(context, mode);
38 | }
39 |
40 | public PullToRefreshScrollView(Context context, Mode mode, AnimationStyle style) {
41 | super(context, mode, style);
42 | }
43 |
44 | @Override
45 | public final Orientation getPullToRefreshScrollDirection() {
46 | return Orientation.VERTICAL;
47 | }
48 |
49 | @Override
50 | protected ScrollView createRefreshableView(Context context, AttributeSet attrs) {
51 | ScrollView scrollView;
52 | if (VERSION.SDK_INT >= VERSION_CODES.GINGERBREAD) {
53 | scrollView = new InternalScrollViewSDK9(context, attrs);
54 | } else {
55 | scrollView = new ScrollView(context, attrs);
56 | }
57 |
58 | scrollView.setId(R.id.scrollview);
59 | return scrollView;
60 | }
61 |
62 | @Override
63 | protected boolean isReadyForPullStart() {
64 | return mRefreshableView.getScrollY() == 0;
65 | }
66 |
67 | @Override
68 | protected boolean isReadyForPullEnd() {
69 | View scrollViewChild = mRefreshableView.getChildAt(0);
70 | if (null != scrollViewChild) {
71 | return mRefreshableView.getScrollY() >= (scrollViewChild.getHeight() - getHeight());
72 | }
73 | return false;
74 | }
75 |
76 | @TargetApi(9)
77 | final class InternalScrollViewSDK9 extends ScrollView {
78 |
79 | public InternalScrollViewSDK9(Context context, AttributeSet attrs) {
80 | super(context, attrs);
81 | }
82 |
83 | @Override
84 | protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX,
85 | int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
86 |
87 | final boolean returnValue = super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX,
88 | scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
89 |
90 | // Does all of the hard work...
91 | OverscrollHelper.overScrollBy(PullToRefreshScrollView.this, deltaX, scrollX, deltaY, scrollY,
92 | getScrollRange(), isTouchEvent);
93 |
94 | return returnValue;
95 | }
96 |
97 | /**
98 | * Taken from the AOSP ScrollView source
99 | */
100 | private int getScrollRange() {
101 | int scrollRange = 0;
102 | if (getChildCount() > 0) {
103 | View child = getChildAt(0);
104 | scrollRange = Math.max(0, child.getHeight() - (getHeight() - getPaddingBottom() - getPaddingTop()));
105 | }
106 | return scrollRange;
107 | }
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/internal/RotateLoadingLayout.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011, 2012 Chris Banes.
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 | package com.yinglan.swiperefresh.internal;
17 |
18 | import android.content.Context;
19 | import android.content.res.TypedArray;
20 | import android.graphics.Matrix;
21 | import android.graphics.drawable.Drawable;
22 | import android.view.animation.Animation;
23 | import android.view.animation.RotateAnimation;
24 | import android.widget.ImageView.ScaleType;
25 |
26 | import com.yinglan.swiperefresh.PullToRefreshBase.Mode;
27 | import com.yinglan.swiperefresh.PullToRefreshBase.Orientation;
28 | import com.yinglan.swiperefresh.R;
29 |
30 | public class RotateLoadingLayout extends LoadingLayout {
31 |
32 | static final int ROTATION_ANIMATION_DURATION = 1200;
33 |
34 | private final Animation mRotateAnimation;
35 | private final Matrix mHeaderImageMatrix;
36 |
37 | private float mRotationPivotX, mRotationPivotY;
38 |
39 | private final boolean mRotateDrawableWhilePulling;
40 |
41 | public RotateLoadingLayout(Context context, Mode mode, Orientation scrollDirection, TypedArray attrs) {
42 | super(context, mode, scrollDirection, attrs);
43 |
44 | mRotateDrawableWhilePulling = attrs.getBoolean(R.styleable.PullToRefresh_ptrRotateDrawableWhilePulling, true);
45 |
46 | mHeaderImage.setScaleType(ScaleType.MATRIX);
47 | mHeaderImageMatrix = new Matrix();
48 | mHeaderImage.setImageMatrix(mHeaderImageMatrix);
49 |
50 | mRotateAnimation = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
51 | 0.5f);
52 | mRotateAnimation.setInterpolator(ANIMATION_INTERPOLATOR);
53 | mRotateAnimation.setDuration(ROTATION_ANIMATION_DURATION);
54 | mRotateAnimation.setRepeatCount(Animation.INFINITE);
55 | mRotateAnimation.setRepeatMode(Animation.RESTART);
56 | }
57 |
58 | public void onLoadingDrawableSet(Drawable imageDrawable) {
59 | if (null != imageDrawable) {
60 | mRotationPivotX = Math.round(imageDrawable.getIntrinsicWidth() / 2f);
61 | mRotationPivotY = Math.round(imageDrawable.getIntrinsicHeight() / 2f);
62 | }
63 | }
64 |
65 | protected void onPullImpl(float scaleOfLayout) {
66 | float angle;
67 | if (mRotateDrawableWhilePulling) {
68 | angle = scaleOfLayout * 90f;
69 | } else {
70 | angle = Math.max(0f, Math.min(180f, scaleOfLayout * 360f - 180f));
71 | }
72 |
73 | mHeaderImageMatrix.setRotate(angle, mRotationPivotX, mRotationPivotY);
74 | mHeaderImage.setImageMatrix(mHeaderImageMatrix);
75 | }
76 |
77 | @Override
78 | protected void refreshingImpl() {
79 | mHeaderImage.startAnimation(mRotateAnimation);
80 | }
81 |
82 | @Override
83 | protected void resetImpl() {
84 | mHeaderImage.clearAnimation();
85 | resetImageRotation();
86 | }
87 |
88 | private void resetImageRotation() {
89 | if (null != mHeaderImageMatrix) {
90 | mHeaderImageMatrix.reset();
91 | mHeaderImage.setImageMatrix(mHeaderImageMatrix);
92 | }
93 | }
94 |
95 | @Override
96 | protected void pullToRefreshImpl() {
97 | // NO-OP
98 | }
99 |
100 | @Override
101 | protected void releaseToRefreshImpl() {
102 | // NO-OP
103 | }
104 |
105 | @Override
106 | protected int getDefaultDrawableResId() {
107 | return R.drawable.default_ptr_rotate;
108 | }
109 |
110 | }
111 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/PullToRefreshHorizontalScrollView.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011, 2012 Chris Banes.
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 | package com.yinglan.swiperefresh;
17 |
18 | import android.annotation.TargetApi;
19 | import android.content.Context;
20 | import android.os.Build.VERSION;
21 | import android.os.Build.VERSION_CODES;
22 | import android.util.AttributeSet;
23 | import android.view.View;
24 | import android.widget.HorizontalScrollView;
25 |
26 | public class PullToRefreshHorizontalScrollView extends PullToRefreshBase {
27 |
28 | public PullToRefreshHorizontalScrollView(Context context) {
29 | super(context);
30 | }
31 |
32 | public PullToRefreshHorizontalScrollView(Context context, AttributeSet attrs) {
33 | super(context, attrs);
34 | }
35 |
36 | public PullToRefreshHorizontalScrollView(Context context, Mode mode) {
37 | super(context, mode);
38 | }
39 |
40 | public PullToRefreshHorizontalScrollView(Context context, Mode mode, AnimationStyle style) {
41 | super(context, mode, style);
42 | }
43 |
44 | @Override
45 | public final Orientation getPullToRefreshScrollDirection() {
46 | return Orientation.HORIZONTAL;
47 | }
48 |
49 | @Override
50 | protected HorizontalScrollView createRefreshableView(Context context, AttributeSet attrs) {
51 | HorizontalScrollView scrollView;
52 |
53 | if (VERSION.SDK_INT >= VERSION_CODES.GINGERBREAD) {
54 | scrollView = new InternalHorizontalScrollViewSDK9(context, attrs);
55 | } else {
56 | scrollView = new HorizontalScrollView(context, attrs);
57 | }
58 |
59 | scrollView.setId(R.id.scrollview);
60 | return scrollView;
61 | }
62 |
63 | @Override
64 | protected boolean isReadyForPullStart() {
65 | return mRefreshableView.getScrollX() == 0;
66 | }
67 |
68 | @Override
69 | protected boolean isReadyForPullEnd() {
70 | View scrollViewChild = mRefreshableView.getChildAt(0);
71 | if (null != scrollViewChild) {
72 | return mRefreshableView.getScrollX() >= (scrollViewChild.getWidth() - getWidth());
73 | }
74 | return false;
75 | }
76 |
77 | @TargetApi(9)
78 | final class InternalHorizontalScrollViewSDK9 extends HorizontalScrollView {
79 |
80 | public InternalHorizontalScrollViewSDK9(Context context, AttributeSet attrs) {
81 | super(context, attrs);
82 | }
83 |
84 | @Override
85 | protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX,
86 | int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
87 |
88 | final boolean returnValue = super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX,
89 | scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
90 |
91 | // Does all of the hard work...
92 | OverscrollHelper.overScrollBy(PullToRefreshHorizontalScrollView.this, deltaX, scrollX, deltaY, scrollY,
93 | getScrollRange(), isTouchEvent);
94 |
95 | return returnValue;
96 | }
97 |
98 | /**
99 | * Taken from the AOSP ScrollView source
100 | */
101 | private int getScrollRange() {
102 | int scrollRange = 0;
103 | if (getChildCount() > 0) {
104 | View child = getChildAt(0);
105 | scrollRange = Math.max(0, child.getWidth() - (getWidth() - getPaddingLeft() - getPaddingRight()));
106 | }
107 | return scrollRange;
108 | }
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/SwipeMenuAdapter.java:
--------------------------------------------------------------------------------
1 | package com.yinglan.swiperefresh;
2 |
3 |
4 | import android.content.Context;
5 | import android.database.DataSetObserver;
6 | import android.graphics.Color;
7 | import android.graphics.drawable.ColorDrawable;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.ListAdapter;
11 | import android.widget.WrapperListAdapter;
12 |
13 | public class SwipeMenuAdapter implements WrapperListAdapter,
14 | SwipeMenuView.OnSwipeItemClickListener {
15 |
16 | private ListAdapter mAdapter;
17 | private Context mContext;
18 | private SwipeMenuListView.OnMenuItemClickListener onMenuItemClickListener;
19 |
20 | public SwipeMenuAdapter(Context context, ListAdapter adapter) {
21 | mAdapter = adapter;
22 | mContext = context;
23 | }
24 |
25 | @Override
26 | public int getCount() {
27 | return mAdapter.getCount();
28 | }
29 |
30 | @Override
31 | public Object getItem(int position) {
32 | return mAdapter.getItem(position);
33 | }
34 |
35 | @Override
36 | public long getItemId(int position) {
37 | return mAdapter.getItemId(position);
38 | }
39 |
40 | @Override
41 | public View getView(int position, View convertView, ViewGroup parent) {
42 | SwipeMenuLayout layout = null;
43 | if (convertView == null) {
44 | View contentView = mAdapter.getView(position, convertView, parent);
45 | SwipeMenu menu = new SwipeMenu(mContext);
46 | menu.setViewType(getItemViewType(position));
47 | createMenu(menu);
48 | SwipeMenuView menuView = new SwipeMenuView(menu,
49 | (SwipeMenuListView) parent);
50 | menuView.setOnSwipeItemClickListener(this);
51 | SwipeMenuListView listView = (SwipeMenuListView) parent;
52 | layout = new SwipeMenuLayout(contentView, menuView,
53 | listView.getCloseInterpolator(),
54 | listView.getOpenInterpolator());
55 | layout.setPosition(position);
56 | } else {
57 | layout = (SwipeMenuLayout) convertView;
58 | layout.closeMenu();
59 | layout.setPosition(position);
60 | View view = mAdapter.getView(position, layout.getContentView(),
61 | parent);
62 | }
63 | if (mAdapter instanceof BaseSwipListAdapter) {
64 | boolean swipEnable = (((BaseSwipListAdapter) mAdapter).getSwipEnableByPosition(position));
65 | layout.setSwipEnable(swipEnable);
66 | }
67 | return layout;
68 | }
69 |
70 | public void createMenu(SwipeMenu menu) {
71 | // Test Code
72 | SwipeMenuItem item = new SwipeMenuItem(mContext);
73 | item.setTitle("Item 1");
74 | item.setBackground(new ColorDrawable(Color.GRAY));
75 | item.setWidth(300);
76 | menu.addMenuItem(item);
77 |
78 | item = new SwipeMenuItem(mContext);
79 | item.setTitle("Item 2");
80 | item.setBackground(new ColorDrawable(Color.RED));
81 | item.setWidth(300);
82 | menu.addMenuItem(item);
83 | }
84 |
85 | @Override
86 | public void onItemClick(SwipeMenuView view, SwipeMenu menu, int index) {
87 | if (onMenuItemClickListener != null) {
88 | onMenuItemClickListener.onMenuItemClick(view.getPosition(), menu,
89 | index);
90 | }
91 | }
92 |
93 | public void setOnSwipeItemClickListener(
94 | SwipeMenuListView.OnMenuItemClickListener onMenuItemClickListener) {
95 | this.onMenuItemClickListener = onMenuItemClickListener;
96 | }
97 |
98 | @Override
99 | public void registerDataSetObserver(DataSetObserver observer) {
100 | mAdapter.registerDataSetObserver(observer);
101 | }
102 |
103 | @Override
104 | public void unregisterDataSetObserver(DataSetObserver observer) {
105 | mAdapter.unregisterDataSetObserver(observer);
106 | }
107 |
108 | @Override
109 | public boolean areAllItemsEnabled() {
110 | return mAdapter.areAllItemsEnabled();
111 | }
112 |
113 | @Override
114 | public boolean isEnabled(int position) {
115 | return mAdapter.isEnabled(position);
116 | }
117 |
118 | @Override
119 | public boolean hasStableIds() {
120 | return mAdapter.hasStableIds();
121 | }
122 |
123 | @Override
124 | public int getItemViewType(int position) {
125 | return mAdapter.getItemViewType(position);
126 | }
127 |
128 | @Override
129 | public int getViewTypeCount() {
130 | return mAdapter.getViewTypeCount();
131 | }
132 |
133 | @Override
134 | public boolean isEmpty() {
135 | return mAdapter.isEmpty();
136 | }
137 |
138 | @Override
139 | public ListAdapter getWrappedAdapter() {
140 | return mAdapter;
141 | }
142 |
143 | }
144 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yinglan/demo/UnlimitHighActivity.java:
--------------------------------------------------------------------------------
1 | package com.yinglan.demo;
2 |
3 | import android.graphics.Color;
4 | import android.graphics.drawable.ColorDrawable;
5 | import android.os.Bundle;
6 | import android.os.Handler;
7 | import android.support.annotation.Nullable;
8 | import android.support.v7.app.AppCompatActivity;
9 | import android.util.TypedValue;
10 | import android.view.View;
11 | import android.widget.AbsListView;
12 | import android.widget.ListView;
13 |
14 | import com.yinglan.demo.adapter.ListLessAdapter;
15 | import com.yinglan.demo.adapter.ListTextAdapter;
16 | import com.yinglan.swiperefresh.PullToRefreshBase;
17 | import com.yinglan.swiperefresh.PullToRefreshListView;
18 | import com.yinglan.swiperefresh.SwipeMenu;
19 | import com.yinglan.swiperefresh.SwipeMenuCreator;
20 | import com.yinglan.swiperefresh.SwipeMenuHighListView;
21 | import com.yinglan.swiperefresh.SwipeMenuItem;
22 |
23 | /**
24 | * Created by yinglan
25 | */
26 |
27 | public class UnlimitHighActivity extends AppCompatActivity {
28 | private com.yinglan.swiperefresh.PullToRefreshListView pulltorefreshpull;
29 |
30 | @Override
31 | protected void onCreate(@Nullable Bundle savedInstanceState) {
32 | super.onCreate(savedInstanceState);
33 | setContentView(R.layout.activity_pull);
34 | initView();
35 | }
36 |
37 | private void initView() {
38 |
39 | this.pulltorefreshpull = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_pull);
40 |
41 | pulltorefreshpull.setAdapter(new ListTextAdapter(this));
42 | pulltorefreshpull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener() {
43 | @Override
44 | public void onRefresh(PullToRefreshBase refreshView) {
45 | new Handler().postDelayed(new Runnable() {
46 | @Override
47 | public void run() {
48 | pulltorefreshpull.onRefreshComplete();
49 | }
50 | }, 2000);
51 | }
52 | });
53 |
54 | View headerView = View.inflate(this, R.layout.header, null);
55 |
56 | ListLessAdapter lessAdapter = new ListLessAdapter(this);
57 |
58 | final SwipeMenuHighListView swipeMenuHighListView = (SwipeMenuHighListView) headerView.findViewById(R.id.highsiwpe);
59 | swipeMenuHighListView.setAdapter(lessAdapter);
60 | swipeMenuHighListView.setMenuCreator(creator);
61 | swipeMenuHighListView.setOnMenuItemClickListener(lessAdapter);
62 |
63 | pulltorefreshpull.getRefreshableView().addHeaderView(headerView);
64 |
65 | pulltorefreshpull.setOnScrollListener(new AbsListView.OnScrollListener() {
66 | @Override
67 | public void onScrollStateChanged(AbsListView absListView, int i) {
68 | swipeMenuHighListView.smoothCloseMenu();
69 | }
70 |
71 | @Override
72 | public void onScroll(AbsListView absListView, int i, int i1, int i2) {
73 |
74 | }
75 | });
76 | }
77 |
78 |
79 | private SwipeMenuCreator creator = new SwipeMenuCreator() {
80 |
81 | @Override
82 | public void create(SwipeMenu menu) {
83 | // Create different menus depending on the view type
84 | switch (menu.getViewType()) {
85 | case 0:
86 | createMenu1(menu);
87 | break;
88 | }
89 | }
90 |
91 | private void createMenu1(SwipeMenu menu) {
92 | SwipeMenuItem openItem = new SwipeMenuItem(
93 | getApplicationContext());
94 | // set item background
95 | openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
96 | 0xCE)));
97 | // set item width
98 | openItem.setWidth(dp2px(90));
99 | // set item title
100 | openItem.setTitle("Open");
101 | // set item title fontsize
102 | openItem.setTitleSize(18);
103 | // set item title font color
104 | openItem.setTitleColor(Color.WHITE);
105 | // add to menu
106 | menu.addMenuItem(openItem);
107 |
108 | // create "delete" item
109 | SwipeMenuItem deleteItem = new SwipeMenuItem(
110 | getApplicationContext());
111 | // set item background
112 | deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
113 | 0x3F, 0x25)));
114 | // set item width
115 | deleteItem.setWidth(dp2px(90));
116 | // set a icon
117 | deleteItem.setIcon(R.drawable.ic_delete);
118 | // add to menu
119 | menu.addMenuItem(deleteItem);
120 | }
121 |
122 | private int dp2px(int dp) {
123 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
124 | getResources().getDisplayMetrics());
125 | }
126 | };
127 | }
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/PullToRefreshWebView.java:
--------------------------------------------------------------------------------
1 | package com.yinglan.swiperefresh;
2 |
3 | import android.annotation.TargetApi;
4 | import android.content.Context;
5 | import android.os.Build.VERSION;
6 | import android.os.Build.VERSION_CODES;
7 | import android.os.Bundle;
8 | import android.util.AttributeSet;
9 | import android.webkit.WebChromeClient;
10 | import android.webkit.WebView;
11 |
12 | public class PullToRefreshWebView extends PullToRefreshBase {
13 |
14 | private static final OnRefreshListener defaultOnRefreshListener = new OnRefreshListener() {
15 |
16 | @Override
17 | public void onRefresh(PullToRefreshBase refreshView) {
18 | refreshView.getRefreshableView().reload();
19 | }
20 |
21 | };
22 |
23 | private final WebChromeClient defaultWebChromeClient = new WebChromeClient() {
24 |
25 | @Override
26 | public void onProgressChanged(WebView view, int newProgress) {
27 | if (newProgress == 100) {
28 | onRefreshComplete();
29 | }
30 | }
31 |
32 | };
33 |
34 | public PullToRefreshWebView(Context context) {
35 | super(context);
36 |
37 | /**
38 | * Added so that by default, Pull-to-Refresh refreshes the page
39 | */
40 | setOnRefreshListener(defaultOnRefreshListener);
41 | mRefreshableView.setWebChromeClient(defaultWebChromeClient);
42 | }
43 |
44 | public PullToRefreshWebView(Context context, AttributeSet attrs) {
45 | super(context, attrs);
46 |
47 | /**
48 | * Added so that by default, Pull-to-Refresh refreshes the page
49 | */
50 | setOnRefreshListener(defaultOnRefreshListener);
51 | mRefreshableView.setWebChromeClient(defaultWebChromeClient);
52 | }
53 |
54 | public PullToRefreshWebView(Context context, Mode mode) {
55 | super(context, mode);
56 |
57 | /**
58 | * Added so that by default, Pull-to-Refresh refreshes the page
59 | */
60 | setOnRefreshListener(defaultOnRefreshListener);
61 | mRefreshableView.setWebChromeClient(defaultWebChromeClient);
62 | }
63 |
64 | public PullToRefreshWebView(Context context, Mode mode, AnimationStyle style) {
65 | super(context, mode, style);
66 |
67 | /**
68 | * Added so that by default, Pull-to-Refresh refreshes the page
69 | */
70 | setOnRefreshListener(defaultOnRefreshListener);
71 | mRefreshableView.setWebChromeClient(defaultWebChromeClient);
72 | }
73 |
74 | @Override
75 | public final Orientation getPullToRefreshScrollDirection() {
76 | return Orientation.VERTICAL;
77 | }
78 |
79 | @Override
80 | protected WebView createRefreshableView(Context context, AttributeSet attrs) {
81 | WebView webView;
82 | if (VERSION.SDK_INT >= VERSION_CODES.GINGERBREAD) {
83 | webView = new InternalWebViewSDK9(context, attrs);
84 | } else {
85 | webView = new WebView(context, attrs);
86 | }
87 |
88 | webView.setId(R.id.webview);
89 | return webView;
90 | }
91 |
92 | @Override
93 | protected boolean isReadyForPullStart() {
94 | return mRefreshableView.getScrollY() == 0;
95 | }
96 |
97 | @Override
98 | protected boolean isReadyForPullEnd() {
99 | float exactContentHeight = (float) Math.floor(mRefreshableView.getContentHeight() * mRefreshableView.getScale());
100 | return mRefreshableView.getScrollY() >= (exactContentHeight - mRefreshableView.getHeight());
101 | }
102 |
103 | @Override
104 | protected void onPtrRestoreInstanceState(Bundle savedInstanceState) {
105 | super.onPtrRestoreInstanceState(savedInstanceState);
106 | mRefreshableView.restoreState(savedInstanceState);
107 | }
108 |
109 | @Override
110 | protected void onPtrSaveInstanceState(Bundle saveState) {
111 | super.onPtrSaveInstanceState(saveState);
112 | mRefreshableView.saveState(saveState);
113 | }
114 |
115 | @TargetApi(9)
116 | final class InternalWebViewSDK9 extends WebView {
117 |
118 | // WebView doesn't always scroll back to it's edge so we add some
119 | // fuzziness
120 | static final int OVERSCROLL_FUZZY_THRESHOLD = 2;
121 |
122 | // WebView seems quite reluctant to overscroll so we use the scale
123 | // factor to scale it's value
124 | static final float OVERSCROLL_SCALE_FACTOR = 1.5f;
125 |
126 | public InternalWebViewSDK9(Context context, AttributeSet attrs) {
127 | super(context, attrs);
128 | }
129 |
130 | @Override
131 | protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX,
132 | int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
133 |
134 | final boolean returnValue = super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX,
135 | scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
136 |
137 | // Does all of the hard work...
138 | OverscrollHelper.overScrollBy(PullToRefreshWebView.this, deltaX, scrollX, deltaY, scrollY,
139 | getScrollRange(), OVERSCROLL_FUZZY_THRESHOLD, OVERSCROLL_SCALE_FACTOR, isTouchEvent);
140 |
141 | return returnValue;
142 | }
143 |
144 | private int getScrollRange() {
145 | return (int) Math.max(0, Math.floor(mRefreshableView.getContentHeight() * mRefreshableView.getScale())
146 | - (getHeight() - getPaddingBottom() - getPaddingTop()));
147 | }
148 | }
149 | }
150 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/internal/FlipLoadingLayout.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011, 2012 Chris Banes.
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 | package com.yinglan.swiperefresh.internal;
17 |
18 | import android.annotation.SuppressLint;
19 | import android.content.Context;
20 | import android.content.res.TypedArray;
21 | import android.graphics.Matrix;
22 | import android.graphics.drawable.Drawable;
23 | import android.view.View;
24 | import android.view.ViewGroup;
25 | import android.view.animation.Animation;
26 | import android.view.animation.RotateAnimation;
27 | import android.widget.ImageView.ScaleType;
28 |
29 | import com.yinglan.swiperefresh.PullToRefreshBase.Mode;
30 | import com.yinglan.swiperefresh.PullToRefreshBase.Orientation;
31 | import com.yinglan.swiperefresh.R;
32 |
33 | @SuppressLint("ViewConstructor")
34 | public class FlipLoadingLayout extends LoadingLayout {
35 |
36 | static final int FLIP_ANIMATION_DURATION = 150;
37 |
38 | private final Animation mRotateAnimation, mResetRotateAnimation;
39 |
40 | public FlipLoadingLayout(Context context, final Mode mode, final Orientation scrollDirection, TypedArray attrs) {
41 | super(context, mode, scrollDirection, attrs);
42 |
43 | final int rotateAngle = mode == Mode.PULL_FROM_START ? -180 : 180;
44 |
45 | mRotateAnimation = new RotateAnimation(0, rotateAngle, Animation.RELATIVE_TO_SELF, 0.5f,
46 | Animation.RELATIVE_TO_SELF, 0.5f);
47 | mRotateAnimation.setInterpolator(ANIMATION_INTERPOLATOR);
48 | mRotateAnimation.setDuration(FLIP_ANIMATION_DURATION);
49 | mRotateAnimation.setFillAfter(true);
50 |
51 | mResetRotateAnimation = new RotateAnimation(rotateAngle, 0, Animation.RELATIVE_TO_SELF, 0.5f,
52 | Animation.RELATIVE_TO_SELF, 0.5f);
53 | mResetRotateAnimation.setInterpolator(ANIMATION_INTERPOLATOR);
54 | mResetRotateAnimation.setDuration(FLIP_ANIMATION_DURATION);
55 | mResetRotateAnimation.setFillAfter(true);
56 | }
57 |
58 | @Override
59 | protected void onLoadingDrawableSet(Drawable imageDrawable) {
60 | if (null != imageDrawable) {
61 | final int dHeight = imageDrawable.getIntrinsicHeight();
62 | final int dWidth = imageDrawable.getIntrinsicWidth();
63 |
64 | /**
65 | * We need to set the width/height of the ImageView so that it is
66 | * square with each side the size of the largest drawable dimension.
67 | * This is so that it doesn't clip when rotated.
68 | */
69 | ViewGroup.LayoutParams lp = mHeaderImage.getLayoutParams();
70 | lp.width = lp.height = Math.max(dHeight, dWidth);
71 | mHeaderImage.requestLayout();
72 |
73 | /**
74 | * We now rotate the Drawable so that is at the correct rotation,
75 | * and is centered.
76 | */
77 | mHeaderImage.setScaleType(ScaleType.MATRIX);
78 | Matrix matrix = new Matrix();
79 | matrix.postTranslate((lp.width - dWidth) / 2f, (lp.height - dHeight) / 2f);
80 | matrix.postRotate(getDrawableRotationAngle(), lp.width / 2f, lp.height / 2f);
81 | mHeaderImage.setImageMatrix(matrix);
82 | }
83 | }
84 |
85 | @Override
86 | protected void onPullImpl(float scaleOfLayout) {
87 | // NO-OP
88 | }
89 |
90 | @Override
91 | protected void pullToRefreshImpl() {
92 | // Only start reset Animation, we've previously show the rotate anim
93 | if (mRotateAnimation == mHeaderImage.getAnimation()) {
94 | mHeaderImage.startAnimation(mResetRotateAnimation);
95 | }
96 | }
97 |
98 | @Override
99 | protected void refreshingImpl() {
100 | mHeaderImage.clearAnimation();
101 | mHeaderImage.setVisibility(View.INVISIBLE);
102 | mHeaderProgress.setVisibility(View.VISIBLE);
103 | }
104 |
105 | @Override
106 | protected void releaseToRefreshImpl() {
107 | mHeaderImage.startAnimation(mRotateAnimation);
108 | }
109 |
110 | @Override
111 | protected void resetImpl() {
112 | mHeaderImage.clearAnimation();
113 | mHeaderProgress.setVisibility(View.GONE);
114 | mHeaderImage.setVisibility(View.VISIBLE);
115 | }
116 |
117 | @Override
118 | protected int getDefaultDrawableResId() {
119 | return R.drawable.default_ptr_flip;
120 | }
121 |
122 | private float getDrawableRotationAngle() {
123 | float angle = 0f;
124 | switch (mMode) {
125 | case PULL_FROM_END:
126 | if (mScrollDirection == Orientation.HORIZONTAL) {
127 | angle = 90f;
128 | } else {
129 | angle = 180f;
130 | }
131 | break;
132 |
133 | case PULL_FROM_START:
134 | if (mScrollDirection == Orientation.HORIZONTAL) {
135 | angle = 270f;
136 | }
137 | break;
138 |
139 | default:
140 | break;
141 | }
142 |
143 | return angle;
144 | }
145 |
146 | }
147 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/internal/IndicatorLayout.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011, 2012 Chris Banes.
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 | package com.yinglan.swiperefresh.internal;
17 |
18 | import android.annotation.SuppressLint;
19 | import android.content.Context;
20 | import android.graphics.Matrix;
21 | import android.graphics.drawable.Drawable;
22 | import android.view.View;
23 | import android.view.animation.Animation;
24 | import android.view.animation.Animation.AnimationListener;
25 | import android.view.animation.AnimationUtils;
26 | import android.view.animation.Interpolator;
27 | import android.view.animation.LinearInterpolator;
28 | import android.view.animation.RotateAnimation;
29 | import android.widget.FrameLayout;
30 | import android.widget.ImageView;
31 | import android.widget.ImageView.ScaleType;
32 |
33 | import com.yinglan.swiperefresh.PullToRefreshBase;
34 | import com.yinglan.swiperefresh.R;
35 |
36 | @SuppressLint("ViewConstructor")
37 | public class IndicatorLayout extends FrameLayout implements AnimationListener {
38 |
39 | static final int DEFAULT_ROTATION_ANIMATION_DURATION = 150;
40 |
41 | private Animation mInAnim, mOutAnim;
42 | private ImageView mArrowImageView;
43 |
44 | private final Animation mRotateAnimation, mResetRotateAnimation;
45 |
46 | public IndicatorLayout(Context context, PullToRefreshBase.Mode mode) {
47 | super(context);
48 | mArrowImageView = new ImageView(context);
49 |
50 | Drawable arrowD = getResources().getDrawable(R.drawable.indicator_arrow);
51 | mArrowImageView.setImageDrawable(arrowD);
52 |
53 | final int padding = getResources().getDimensionPixelSize(R.dimen.indicator_internal_padding);
54 | mArrowImageView.setPadding(padding, padding, padding, padding);
55 | addView(mArrowImageView);
56 |
57 | int inAnimResId, outAnimResId;
58 | switch (mode) {
59 | case PULL_FROM_END:
60 | inAnimResId = R.anim.slide_in_from_bottom;
61 | outAnimResId = R.anim.slide_out_to_bottom;
62 | setBackgroundResource(R.drawable.indicator_bg_bottom);
63 |
64 | // Rotate Arrow so it's pointing the correct way
65 | mArrowImageView.setScaleType(ScaleType.MATRIX);
66 | Matrix matrix = new Matrix();
67 | matrix.setRotate(180f, arrowD.getIntrinsicWidth() / 2f, arrowD.getIntrinsicHeight() / 2f);
68 | mArrowImageView.setImageMatrix(matrix);
69 | break;
70 | default:
71 | case PULL_FROM_START:
72 | inAnimResId = R.anim.slide_in_from_top;
73 | outAnimResId = R.anim.slide_out_to_top;
74 | setBackgroundResource(R.drawable.indicator_bg_top);
75 | break;
76 | }
77 |
78 | mInAnim = AnimationUtils.loadAnimation(context, inAnimResId);
79 | mInAnim.setAnimationListener(this);
80 |
81 | mOutAnim = AnimationUtils.loadAnimation(context, outAnimResId);
82 | mOutAnim.setAnimationListener(this);
83 |
84 | final Interpolator interpolator = new LinearInterpolator();
85 | mRotateAnimation = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
86 | 0.5f);
87 | mRotateAnimation.setInterpolator(interpolator);
88 | mRotateAnimation.setDuration(DEFAULT_ROTATION_ANIMATION_DURATION);
89 | mRotateAnimation.setFillAfter(true);
90 |
91 | mResetRotateAnimation = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF, 0.5f,
92 | Animation.RELATIVE_TO_SELF, 0.5f);
93 | mResetRotateAnimation.setInterpolator(interpolator);
94 | mResetRotateAnimation.setDuration(DEFAULT_ROTATION_ANIMATION_DURATION);
95 | mResetRotateAnimation.setFillAfter(true);
96 |
97 | }
98 |
99 | public final boolean isVisible() {
100 | Animation currentAnim = getAnimation();
101 | if (null != currentAnim) {
102 | return mInAnim == currentAnim;
103 | }
104 |
105 | return getVisibility() == View.VISIBLE;
106 | }
107 |
108 | public void hide() {
109 | startAnimation(mOutAnim);
110 | }
111 |
112 | public void show() {
113 | mArrowImageView.clearAnimation();
114 | startAnimation(mInAnim);
115 | }
116 |
117 | @Override
118 | public void onAnimationEnd(Animation animation) {
119 | if (animation == mOutAnim) {
120 | mArrowImageView.clearAnimation();
121 | setVisibility(View.GONE);
122 | } else if (animation == mInAnim) {
123 | setVisibility(View.VISIBLE);
124 | }
125 |
126 | clearAnimation();
127 | }
128 |
129 | @Override
130 | public void onAnimationRepeat(Animation animation) {
131 | // NO-OP
132 | }
133 |
134 | @Override
135 | public void onAnimationStart(Animation animation) {
136 | setVisibility(View.VISIBLE);
137 | }
138 |
139 | public void releaseToRefresh() {
140 | mArrowImageView.startAnimation(mRotateAnimation);
141 | }
142 |
143 | public void pullToRefresh() {
144 | mArrowImageView.startAnimation(mResetRotateAnimation);
145 | }
146 |
147 | }
148 |
--------------------------------------------------------------------------------
/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 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/OverscrollHelper.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011, 2012 Chris Banes.
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 | package com.yinglan.swiperefresh;
17 |
18 | import android.annotation.TargetApi;
19 | import android.util.Log;
20 | import android.view.View;
21 |
22 | import com.yinglan.swiperefresh.PullToRefreshBase.Mode;
23 | import com.yinglan.swiperefresh.PullToRefreshBase.State;
24 |
25 | @TargetApi(9)
26 | public final class OverscrollHelper {
27 |
28 | static final String LOG_TAG = "OverscrollHelper";
29 | static final float DEFAULT_OVERSCROLL_SCALE = 1f;
30 |
31 | /**
32 | * Helper method for Overscrolling that encapsulates all of the necessary
33 | * function.
34 | * This should only be used on AdapterView's such as ListView as it just
35 | * calls through to overScrollBy() with the scrollRange = 0. AdapterView's
36 | * do not have a scroll range (i.e. getScrollY() doesn't work).
37 | *
38 | */
39 | public static void overScrollBy(final PullToRefreshBase> view, final int deltaX, final int scrollX,
40 | final int deltaY, final int scrollY, final boolean isTouchEvent) {
41 | overScrollBy(view, deltaX, scrollX, deltaY, scrollY, 0, isTouchEvent);
42 | }
43 |
44 | /**
45 | * Helper method for Overscrolling that encapsulates all of the necessary
46 | * function. This version of the call is used for Views that need to specify
47 | * a Scroll Range but scroll back to it's edge correctly.
48 | *
49 | */
50 | public static void overScrollBy(final PullToRefreshBase> view, final int deltaX, final int scrollX,
51 | final int deltaY, final int scrollY, final int scrollRange, final boolean isTouchEvent) {
52 | overScrollBy(view, deltaX, scrollX, deltaY, scrollY, scrollRange, 0, DEFAULT_OVERSCROLL_SCALE, isTouchEvent);
53 | }
54 |
55 | /**
56 | * Helper method for Overscrolling that encapsulates all of the necessary
57 | * function. This is the advanced version of the call.
58 | *
59 | */
60 | public static void overScrollBy(final PullToRefreshBase> view, final int deltaX, final int scrollX,
61 | final int deltaY, final int scrollY, final int scrollRange, final int fuzzyThreshold,
62 | final float scaleFactor, final boolean isTouchEvent) {
63 |
64 | final int deltaValue, currentScrollValue, scrollValue;
65 | switch (view.getPullToRefreshScrollDirection()) {
66 | case HORIZONTAL:
67 | deltaValue = deltaX;
68 | scrollValue = scrollX;
69 | currentScrollValue = view.getScrollX();
70 | break;
71 | case VERTICAL:
72 | default:
73 | deltaValue = deltaY;
74 | scrollValue = scrollY;
75 | currentScrollValue = view.getScrollY();
76 | break;
77 | }
78 |
79 | // Check that OverScroll is enabled and that we're not currently
80 | // refreshing.
81 | if (view.isPullToRefreshOverScrollEnabled() && !view.isRefreshing()) {
82 | final Mode mode = view.getMode();
83 |
84 | // Check that Pull-to-Refresh is enabled, and the event isn't from
85 | // touch
86 | if (mode.permitsPullToRefresh() && !isTouchEvent && deltaValue != 0) {
87 | final int newScrollValue = (deltaValue + scrollValue);
88 |
89 | if (PullToRefreshBase.DEBUG) {
90 | Log.d(LOG_TAG, "OverScroll. DeltaX: " + deltaX + ", ScrollX: " + scrollX + ", DeltaY: " + deltaY
91 | + ", ScrollY: " + scrollY + ", NewY: " + newScrollValue + ", ScrollRange: " + scrollRange
92 | + ", CurrentScroll: " + currentScrollValue);
93 | }
94 |
95 | if (newScrollValue < (0 - fuzzyThreshold)) {
96 | // Check the mode supports the overscroll direction, and
97 | // then move scroll
98 | if (mode.showHeaderLoadingLayout()) {
99 | // If we're currently at zero, we're about to start
100 | // overscrolling, so change the state
101 | if (currentScrollValue == 0) {
102 | view.setState(State.OVERSCROLLING);
103 | }
104 |
105 | view.setHeaderScroll((int) (scaleFactor * (currentScrollValue + newScrollValue)));
106 | }
107 | } else if (newScrollValue > (scrollRange + fuzzyThreshold)) {
108 | // Check the mode supports the overscroll direction, and
109 | // then move scroll
110 | if (mode.showFooterLoadingLayout()) {
111 | // If we're currently at zero, we're about to start
112 | // overscrolling, so change the state
113 | if (currentScrollValue == 0) {
114 | view.setState(State.OVERSCROLLING);
115 | }
116 |
117 | view.setHeaderScroll((int) (scaleFactor * (currentScrollValue + newScrollValue - scrollRange)));
118 | }
119 | } else if (Math.abs(newScrollValue) <= fuzzyThreshold
120 | || Math.abs(newScrollValue - scrollRange) <= fuzzyThreshold) {
121 | // Means we've stopped overscrolling, so scroll back to 0
122 | view.setState(State.RESET);
123 | }
124 | } else if (isTouchEvent && State.OVERSCROLLING == view.getState()) {
125 | // This condition means that we were overscrolling from a fling,
126 | // but the user has touched the View and is now overscrolling
127 | // from touch instead. We need to just reset.
128 | view.setState(State.RESET);
129 | }
130 | }
131 | }
132 |
133 | static boolean isAndroidOverScrollEnabled(View view) {
134 | return view.getOverScrollMode() != View.OVER_SCROLL_NEVER;
135 | }
136 | }
137 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yinglan/demo/OrdinaryActivity.java:
--------------------------------------------------------------------------------
1 | package com.yinglan.demo;
2 |
3 | import android.graphics.Color;
4 | import android.graphics.drawable.ColorDrawable;
5 | import android.os.Bundle;
6 | import android.os.Handler;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.util.TypedValue;
9 | import android.widget.Toast;
10 |
11 | import com.yinglan.demo.adapter.ListAdapter;
12 | import com.yinglan.swiperefresh.PullToRefreshBase;
13 | import com.yinglan.swiperefresh.PullToRefreshSwipeListView;
14 | import com.yinglan.swiperefresh.SwipeMenu;
15 | import com.yinglan.swiperefresh.SwipeMenuCreator;
16 | import com.yinglan.swiperefresh.SwipeMenuItem;
17 | import com.yinglan.swiperefresh.SwipeMenuListView;
18 |
19 | /**
20 | * Created by yinglan
21 | */
22 | public class OrdinaryActivity extends AppCompatActivity {
23 |
24 | private PullToRefreshSwipeListView mPullRefreshListView;
25 |
26 | @Override
27 | protected void onCreate(Bundle savedInstanceState) {
28 | super.onCreate(savedInstanceState);
29 | setContentView(R.layout.activity_list);
30 |
31 | initView();
32 | }
33 |
34 | private void initView() {
35 |
36 | mPullRefreshListView = (PullToRefreshSwipeListView) findViewById(R.id.pull_to_refresh_list);
37 | mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
38 | @Override
39 | public void onPullDownToRefresh(PullToRefreshBase refreshView) {
40 | new Handler().postDelayed(new Runnable() {
41 | @Override
42 | public void run() {
43 | Toast.makeText(OrdinaryActivity.this, "ok", Toast.LENGTH_SHORT).show();
44 | mPullRefreshListView.onRefreshComplete();
45 | }
46 | }, 2000);
47 | }
48 |
49 | @Override
50 | public void onPullUpToRefresh(PullToRefreshBase refreshView) {
51 | new Handler().postDelayed(new Runnable() {
52 | @Override
53 | public void run() {
54 | Toast.makeText(OrdinaryActivity.this, "ok", Toast.LENGTH_SHORT).show();
55 | mPullRefreshListView.onRefreshComplete();
56 | }
57 | }, 2000);
58 | }
59 | });
60 |
61 | ListAdapter adapter = new ListAdapter(this);
62 |
63 | mPullRefreshListView.setAdapter(adapter);
64 | mPullRefreshListView.getRefreshableView().setOnMenuItemClickListener(adapter);
65 | mPullRefreshListView.getRefreshableView().setMenuCreator(creator);
66 | }
67 |
68 |
69 | private SwipeMenuCreator creator = new SwipeMenuCreator() {
70 |
71 | @Override
72 | public void create(SwipeMenu menu) {
73 | // Create different menus depending on the view type
74 | switch (menu.getViewType()) {
75 | case 0:
76 | createMenu1(menu);
77 | break;
78 | case 1:
79 | createMenu2(menu);
80 | break;
81 | case 2:
82 | createMenu3(menu);
83 | break;
84 | }
85 | }
86 |
87 | private void createMenu1(SwipeMenu menu) {
88 | SwipeMenuItem item1 = new SwipeMenuItem(
89 | OrdinaryActivity.this);
90 | item1.setBackground(new ColorDrawable(Color.rgb(0xE5, 0x18,
91 | 0x5E)));
92 | item1.setWidth(dp2px(90));
93 | item1.setIcon(R.drawable.ic_action_favorite);
94 | menu.addMenuItem(item1);
95 | SwipeMenuItem item2 = new SwipeMenuItem(
96 | getApplicationContext());
97 | item2.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
98 | 0xCE)));
99 | item2.setWidth(dp2px(90));
100 | item2.setIcon(R.drawable.ic_action_good);
101 | menu.addMenuItem(item2);
102 | }
103 |
104 | private void createMenu2(SwipeMenu menu) {
105 | SwipeMenuItem openItem = new SwipeMenuItem(
106 | getApplicationContext());
107 | // set item background
108 | openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
109 | 0xCE)));
110 | // set item width
111 | openItem.setWidth(dp2px(90));
112 | // set item title
113 | openItem.setTitle("Open");
114 | // set item title fontsize
115 | openItem.setTitleSize(18);
116 | // set item title font color
117 | openItem.setTitleColor(Color.WHITE);
118 | // add to menu
119 | menu.addMenuItem(openItem);
120 |
121 | // create "delete" item
122 | SwipeMenuItem deleteItem = new SwipeMenuItem(
123 | getApplicationContext());
124 | // set item background
125 | deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
126 | 0x3F, 0x25)));
127 | // set item width
128 | deleteItem.setWidth(dp2px(90));
129 | // set a icon
130 | deleteItem.setIcon(R.drawable.ic_delete);
131 | // add to menu
132 | menu.addMenuItem(deleteItem);
133 |
134 | SwipeMenuItem item1 = new SwipeMenuItem(
135 | OrdinaryActivity.this);
136 | item1.setBackground(new ColorDrawable(Color.rgb(0xE5, 0xE0,
137 | 0x3F)));
138 | item1.setWidth(dp2px(90));
139 | item1.setIcon(R.drawable.ic_action_important);
140 | menu.addMenuItem(item1);
141 | }
142 |
143 | private void createMenu3(SwipeMenu menu) {
144 | SwipeMenuItem item1 = new SwipeMenuItem(
145 | OrdinaryActivity.this);
146 | item1.setBackground(new ColorDrawable(Color.rgb(0x30, 0xB1,
147 | 0xF5)));
148 | item1.setWidth(dp2px(90));
149 | item1.setIcon(R.drawable.ic_action_about);
150 | menu.addMenuItem(item1);
151 | SwipeMenuItem item2 = new SwipeMenuItem(
152 | getApplicationContext());
153 | item2.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
154 | 0xCE)));
155 | item2.setWidth(dp2px(90));
156 | item2.setIcon(R.drawable.ic_action_share);
157 | menu.addMenuItem(item2);
158 | }
159 | };
160 |
161 | private int dp2px(int dp) {
162 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
163 | getResources().getDisplayMetrics());
164 | }
165 | }
166 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/IPullToRefresh.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011, 2012 Chris Banes.
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 | package com.yinglan.swiperefresh;
17 |
18 | import android.view.View;
19 | import android.view.animation.Interpolator;
20 |
21 | import com.yinglan.swiperefresh.PullToRefreshBase.Mode;
22 | import com.yinglan.swiperefresh.PullToRefreshBase.OnPullEventListener;
23 | import com.yinglan.swiperefresh.PullToRefreshBase.OnRefreshListener;
24 | import com.yinglan.swiperefresh.PullToRefreshBase.OnRefreshListener2;
25 | import com.yinglan.swiperefresh.PullToRefreshBase.State;
26 |
27 | public interface IPullToRefresh {
28 |
29 | /**
30 | * Demos the Pull-to-Refresh functionality to the user so that they are
31 | * aware it is there. This could be useful when the user first opens your
32 | * app, etc. The animation will only happen if the Refresh View (ListView,
33 | * ScrollView, etc) is in a state where a Pull-to-Refresh could occur by a
34 | * user's touch gesture (i.e. scrolled to the top/bottom).
35 | *
36 | */
37 | public boolean demo();
38 |
39 | /**
40 | * Get the mode that this view is currently in. This is only really useful
41 | * when using Mode.BOTH.
42 | *
43 | */
44 | public Mode getCurrentMode();
45 |
46 | /**
47 | * Returns whether the Touch Events are filtered or not. If true is
48 | * returned, then the View will only use touch events where the difference
49 | * in the Y-axis is greater than the difference in the X-axis. This means
50 | * that the View will not interfere when it is used in a horizontal
51 | * scrolling View (such as a ViewPager).
52 | *
53 | */
54 | public boolean getFilterTouchEvents();
55 |
56 | /**
57 | * Returns a proxy object which allows you to call methods on all of the
58 | * LoadingLayouts (the Views which show when Pulling/Refreshing).
59 | * You should not keep the result of this method any longer than you need
60 | * it.
61 | *
62 | * @return Object which will proxy any calls you make on it, to all of the
63 | * LoadingLayouts.
64 | */
65 | public ILoadingLayout getLoadingLayoutProxy();
66 |
67 | /**
68 | * Returns a proxy object which allows you to call methods on the
69 | * LoadingLayouts (the Views which show when Pulling/Refreshing). The actual
70 | * LoadingLayout(s) which will be affected, are chosen by the parameters you
71 | * give.
72 | * You should not keep the result of this method any longer than you need
73 | * it.
74 | *
75 | */
76 | public ILoadingLayout getLoadingLayoutProxy(boolean includeStart, boolean includeEnd);
77 |
78 | /**
79 | * Get the mode that this view has been set to. If this returns
80 | * check which mode the view is currently in
81 | *
82 | */
83 | public Mode getMode();
84 |
85 | /**
86 | * Get the Wrapped Refreshable View. Anything returned here has already been
87 | * added to the content view.
88 | *
89 | */
90 | public T getRefreshableView();
91 |
92 | /**
93 | * Get whether the 'Refreshing' View should be automatically shown when
94 | * refreshing. Returns true by default.
95 | *
96 | */
97 | public boolean getShowViewWhileRefreshing();
98 |
99 | /**
100 | */
101 | public State getState();
102 |
103 | /**
104 | * Whether Pull-to-Refresh is enabled
105 | *
106 | */
107 | public boolean isPullToRefreshEnabled();
108 |
109 | /**
110 | * Gets whether Overscroll support is enabled. This is different to
111 | * Android's standard Overscroll support (the edge-glow) which is available
112 | * from GINGERBREAD onwards
113 | *
114 | */
115 | public boolean isPullToRefreshOverScrollEnabled();
116 |
117 | /**
118 | * Returns whether the Widget is currently in the Refreshing mState
119 | *
120 | */
121 | public boolean isRefreshing();
122 |
123 | /**
124 | * Returns whether the widget has enabled scrolling on the Refreshable View
125 | * while refreshing.
126 | *
127 | */
128 | public boolean isScrollingWhileRefreshingEnabled();
129 |
130 | /**
131 | * Mark the current Refresh as complete. Will Reset the UI and hide the
132 | * Refreshing View
133 | */
134 | public void onRefreshComplete();
135 |
136 | /**
137 | * Set the Touch Events to be filtered or not. If set to true, then the View
138 | * will only use touch events where the difference in the Y-axis is greater
139 | * than the difference in the X-axis. This means that the View will not
140 | * interfere when it is used in a horizontal scrolling View (such as a
141 | * ViewPager), but will restrict which types of finger scrolls will trigger
142 | * the View.
143 | *
144 | */
145 | public void setFilterTouchEvents(boolean filterEvents);
146 |
147 | /**
148 | * Set the mode of Pull-to-Refresh that this view will use.
149 | *
150 | */
151 | public void setMode(Mode mode);
152 |
153 | /**
154 | * Set OnPullEventListener for the Widget
155 | *
156 | */
157 | public void setOnPullEventListener(OnPullEventListener listener);
158 |
159 | /**
160 | * Set OnRefreshListener for the Widget
161 | *
162 | */
163 | public void setOnRefreshListener(OnRefreshListener listener);
164 |
165 | /**
166 | * Set OnRefreshListener for the Widget
167 | *
168 | */
169 | public void setOnRefreshListener(OnRefreshListener2 listener);
170 |
171 | /**
172 | * Sets whether Overscroll support is enabled. This is different to
173 | * Android's standard Overscroll support (the edge-glow). This setting only
174 | * takes effect when running on device with Android v2.3 or greater.
175 | *
176 | */
177 | public void setPullToRefreshOverScrollEnabled(boolean enabled);
178 |
179 | /**
180 | * Sets the Widget to be in the refresh state. The UI will be updated to
181 | * show the 'Refreshing' view, and be scrolled to show such.
182 | */
183 | public void setRefreshing();
184 |
185 | /**
186 | * Sets the Widget to be in the refresh state. The UI will be updated to
187 | * show the 'Refreshing' view.
188 | *
189 | */
190 | public void setRefreshing(boolean doScroll);
191 |
192 | /**
193 | * Sets the Animation Interpolator that is used for animated scrolling.
194 | * Defaults to a DecelerateInterpolator
195 | *
196 | */
197 | public void setScrollAnimationInterpolator(Interpolator interpolator);
198 |
199 | /**
200 | * By default the Widget disables scrolling on the Refreshable View while
201 | * refreshing. This method can change this behaviour.
202 | *
203 | */
204 | public void setScrollingWhileRefreshingEnabled(boolean scrollingWhileRefreshingEnabled);
205 |
206 | /**
207 | * A mutator to enable/disable whether the 'Refreshing' View should be
208 | * automatically shown when refreshing.
209 | *
210 | */
211 | public void setShowViewWhileRefreshing(boolean showView);
212 |
213 | }
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/SwipeMenuLayout.java:
--------------------------------------------------------------------------------
1 | package com.yinglan.swiperefresh;
2 |
3 | import android.content.Context;
4 | import android.support.v4.view.GestureDetectorCompat;
5 | import android.support.v4.widget.ScrollerCompat;
6 | import android.util.AttributeSet;
7 | import android.util.Log;
8 | import android.util.TypedValue;
9 | import android.view.GestureDetector.OnGestureListener;
10 | import android.view.GestureDetector.SimpleOnGestureListener;
11 | import android.view.MotionEvent;
12 | import android.view.View;
13 | import android.view.animation.Interpolator;
14 | import android.widget.AbsListView;
15 | import android.widget.FrameLayout;
16 |
17 | public class SwipeMenuLayout extends FrameLayout {
18 |
19 | private static final int CONTENT_VIEW_ID = 1;
20 | private static final int MENU_VIEW_ID = 2;
21 |
22 | private static final int STATE_CLOSE = 0;
23 | private static final int STATE_OPEN = 1;
24 |
25 | private int mSwipeDirection;
26 |
27 | private View mContentView;
28 | private SwipeMenuView mMenuView;
29 | private int mDownX;
30 | private int state = STATE_CLOSE;
31 | private GestureDetectorCompat mGestureDetector;
32 | private OnGestureListener mGestureListener;
33 | private boolean isFling;
34 | private int MIN_FLING = dp2px(15);
35 | private int MAX_VELOCITYX = -dp2px(500);
36 | private ScrollerCompat mOpenScroller;
37 | private ScrollerCompat mCloseScroller;
38 | private int mBaseX;
39 | private int position;
40 | private Interpolator mCloseInterpolator;
41 | private Interpolator mOpenInterpolator;
42 |
43 | private boolean mSwipEnable = true;
44 |
45 | public SwipeMenuLayout(View contentView, SwipeMenuView menuView) {
46 | this(contentView, menuView, null, null);
47 | }
48 |
49 | public SwipeMenuLayout(View contentView, SwipeMenuView menuView,
50 | Interpolator closeInterpolator, Interpolator openInterpolator) {
51 | super(contentView.getContext());
52 | mCloseInterpolator = closeInterpolator;
53 | mOpenInterpolator = openInterpolator;
54 | mContentView = contentView;
55 | mMenuView = menuView;
56 | mMenuView.setLayout(this);
57 | init();
58 | }
59 |
60 | // private SwipeMenuLayout(Context context, AttributeSet attrs, int
61 | // defStyle) {
62 | // super(context, attrs, defStyle);
63 | // }
64 |
65 | private SwipeMenuLayout(Context context, AttributeSet attrs) {
66 | super(context, attrs);
67 | }
68 |
69 | private SwipeMenuLayout(Context context) {
70 | super(context);
71 | }
72 |
73 | public int getPosition() {
74 | return position;
75 | }
76 |
77 | public void setPosition(int position) {
78 | this.position = position;
79 | mMenuView.setPosition(position);
80 | }
81 |
82 | public void setSwipeDirection(int swipeDirection) {
83 | mSwipeDirection = swipeDirection;
84 | }
85 |
86 | private void init() {
87 | setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,
88 | LayoutParams.WRAP_CONTENT));
89 | mGestureListener = new SimpleOnGestureListener() {
90 | @Override
91 | public boolean onDown(MotionEvent e) {
92 | isFling = false;
93 | return true;
94 | }
95 |
96 | @Override
97 | public boolean onFling(MotionEvent e1, MotionEvent e2,
98 | float velocityX, float velocityY) {
99 | // TODO
100 | if (Math.abs(e1.getX() - e2.getX()) > MIN_FLING
101 | && velocityX < MAX_VELOCITYX) {
102 | isFling = true;
103 | }
104 | // Log.i("byz", MAX_VELOCITYX + ", velocityX = " + velocityX);
105 | return super.onFling(e1, e2, velocityX, velocityY);
106 | }
107 | };
108 | mGestureDetector = new GestureDetectorCompat(getContext(),
109 | mGestureListener);
110 |
111 | // mScroller = ScrollerCompat.create(getContext(), new
112 | // BounceInterpolator());
113 | if (mCloseInterpolator != null) {
114 | mCloseScroller = ScrollerCompat.create(getContext(),
115 | mCloseInterpolator);
116 | } else {
117 | mCloseScroller = ScrollerCompat.create(getContext());
118 | }
119 | if (mOpenInterpolator != null) {
120 | mOpenScroller = ScrollerCompat.create(getContext(),
121 | mOpenInterpolator);
122 | } else {
123 | mOpenScroller = ScrollerCompat.create(getContext());
124 | }
125 |
126 | LayoutParams contentParams = new LayoutParams(
127 | LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
128 | mContentView.setLayoutParams(contentParams);
129 | if (mContentView.getId() < 1) {
130 | mContentView.setId(CONTENT_VIEW_ID);
131 | }
132 |
133 | mMenuView.setId(MENU_VIEW_ID);
134 | mMenuView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
135 | mContentView.getHeight()));
136 |
137 | addView(mContentView);
138 | addView(mMenuView);
139 |
140 | // if (mContentView.getBackground() == null) {
141 | // mContentView.setBackgroundColor(Color.WHITE);
142 | // }
143 |
144 | // in android 2.x, MenuView height is MATCH_PARENT is not work.
145 | // getViewTreeObserver().addOnGlobalLayoutListener(
146 | // new OnGlobalLayoutListener() {
147 | // @Override
148 | // public void onGlobalLayout() {
149 | // setMenuHeight(mContentView.getHeight());
150 | // // getViewTreeObserver()
151 | // // .removeGlobalOnLayoutListener(this);
152 | // }
153 | // });
154 |
155 | }
156 |
157 | @Override
158 | protected void onAttachedToWindow() {
159 | super.onAttachedToWindow();
160 | }
161 |
162 | @Override
163 | protected void onSizeChanged(int w, int h, int oldw, int oldh) {
164 | super.onSizeChanged(w, h, oldw, oldh);
165 | }
166 |
167 | public boolean onSwipe(MotionEvent event) {
168 | mGestureDetector.onTouchEvent(event);
169 | switch (event.getAction()) {
170 | case MotionEvent.ACTION_DOWN:
171 | mDownX = (int) event.getX();
172 | isFling = false;
173 | break;
174 | case MotionEvent.ACTION_MOVE:
175 | // Log.i("byz", "downX = " + mDownX + ", moveX = " + event.getX());
176 | int dis = (int) (mDownX - event.getX());
177 | if (state == STATE_OPEN) {
178 | dis += mMenuView.getWidth()*mSwipeDirection;;
179 | }
180 | swipe(dis);
181 | break;
182 | case MotionEvent.ACTION_UP:
183 | if ((isFling || Math.abs(mDownX - event.getX()) > (mMenuView.getWidth() / 2)) &&
184 | Math.signum(mDownX - event.getX()) == mSwipeDirection) {
185 | if(state == STATE_OPEN){
186 | smoothCloseMenu();
187 | }else{
188 |
189 | // open
190 | smoothOpenMenu();
191 | }
192 | } else {
193 | // close
194 | smoothCloseMenu();
195 | return false;
196 | }
197 | break;
198 | }
199 | return true;
200 | }
201 |
202 | public boolean isOpen() {
203 | return state == STATE_OPEN;
204 | }
205 |
206 | @Override
207 | public boolean onTouchEvent(MotionEvent event) {
208 | return super.onTouchEvent(event);
209 | }
210 |
211 | private void swipe(int dis) {
212 | if(!mSwipEnable){
213 | return ;
214 | }
215 | if (Math.signum(dis) != mSwipeDirection) {
216 | dis = 0;
217 | } else if (Math.abs(dis) > mMenuView.getWidth()) {
218 | dis = mMenuView.getWidth()*mSwipeDirection;
219 | }
220 |
221 | mContentView.layout(-dis, mContentView.getTop(),
222 | mContentView.getWidth() -dis, getMeasuredHeight());
223 |
224 | if (mSwipeDirection == SwipeMenuListView.DIRECTION_LEFT) {
225 |
226 | mMenuView.layout(mContentView.getWidth() - dis, mMenuView.getTop(),
227 | mContentView.getWidth() + mMenuView.getWidth() - dis,
228 | mMenuView.getBottom());
229 | } else {
230 | mMenuView.layout(-mMenuView.getWidth() - dis, mMenuView.getTop(),
231 | - dis, mMenuView.getBottom());
232 | }
233 | }
234 |
235 | @Override
236 | public void computeScroll() {
237 | if (state == STATE_OPEN) {
238 | if (mOpenScroller.computeScrollOffset()) {
239 | swipe(mOpenScroller.getCurrX()*mSwipeDirection);
240 | postInvalidate();
241 | }
242 | } else {
243 | if (mCloseScroller.computeScrollOffset()) {
244 | swipe((mBaseX - mCloseScroller.getCurrX())*mSwipeDirection);
245 | postInvalidate();
246 | }
247 | }
248 | }
249 |
250 | public void smoothCloseMenu() {
251 | state = STATE_CLOSE;
252 | if (mSwipeDirection == SwipeMenuListView.DIRECTION_LEFT) {
253 | mBaseX = -mContentView.getLeft();
254 | mCloseScroller.startScroll(0, 0, mMenuView.getWidth(), 0, 350);
255 | } else {
256 | mBaseX = mMenuView.getRight();
257 | mCloseScroller.startScroll(0, 0, mMenuView.getWidth(), 0, 350);
258 | }
259 | postInvalidate();
260 | }
261 |
262 | public void smoothOpenMenu() {
263 | if(!mSwipEnable){
264 | return ;
265 | }
266 | state = STATE_OPEN;
267 | if (mSwipeDirection == SwipeMenuListView.DIRECTION_LEFT) {
268 | mOpenScroller.startScroll(-mContentView.getLeft(), 0, mMenuView.getWidth(), 0, 350);
269 | } else {
270 | mOpenScroller.startScroll(mContentView.getLeft(), 0, mMenuView.getWidth(), 0, 350);
271 | }
272 | postInvalidate();
273 | }
274 |
275 | public void closeMenu() {
276 | if (mCloseScroller.computeScrollOffset()) {
277 | mCloseScroller.abortAnimation();
278 | }
279 | if (state == STATE_OPEN) {
280 | state = STATE_CLOSE;
281 | swipe(0);
282 | }
283 | }
284 |
285 | public void openMenu() {
286 | if(!mSwipEnable){
287 | return ;
288 | }
289 | if (state == STATE_CLOSE) {
290 | state = STATE_OPEN;
291 | swipe(mMenuView.getWidth() * mSwipeDirection);
292 | }
293 | }
294 |
295 | public View getContentView() {
296 | return mContentView;
297 | }
298 |
299 | public SwipeMenuView getMenuView() {
300 | return mMenuView;
301 | }
302 |
303 | private int dp2px(int dp) {
304 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
305 | getContext().getResources().getDisplayMetrics());
306 | }
307 |
308 | @Override
309 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
310 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
311 | mMenuView.measure(MeasureSpec.makeMeasureSpec(0,
312 | MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(
313 | getMeasuredHeight(), MeasureSpec.EXACTLY));
314 | }
315 |
316 | @Override
317 | protected void onLayout(boolean changed, int l, int t, int r, int b) {
318 | mContentView.layout(0, 0, getMeasuredWidth(),
319 | mContentView.getMeasuredHeight());
320 | if (mSwipeDirection == SwipeMenuListView.DIRECTION_LEFT) {
321 | mMenuView.layout(getMeasuredWidth(), 0,
322 | getMeasuredWidth() + mMenuView.getMeasuredWidth(),
323 | mContentView.getMeasuredHeight());
324 | } else {
325 | mMenuView.layout(-mMenuView.getMeasuredWidth(), 0,
326 | 0, mContentView.getMeasuredHeight());
327 | }
328 | }
329 |
330 | public void setMenuHeight(int measuredHeight) {
331 | Log.i("byz", "pos = " + position + ", height = " + measuredHeight);
332 | LayoutParams params = (LayoutParams) mMenuView.getLayoutParams();
333 | if (params.height != measuredHeight) {
334 | params.height = measuredHeight;
335 | mMenuView.setLayoutParams(mMenuView.getLayoutParams());
336 | }
337 | }
338 |
339 | public void setSwipEnable(boolean swipEnable){
340 | mSwipEnable = swipEnable;
341 | }
342 |
343 | public boolean getSwipEnable(){
344 | return mSwipEnable;
345 | }
346 | }
347 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 |
42 | "Derivative Works" shall mean any work, whether in Source or Object
43 | form, that is based on (or derived from) the Work and for which the
44 | editorial revisions, annotations, elaborations, or other modifications
45 | represent, as a whole, an original work of authorship. For the purposes
46 | of this License, Derivative Works shall not include works that remain
47 | separable from, or merely link (or bind by name) to the interfaces of,
48 | the Work and Derivative Works thereof.
49 |
50 | "Contribution" shall mean any work of authorship, including
51 | the original version of the Work and any modifications or additions
52 | to that Work or Derivative Works thereof, that is intentionally
53 | submitted to Licensor for inclusion in the Work by the copyright owner
54 | or by an individual or Legal Entity authorized to submit on behalf of
55 | the copyright owner. For the purposes of this definition, "submitted"
56 | means any form of electronic, verbal, or written communication sent
57 | to the Licensor or its representatives, including but not limited to
58 | communication on electronic mailing lists, source code control systems,
59 | and issue tracking systems that are managed by, or on behalf of, the
60 | Licensor for the purpose of discussing and improving the Work, but
61 | excluding communication that is conspicuously marked or otherwise
62 | designated in writing by the copyright owner as "Not a Contribution."
63 |
64 | "Contributor" shall mean Licensor and any individual or Legal Entity
65 | on behalf of whom a Contribution has been received by Licensor and
66 | subsequently incorporated within the Work.
67 |
68 | 2. Grant of Copyright License. Subject to the terms and conditions of
69 | this License, each Contributor hereby grants to You a perpetual,
70 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
71 | copyright license to reproduce, prepare Derivative Works of,
72 | publicly display, publicly perform, sublicense, and distribute the
73 | Work and such Derivative Works in Source or Object form.
74 |
75 | 3. Grant of Patent License. Subject to the terms and conditions of
76 | this License, each Contributor hereby grants to You a perpetual,
77 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
78 | (except as stated in this section) patent license to make, have made,
79 | use, offer to sell, sell, import, and otherwise transfer the Work,
80 | where such license applies only to those patent claims licensable
81 | by such Contributor that are necessarily infringed by their
82 | Contribution(s) alone or by combination of their Contribution(s)
83 | with the Work to which such Contribution(s) was submitted. If You
84 | institute patent litigation against any entity (including a
85 | cross-claim or counterclaim in a lawsuit) alleging that the Work
86 | or a Contribution incorporated within the Work constitutes direct
87 | or contributory patent infringement, then any patent licenses
88 | granted to You under this License for that Work shall terminate
89 | as of the date such litigation is filed.
90 |
91 | 4. Redistribution. You may reproduce and distribute copies of the
92 | Work or Derivative Works thereof in any medium, with or without
93 | modifications, and in Source or Object form, provided that You
94 | meet the following conditions:
95 |
96 | (a) You must give any other recipients of the Work or
97 | Derivative Works a copy of this License; and
98 |
99 | (b) You must cause any modified files to carry prominent notices
100 | stating that You changed the files; and
101 |
102 | (c) You must retain, in the Source form of any Derivative Works
103 | that You distribute, all copyright, patent, trademark, and
104 | attribution notices from the Source form of the Work,
105 | excluding those notices that do not pertain to any part of
106 | the Derivative Works; and
107 |
108 | (d) If the Work includes a "NOTICE" text file as part of its
109 | distribution, then any Derivative Works that You distribute must
110 | include a readable copy of the attribution notices contained
111 | within such NOTICE file, excluding those notices that do not
112 | pertain to any part of the Derivative Works, in at least one
113 | of the following places: within a NOTICE text file distributed
114 | as part of the Derivative Works; within the Source form or
115 | documentation, if provided along with the Derivative Works; or,
116 | within a display generated by the Derivative Works, if and
117 | wherever such third-party notices normally appear. The contents
118 | of the NOTICE file are for informational purposes only and
119 | do not modify the License. You may add Your own attribution
120 | notices within Derivative Works that You distribute, alongside
121 | or as an addendum to the NOTICE text from the Work, provided
122 | that such additional attribution notices cannot be construed
123 | as modifying the License.
124 |
125 | You may add Your own copyright statement to Your modifications and
126 | may provide additional or different license terms and conditions
127 | for use, reproduction, or distribution of Your modifications, or
128 | for any such Derivative Works as a whole, provided Your use,
129 | reproduction, and distribution of the Work otherwise complies with
130 | the conditions stated in this License.
131 |
132 | 5. Submission of Contributions. Unless You explicitly state otherwise,
133 | any Contribution intentionally submitted for inclusion in the Work
134 | by You to the Licensor shall be under the terms and conditions of
135 | this License, without any additional terms or conditions.
136 | Notwithstanding the above, nothing herein shall supersede or modify
137 | the terms of any separate license agreement you may have executed
138 | with Licensor regarding such Contributions.
139 |
140 | 6. Trademarks. This License does not grant permission to use the trade
141 | names, trademarks, service marks, or product names of the Licensor,
142 | except as required for reasonable and customary use in describing the
143 | origin of the Work and reproducing the content of the NOTICE file.
144 |
145 | 7. Disclaimer of Warranty. Unless required by applicable law or
146 | agreed to in writing, Licensor provides the Work (and each
147 | Contributor provides its Contributions) on an "AS IS" BASIS,
148 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
149 | implied, including, without limitation, any warranties or conditions
150 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
151 | PARTICULAR PURPOSE. You are solely responsible for determining the
152 | appropriateness of using or redistributing the Work and assume any
153 | risks associated with Your exercise of permissions under this License.
154 |
155 | 8. Limitation of Liability. In no event and under no legal theory,
156 | whether in tort (including negligence), contract, or otherwise,
157 | unless required by applicable law (such as deliberate and grossly
158 | negligent acts) or agreed to in writing, shall any Contributor be
159 | liable to You for damages, including any direct, indirect, special,
160 | incidental, or consequential damages of any character arising as a
161 | result of this License or out of the use or inability to use the
162 | Work (including but not limited to damages for loss of goodwill,
163 | work stoppage, computer failure or malfunction, or any and all
164 | other commercial damages or losses), even if such Contributor
165 | has been advised of the possibility of such damages.
166 |
167 | 9. Accepting Warranty or Additional Liability. While redistributing
168 | the Work or Derivative Works thereof, You may choose to offer,
169 | and charge a fee for, acceptance of support, warranty, indemnity,
170 | or other liability obligations and/or rights consistent with this
171 | License. However, in accepting such obligations, You may act only
172 | on Your own behalf and on Your sole responsibility, not on behalf
173 | of any other Contributor, and only if You agree to indemnify,
174 | defend, and hold each Contributor harmless for any liability
175 | incurred by, or claims asserted against, such Contributor by reason
176 | of your accepting any such warranty or additional liability.
177 |
178 | END OF TERMS AND CONDITIONS
179 |
180 | APPENDIX: How to apply the Apache License to your work.
181 |
182 | To apply the Apache License to your work, attach the following
183 | boilerplate notice, with the fields enclosed by brackets "[]"
184 | replaced with your own identifying information. (Don't include
185 | the brackets!) The text should be enclosed in the appropriate
186 | comment syntax for the file format. We also recommend that a
187 | file or class name and description of purpose be included on the
188 | same "printed page" as the copyright notice for easier
189 | identification within third-party archives.
190 |
191 | Copyright [yyyy] [name of copyright owner]
192 |
193 | Licensed under the Apache License, Version 2.0 (the "License");
194 | you may not use this file except in compliance with the License.
195 | You may obtain a copy of the License at
196 |
197 | http://www.apache.org/licenses/LICENSE-2.0
198 |
199 | Unless required by applicable law or agreed to in writing, software
200 | distributed under the License is distributed on an "AS IS" BASIS,
201 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
202 | See the License for the specific language governing permissions and
203 | limitations under the License.
204 |
--------------------------------------------------------------------------------
/library/LICENSE.txt:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright [yyyy] [name of copyright owner]
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
203 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/PullToRefreshSwipeListView.java:
--------------------------------------------------------------------------------
1 | package com.yinglan.swiperefresh;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.graphics.Canvas;
6 | import android.util.AttributeSet;
7 | import android.view.Gravity;
8 | import android.view.MotionEvent;
9 | import android.view.View;
10 | import android.widget.FrameLayout;
11 | import android.widget.ListAdapter;
12 |
13 | import com.yinglan.swiperefresh.internal.EmptyViewMethodAccessor;
14 | import com.yinglan.swiperefresh.internal.LoadingLayout;
15 |
16 |
17 | public class PullToRefreshSwipeListView extends PullToRefreshAdapterViewBase{
18 | private LoadingLayout mHeaderLoadingView;
19 | private LoadingLayout mFooterLoadingView;
20 |
21 | private FrameLayout mLvFooterLoadingFrame;
22 |
23 | private boolean mListViewExtrasEnabled;
24 |
25 | public PullToRefreshSwipeListView(Context context) {
26 | super(context);
27 | }
28 |
29 | public PullToRefreshSwipeListView(Context context, AttributeSet attrs) {
30 | super(context, attrs);
31 | }
32 |
33 | public PullToRefreshSwipeListView(Context context, Mode mode) {
34 | super(context, mode);
35 | }
36 |
37 | public PullToRefreshSwipeListView(Context context, Mode mode, AnimationStyle style) {
38 | super(context, mode, style);
39 | }
40 |
41 | @Override
42 | public final Orientation getPullToRefreshScrollDirection() {
43 | return Orientation.VERTICAL;
44 | }
45 |
46 | @Override
47 | protected void onRefreshing(final boolean doScroll) {
48 | SwipeMenuListView listView = getRefreshableView();
49 | listView.smoothCloseMenu();
50 | /**
51 | * If we're not showing the Refreshing view, or the list is empty, the
52 | * the header/footer views won't show so we use the normal method.
53 | */
54 | ListAdapter adapter = mRefreshableView.getAdapter();
55 | if (!mListViewExtrasEnabled || !getShowViewWhileRefreshing() || null == adapter || adapter.isEmpty()) {
56 | super.onRefreshing(doScroll);
57 | return;
58 | }
59 |
60 | super.onRefreshing(false);
61 |
62 | final LoadingLayout origLoadingView, listViewLoadingView, oppositeListViewLoadingView;
63 | final int selection, scrollToY;
64 |
65 | switch (getCurrentMode()) {
66 | case MANUAL_REFRESH_ONLY:
67 | case PULL_FROM_END:
68 | origLoadingView = getFooterLayout();
69 | listViewLoadingView = mFooterLoadingView;
70 | oppositeListViewLoadingView = mHeaderLoadingView;
71 | selection = mRefreshableView.getCount() - 1;
72 | scrollToY = getScrollY() - getFooterSize();
73 | break;
74 | case PULL_FROM_START:
75 | default:
76 | origLoadingView = getHeaderLayout();
77 | listViewLoadingView = mHeaderLoadingView;
78 | oppositeListViewLoadingView = mFooterLoadingView;
79 | selection = 0;
80 | scrollToY = getScrollY() + getHeaderSize();
81 | break;
82 | }
83 |
84 | // Hide our original Loading View
85 | origLoadingView.reset();
86 | origLoadingView.hideAllViews();
87 |
88 | // Make sure the opposite end is hidden too
89 | oppositeListViewLoadingView.setVisibility(View.GONE);
90 |
91 | // Show the ListView Loading View and set it to refresh.
92 | listViewLoadingView.setVisibility(View.VISIBLE);
93 | listViewLoadingView.refreshing();
94 |
95 | if (doScroll) {
96 | // We need to disable the automatic visibility changes for now
97 | disableLoadingLayoutVisibilityChanges();
98 |
99 | // We scroll slightly so that the ListView's header/footer is at the
100 | // same Y position as our normal header/footer
101 | setHeaderScroll(scrollToY);
102 |
103 | // Make sure the ListView is scrolled to show the loading
104 | // header/footer
105 | mRefreshableView.setSelection(selection);
106 |
107 | // Smooth scroll as normal
108 | smoothScrollTo(0);
109 | }
110 | }
111 |
112 | @Override
113 | protected void onReset() {
114 | /**
115 | * If the extras are not enabled, just call up to super and return.
116 | */
117 | if (!mListViewExtrasEnabled) {
118 | super.onReset();
119 | return;
120 | }
121 |
122 | final LoadingLayout originalLoadingLayout, listViewLoadingLayout;
123 | final int scrollToHeight, selection;
124 | final boolean scrollLvToEdge;
125 |
126 | switch (getCurrentMode()) {
127 | case MANUAL_REFRESH_ONLY:
128 | case PULL_FROM_END:
129 | originalLoadingLayout = getFooterLayout();
130 | listViewLoadingLayout = mFooterLoadingView;
131 | selection = mRefreshableView.getCount() - 1;
132 | scrollToHeight = getFooterSize();
133 | scrollLvToEdge = Math.abs(mRefreshableView.getLastVisiblePosition() - selection) <= 1;
134 | break;
135 | case PULL_FROM_START:
136 | default:
137 | originalLoadingLayout = getHeaderLayout();
138 | listViewLoadingLayout = mHeaderLoadingView;
139 | scrollToHeight = -getHeaderSize();
140 | selection = 0;
141 | scrollLvToEdge = Math.abs(mRefreshableView.getFirstVisiblePosition() - selection) <= 1;
142 | break;
143 | }
144 |
145 | // If the ListView header loading layout is showing, then we need to
146 | // flip so that the original one is showing instead
147 | if (listViewLoadingLayout.getVisibility() == View.VISIBLE) {
148 |
149 | // Set our Original View to Visible
150 | originalLoadingLayout.showInvisibleViews();
151 |
152 | // Hide the ListView Header/Footer
153 | listViewLoadingLayout.setVisibility(View.GONE);
154 |
155 | /**
156 | * Scroll so the View is at the same Y as the ListView
157 | * header/footer, but only scroll if: we've pulled to refresh, it's
158 | * positioned correctly
159 | */
160 | if (scrollLvToEdge && getState() != State.MANUAL_REFRESHING) {
161 | mRefreshableView.setSelection(selection);
162 | setHeaderScroll(scrollToHeight);
163 | }
164 | }
165 |
166 | // Finally, call up to super
167 | super.onReset();
168 | }
169 |
170 | @Override
171 | protected LoadingLayoutProxy createLoadingLayoutProxy(final boolean includeStart, final boolean includeEnd) {
172 | LoadingLayoutProxy proxy = super.createLoadingLayoutProxy(includeStart, includeEnd);
173 |
174 | if (mListViewExtrasEnabled) {
175 | final Mode mode = getMode();
176 |
177 | if (includeStart && mode.showHeaderLoadingLayout()) {
178 | proxy.addLayout(mHeaderLoadingView);
179 | }
180 | if (includeEnd && mode.showFooterLoadingLayout()) {
181 | proxy.addLayout(mFooterLoadingView);
182 | }
183 | }
184 |
185 | return proxy;
186 | }
187 |
188 | protected SwipeMenuListView createListView(Context context, AttributeSet attrs) {
189 | final SwipeMenuListView lv;
190 | lv = new PullToRefreshSwipeListView.InternalListView(context, attrs);
191 | return lv;
192 | }
193 |
194 | @Override
195 | protected SwipeMenuListView createRefreshableView(Context context, AttributeSet attrs) {
196 | SwipeMenuListView lv = createListView(context, attrs);
197 |
198 | // Set it to this so it can be used in ListActivity/ListFragment
199 | lv.setId(android.R.id.list);
200 | return lv;
201 | }
202 |
203 | @Override
204 | protected void handleStyledAttributes(TypedArray a) {
205 | super.handleStyledAttributes(a);
206 |
207 | mListViewExtrasEnabled = a.getBoolean(R.styleable.PullToRefresh_ptrListViewExtrasEnabled, true);
208 |
209 | if (mListViewExtrasEnabled) {
210 | final FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
211 | FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
212 |
213 | // Create Loading Views ready for use later
214 | FrameLayout frame = new FrameLayout(getContext());
215 | mHeaderLoadingView = createLoadingLayout(getContext(), Mode.PULL_FROM_START, a);
216 | mHeaderLoadingView.setVisibility(View.GONE);
217 | frame.addView(mHeaderLoadingView, lp);
218 | mRefreshableView.addHeaderView(frame, null, false);
219 |
220 | mLvFooterLoadingFrame = new FrameLayout(getContext());
221 | mFooterLoadingView = createLoadingLayout(getContext(), Mode.PULL_FROM_END, a);
222 | mFooterLoadingView.setVisibility(View.GONE);
223 | mLvFooterLoadingFrame.addView(mFooterLoadingView, lp);
224 |
225 | /**
226 | * If the value for Scrolling While Refreshing hasn't been
227 | * explicitly set via XML, enable Scrolling While Refreshing.
228 | */
229 | if (!a.hasValue(R.styleable.PullToRefresh_ptrScrollingWhileRefreshingEnabled)) {
230 | setScrollingWhileRefreshingEnabled(true);
231 | }
232 | }
233 | }
234 |
235 | protected class InternalListView extends SwipeMenuListView implements EmptyViewMethodAccessor {
236 |
237 | private boolean mAddedLvFooter = false;
238 |
239 | public InternalListView(Context context, AttributeSet attrs) {
240 | super(context, attrs);
241 | }
242 |
243 | @Override
244 | protected void dispatchDraw(Canvas canvas) {
245 | /**
246 | * This is a bit hacky, but Samsung's ListView has got a bug in it
247 | * when using Header/Footer Views and the list is empty. This masks
248 | * the issue so that it doesn't cause an FC. See Issue #66.
249 | */
250 | try {
251 | super.dispatchDraw(canvas);
252 | } catch (IndexOutOfBoundsException e) {
253 | e.printStackTrace();
254 | }
255 | }
256 |
257 | @Override
258 | public boolean dispatchTouchEvent(MotionEvent ev) {
259 | /**
260 | * This is a bit hacky, but Samsung's ListView has got a bug in it
261 | * when using Header/Footer Views and the list is empty. This masks
262 | * the issue so that it doesn't cause an FC. See Issue #66.
263 | */
264 | try {
265 | return super.dispatchTouchEvent(ev);
266 | } catch (IndexOutOfBoundsException e) {
267 | e.printStackTrace();
268 | return false;
269 | }
270 | }
271 |
272 | @Override
273 | public void setAdapter(ListAdapter adapter) {
274 | // Add the Footer View at the last possible moment
275 | if (null != mLvFooterLoadingFrame && !mAddedLvFooter) {
276 | addFooterView(mLvFooterLoadingFrame, null, false);
277 | mAddedLvFooter = true;
278 | }
279 |
280 | super.setAdapter(adapter);
281 | }
282 |
283 | @Override
284 | public void setEmptyView(View emptyView) {
285 | PullToRefreshSwipeListView.this.setEmptyView(emptyView);
286 | }
287 |
288 | @Override
289 | public void setEmptyViewInternal(View emptyView) {
290 | super.setEmptyView(emptyView);
291 | }
292 |
293 | }
294 | }
295 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yinglan/swiperefresh/PullToRefreshListView.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011, 2012 Chris Banes.
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 | package com.yinglan.swiperefresh;
17 |
18 | import android.content.Context;
19 | import android.content.res.TypedArray;
20 | import android.graphics.Canvas;
21 | import android.util.AttributeSet;
22 | import android.view.Gravity;
23 | import android.view.MotionEvent;
24 | import android.view.View;
25 | import android.widget.FrameLayout;
26 | import android.widget.ListAdapter;
27 | import android.widget.ListView;
28 |
29 | import com.yinglan.swiperefresh.internal.EmptyViewMethodAccessor;
30 | import com.yinglan.swiperefresh.internal.LoadingLayout;
31 |
32 | public class PullToRefreshListView extends PullToRefreshAdapterViewBase {
33 |
34 | private LoadingLayout mHeaderLoadingView;
35 | private LoadingLayout mFooterLoadingView;
36 |
37 | private FrameLayout mLvFooterLoadingFrame;
38 |
39 | private boolean mListViewExtrasEnabled;
40 |
41 | public PullToRefreshListView(Context context) {
42 | super(context);
43 | }
44 |
45 | public PullToRefreshListView(Context context, AttributeSet attrs) {
46 | super(context, attrs);
47 | }
48 |
49 | public PullToRefreshListView(Context context, Mode mode) {
50 | super(context, mode);
51 | }
52 |
53 | public PullToRefreshListView(Context context, Mode mode, AnimationStyle style) {
54 | super(context, mode, style);
55 | }
56 |
57 | @Override
58 | public final Orientation getPullToRefreshScrollDirection() {
59 | return Orientation.VERTICAL;
60 | }
61 |
62 | @Override
63 | protected void onRefreshing(final boolean doScroll) {
64 | /**
65 | * If we're not showing the Refreshing view, or the list is empty, the
66 | * the header/footer views won't show so we use the normal method.
67 | */
68 | ListAdapter adapter = mRefreshableView.getAdapter();
69 | if (!mListViewExtrasEnabled || !getShowViewWhileRefreshing() || null == adapter || adapter.isEmpty()) {
70 | super.onRefreshing(doScroll);
71 | return;
72 | }
73 |
74 | super.onRefreshing(false);
75 |
76 | final LoadingLayout origLoadingView, listViewLoadingView, oppositeListViewLoadingView;
77 | final int selection, scrollToY;
78 |
79 | switch (getCurrentMode()) {
80 | case MANUAL_REFRESH_ONLY:
81 | case PULL_FROM_END:
82 | origLoadingView = getFooterLayout();
83 | listViewLoadingView = mFooterLoadingView;
84 | oppositeListViewLoadingView = mHeaderLoadingView;
85 | selection = mRefreshableView.getCount() - 1;
86 | scrollToY = getScrollY() - getFooterSize();
87 | break;
88 | case PULL_FROM_START:
89 | default:
90 | origLoadingView = getHeaderLayout();
91 | listViewLoadingView = mHeaderLoadingView;
92 | oppositeListViewLoadingView = mFooterLoadingView;
93 | selection = 0;
94 | scrollToY = getScrollY() + getHeaderSize();
95 | break;
96 | }
97 |
98 | // Hide our original Loading View
99 | origLoadingView.reset();
100 | origLoadingView.hideAllViews();
101 |
102 | // Make sure the opposite end is hidden too
103 | oppositeListViewLoadingView.setVisibility(View.GONE);
104 |
105 | // Show the ListView Loading View and set it to refresh.
106 | listViewLoadingView.setVisibility(View.VISIBLE);
107 | listViewLoadingView.refreshing();
108 |
109 | if (doScroll) {
110 | // We need to disable the automatic visibility changes for now
111 | disableLoadingLayoutVisibilityChanges();
112 |
113 | // We scroll slightly so that the ListView's header/footer is at the
114 | // same Y position as our normal header/footer
115 | setHeaderScroll(scrollToY);
116 |
117 | // Make sure the ListView is scrolled to show the loading
118 | // header/footer
119 | mRefreshableView.setSelection(selection);
120 |
121 | // Smooth scroll as normal
122 | smoothScrollTo(0);
123 | }
124 | }
125 |
126 | @Override
127 | protected void onReset() {
128 | /**
129 | * If the extras are not enabled, just call up to super and return.
130 | */
131 | if (!mListViewExtrasEnabled) {
132 | super.onReset();
133 | return;
134 | }
135 |
136 | final LoadingLayout originalLoadingLayout, listViewLoadingLayout;
137 | final int scrollToHeight, selection;
138 | final boolean scrollLvToEdge;
139 |
140 | switch (getCurrentMode()) {
141 | case MANUAL_REFRESH_ONLY:
142 | case PULL_FROM_END:
143 | originalLoadingLayout = getFooterLayout();
144 | listViewLoadingLayout = mFooterLoadingView;
145 | selection = mRefreshableView.getCount() - 1;
146 | scrollToHeight = getFooterSize();
147 | scrollLvToEdge = Math.abs(mRefreshableView.getLastVisiblePosition() - selection) <= 1;
148 | break;
149 | case PULL_FROM_START:
150 | default:
151 | originalLoadingLayout = getHeaderLayout();
152 | listViewLoadingLayout = mHeaderLoadingView;
153 | scrollToHeight = -getHeaderSize();
154 | selection = 0;
155 | scrollLvToEdge = Math.abs(mRefreshableView.getFirstVisiblePosition() - selection) <= 1;
156 | break;
157 | }
158 |
159 | // If the ListView header loading layout is showing, then we need to
160 | // flip so that the original one is showing instead
161 | if (listViewLoadingLayout.getVisibility() == View.VISIBLE) {
162 |
163 | // Set our Original View to Visible
164 | originalLoadingLayout.showInvisibleViews();
165 |
166 | // Hide the ListView Header/Footer
167 | listViewLoadingLayout.setVisibility(View.GONE);
168 |
169 | /**
170 | * Scroll so the View is at the same Y as the ListView
171 | * header/footer, but only scroll if: we've pulled to refresh, it's
172 | * positioned correctly
173 | */
174 | if (scrollLvToEdge && getState() != State.MANUAL_REFRESHING) {
175 | mRefreshableView.setSelection(selection);
176 | setHeaderScroll(scrollToHeight);
177 | }
178 | }
179 |
180 | // Finally, call up to super
181 | super.onReset();
182 | }
183 |
184 | @Override
185 | protected LoadingLayoutProxy createLoadingLayoutProxy(final boolean includeStart, final boolean includeEnd) {
186 | LoadingLayoutProxy proxy = super.createLoadingLayoutProxy(includeStart, includeEnd);
187 |
188 | if (mListViewExtrasEnabled) {
189 | final Mode mode = getMode();
190 |
191 | if (includeStart && mode.showHeaderLoadingLayout()) {
192 | proxy.addLayout(mHeaderLoadingView);
193 | }
194 | if (includeEnd && mode.showFooterLoadingLayout()) {
195 | proxy.addLayout(mFooterLoadingView);
196 | }
197 | }
198 |
199 | return proxy;
200 | }
201 |
202 | protected ListView createListView(Context context, AttributeSet attrs) {
203 | final ListView lv;
204 | lv = new InternalListView(context, attrs);
205 | return lv;
206 | }
207 |
208 | @Override
209 | protected ListView createRefreshableView(Context context, AttributeSet attrs) {
210 | ListView lv = createListView(context, attrs);
211 |
212 | // Set it to this so it can be used in ListActivity/ListFragment
213 | lv.setId(android.R.id.list);
214 | return lv;
215 | }
216 |
217 | @Override
218 | protected void handleStyledAttributes(TypedArray a) {
219 | super.handleStyledAttributes(a);
220 |
221 | mListViewExtrasEnabled = a.getBoolean(R.styleable.PullToRefresh_ptrListViewExtrasEnabled, true);
222 |
223 | if (mListViewExtrasEnabled) {
224 | final FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
225 | FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
226 |
227 | // Create Loading Views ready for use later
228 | FrameLayout frame = new FrameLayout(getContext());
229 | mHeaderLoadingView = createLoadingLayout(getContext(), Mode.PULL_FROM_START, a);
230 | mHeaderLoadingView.setVisibility(View.GONE);
231 | frame.addView(mHeaderLoadingView, lp);
232 | mRefreshableView.addHeaderView(frame, null, false);
233 |
234 | mLvFooterLoadingFrame = new FrameLayout(getContext());
235 | mFooterLoadingView = createLoadingLayout(getContext(), Mode.PULL_FROM_END, a);
236 | mFooterLoadingView.setVisibility(View.GONE);
237 | mLvFooterLoadingFrame.addView(mFooterLoadingView, lp);
238 |
239 | /**
240 | * If the value for Scrolling While Refreshing hasn't been
241 | * explicitly set via XML, enable Scrolling While Refreshing.
242 | */
243 | if (!a.hasValue(R.styleable.PullToRefresh_ptrScrollingWhileRefreshingEnabled)) {
244 | setScrollingWhileRefreshingEnabled(true);
245 | }
246 | }
247 | }
248 |
249 | protected class InternalListView extends ListView implements EmptyViewMethodAccessor {
250 |
251 | private boolean mAddedLvFooter = false;
252 |
253 | public InternalListView(Context context, AttributeSet attrs) {
254 | super(context, attrs);
255 | }
256 |
257 | @Override
258 | protected void dispatchDraw(Canvas canvas) {
259 | /**
260 | * This is a bit hacky, but Samsung's ListView has got a bug in it
261 | * when using Header/Footer Views and the list is empty. This masks
262 | * the issue so that it doesn't cause an FC. See Issue #66.
263 | */
264 | try {
265 | super.dispatchDraw(canvas);
266 | } catch (IndexOutOfBoundsException e) {
267 | e.printStackTrace();
268 | }
269 | }
270 |
271 | @Override
272 | public boolean dispatchTouchEvent(MotionEvent ev) {
273 | /**
274 | * This is a bit hacky, but Samsung's ListView has got a bug in it
275 | * when using Header/Footer Views and the list is empty. This masks
276 | * the issue so that it doesn't cause an FC. See Issue #66.
277 | */
278 | try {
279 | return super.dispatchTouchEvent(ev);
280 | } catch (IndexOutOfBoundsException e) {
281 | e.printStackTrace();
282 | return false;
283 | }
284 | }
285 |
286 | @Override
287 | public void setAdapter(ListAdapter adapter) {
288 | // Add the Footer View at the last possible moment
289 | if (null != mLvFooterLoadingFrame && !mAddedLvFooter) {
290 | addFooterView(mLvFooterLoadingFrame, null, false);
291 | mAddedLvFooter = true;
292 | }
293 |
294 | super.setAdapter(adapter);
295 | }
296 |
297 | @Override
298 | public void setEmptyView(View emptyView) {
299 | PullToRefreshListView.this.setEmptyView(emptyView);
300 | }
301 |
302 | @Override
303 | public void setEmptyViewInternal(View emptyView) {
304 | super.setEmptyView(emptyView);
305 | }
306 |
307 | }
308 |
309 | }
310 |
--------------------------------------------------------------------------------