├── .gitignore ├── LICENSE ├── PullScrollView ├── .gitignore ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ ├── ic_launcher-web.png │ ├── java │ └── com │ │ └── markmao │ │ └── pullscrollview │ │ └── ui │ │ ├── MainActivity.java │ │ ├── PulldownViewActivity.java │ │ ├── StretchViewActivity.java │ │ └── widget │ │ ├── PullScrollView.java │ │ └── StretchScrollView.java │ └── res │ ├── drawable-hdpi │ ├── add.png │ ├── arrow_up.png │ ├── avatar_default.png │ ├── button_bg_normal.9.png │ ├── button_bg_pressed.9.png │ ├── card_avatar_bar.9.png │ ├── card_whole.9.png │ ├── ic_launcher.png │ └── scrollview_header.jpg │ ├── drawable-mdpi │ └── ic_launcher.png │ ├── drawable-xhdpi │ └── ic_launcher.png │ ├── drawable-xxhdpi │ └── ic_launcher.png │ ├── drawable │ └── selector_btn.xml │ ├── layout │ ├── act_main.xml │ ├── act_pull_down.xml │ └── act_stretch.xml │ ├── values-v11 │ └── styles.xml │ ├── values-v14 │ └── styles.xml │ └── values │ ├── attrs.xml │ ├── color.xml │ ├── strings.xml │ └── styles.xml ├── README.md ├── Screenshots ├── 0.png ├── 1.png └── 2.png ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # files for the dex VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # generated files 12 | bin/ 13 | gen/ 14 | 15 | # Local configuration file (sdk path, etc) 16 | local.properties 17 | 18 | # Eclipse project files 19 | .classpath 20 | .project 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Intellij project files 26 | *.iml 27 | *.ipr 28 | *.iws 29 | .idea/ 30 | /.gradle/ 31 | 32 | /build 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, and 10 | distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by the copyright 13 | owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all other entities 16 | that control, are controlled by, or are under common control with that entity. 17 | For the purposes of this definition, "control" means (i) the power, direct or 18 | indirect, to cause the direction or management of such entity, whether by 19 | contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the 20 | outstanding shares, or (iii) beneficial ownership of such entity. 21 | 22 | "You" (or "Your") shall mean an individual or Legal Entity exercising 23 | permissions granted by this License. 24 | 25 | "Source" form shall mean the preferred form for making modifications, including 26 | but not limited to software source code, documentation source, and configuration 27 | files. 28 | 29 | "Object" form shall mean any form resulting from mechanical transformation or 30 | translation of a Source form, including but not limited to compiled object code, 31 | generated documentation, and conversions to other media types. 32 | 33 | "Work" shall mean the work of authorship, whether in Source or Object form, made 34 | available under the License, as indicated by a copyright notice that is included 35 | in or attached to the work (an example is provided in the Appendix below). 36 | 37 | "Derivative Works" shall mean any work, whether in Source or Object form, that 38 | is based on (or derived from) the Work and for which the editorial revisions, 39 | annotations, elaborations, or other modifications represent, as a whole, an 40 | original work of authorship. For the purposes of this License, Derivative Works 41 | shall not include works that remain separable from, or merely link (or bind by 42 | name) to the interfaces of, the Work and Derivative Works thereof. 43 | 44 | "Contribution" shall mean any work of authorship, including the original version 45 | of the Work and any modifications or additions to that Work or Derivative Works 46 | thereof, that is intentionally submitted to Licensor for inclusion in the Work 47 | by the copyright owner or by an individual or Legal Entity authorized to submit 48 | on behalf of the copyright owner. For the purposes of this definition, 49 | "submitted" means any form of electronic, verbal, or written communication sent 50 | to the Licensor or its representatives, including but not limited to 51 | communication on electronic mailing lists, source code control systems, and 52 | issue tracking systems that are managed by, or on behalf of, the Licensor for 53 | the purpose of discussing and improving the Work, but excluding communication 54 | that is conspicuously marked or otherwise designated in writing by the copyright 55 | owner as "Not a Contribution." 56 | 57 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf 58 | of whom a Contribution has been received by Licensor and subsequently 59 | incorporated within the Work. 60 | 61 | 2. Grant of Copyright License. 62 | 63 | Subject to the terms and conditions of this License, each Contributor hereby 64 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 65 | irrevocable copyright license to reproduce, prepare Derivative Works of, 66 | publicly display, publicly perform, sublicense, and distribute the Work and such 67 | Derivative Works in Source or Object form. 68 | 69 | 3. Grant of Patent License. 70 | 71 | Subject to the terms and conditions of this License, each Contributor hereby 72 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 73 | irrevocable (except as stated in this section) patent license to make, have 74 | made, use, offer to sell, sell, import, and otherwise transfer the Work, where 75 | such license applies only to those patent claims licensable by such Contributor 76 | that are necessarily infringed by their Contribution(s) alone or by combination 77 | of their Contribution(s) with the Work to which such Contribution(s) was 78 | submitted. If You institute patent litigation against any entity (including a 79 | cross-claim or counterclaim in a lawsuit) alleging that the Work or a 80 | Contribution incorporated within the Work constitutes direct or contributory 81 | patent infringement, then any patent licenses granted to You under this License 82 | for that Work shall terminate as of the date such litigation is filed. 83 | 84 | 4. Redistribution. 85 | 86 | You may reproduce and distribute copies of the Work or Derivative Works thereof 87 | in any medium, with or without modifications, and in Source or Object form, 88 | provided that You meet the following conditions: 89 | 90 | You must give any other recipients of the Work or Derivative Works a copy of 91 | this License; and 92 | You must cause any modified files to carry prominent notices stating that You 93 | changed the files; and 94 | You must retain, in the Source form of any Derivative Works that You distribute, 95 | all copyright, patent, trademark, and attribution notices from the Source form 96 | of the Work, excluding those notices that do not pertain to any part of the 97 | Derivative Works; and 98 | If the Work includes a "NOTICE" text file as part of its distribution, then any 99 | Derivative Works that You distribute must include a readable copy of the 100 | attribution notices contained within such NOTICE file, excluding those notices 101 | that do not pertain to any part of the Derivative Works, in at least one of the 102 | following places: within a NOTICE text file distributed as part of the 103 | Derivative Works; within the Source form or documentation, if provided along 104 | with the Derivative Works; or, within a display generated by the Derivative 105 | Works, if and wherever such third-party notices normally appear. The contents of 106 | the NOTICE file are for informational purposes only and do not modify the 107 | License. You may add Your own attribution notices within Derivative Works that 108 | You distribute, alongside or as an addendum to the NOTICE text from the Work, 109 | provided that such additional attribution notices cannot be construed as 110 | modifying the License. 111 | You may add Your own copyright statement to Your modifications and may provide 112 | additional or different license terms and conditions for use, reproduction, or 113 | distribution of Your modifications, or for any such Derivative Works as a whole, 114 | provided Your use, reproduction, and distribution of the Work otherwise complies 115 | with the conditions stated in this License. 116 | 117 | 5. Submission of Contributions. 118 | 119 | Unless You explicitly state otherwise, any Contribution intentionally submitted 120 | for inclusion in the Work by You to the Licensor shall be under the terms and 121 | conditions of this License, without any additional terms or conditions. 122 | Notwithstanding the above, nothing herein shall supersede or modify the terms of 123 | any separate license agreement you may have executed with Licensor regarding 124 | such Contributions. 125 | 126 | 6. Trademarks. 127 | 128 | This License does not grant permission to use the trade names, trademarks, 129 | service marks, or product names of the Licensor, except as required for 130 | reasonable and customary use in describing the origin of the Work and 131 | reproducing the content of the NOTICE file. 132 | 133 | 7. Disclaimer of Warranty. 134 | 135 | Unless required by applicable law or agreed to in writing, Licensor provides the 136 | Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, 137 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, 138 | including, without limitation, any warranties or conditions of TITLE, 139 | NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are 140 | solely responsible for determining the appropriateness of using or 141 | redistributing the Work and assume any risks associated with Your exercise of 142 | permissions under this License. 143 | 144 | 8. Limitation of Liability. 145 | 146 | In no event and under no legal theory, whether in tort (including negligence), 147 | contract, or otherwise, unless required by applicable law (such as deliberate 148 | and grossly negligent acts) or agreed to in writing, shall any Contributor be 149 | liable to You for damages, including any direct, indirect, special, incidental, 150 | or consequential damages of any character arising as a result of this License or 151 | out of the use or inability to use the Work (including but not limited to 152 | damages for loss of goodwill, work stoppage, computer failure or malfunction, or 153 | any and all other commercial damages or losses), even if such Contributor has 154 | been advised of the possibility of such damages. 155 | 156 | 9. Accepting Warranty or Additional Liability. 157 | 158 | While redistributing the Work or Derivative Works thereof, You may choose to 159 | offer, and charge a fee for, acceptance of support, warranty, indemnity, or 160 | other liability obligations and/or rights consistent with this License. However, 161 | in accepting such obligations, You may act only on Your own behalf and on Your 162 | sole responsibility, not on behalf of any other Contributor, and only if You 163 | agree to indemnify, defend, and hold each Contributor harmless for any liability 164 | incurred by, or claims asserted against, such Contributor by reason of your 165 | accepting any such warranty or additional liability. 166 | 167 | END OF TERMS AND CONDITIONS 168 | 169 | APPENDIX: How to apply the Apache License to your work 170 | 171 | To apply the Apache License to your work, attach the following boilerplate 172 | notice, with the fields enclosed by brackets "[]" replaced with your own 173 | identifying information. (Don't include the brackets!) The text should be 174 | enclosed in the appropriate comment syntax for the file format. We also 175 | recommend that a file or class name and description of purpose be included on 176 | the same "printed page" as the copyright notice for easier identification within 177 | third-party archives. 178 | 179 | Copyright [yyyy] [name of copyright owner] 180 | 181 | Licensed under the Apache License, Version 2.0 (the "License"); 182 | you may not use this file except in compliance with the License. 183 | You may obtain a copy of the License at 184 | 185 | http://www.apache.org/licenses/LICENSE-2.0 186 | 187 | Unless required by applicable law or agreed to in writing, software 188 | distributed under the License is distributed on an "AS IS" BASIS, 189 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 190 | See the License for the specific language governing permissions and 191 | limitations under the License. 192 | -------------------------------------------------------------------------------- /PullScrollView/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /PullScrollView/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 18 5 | buildToolsVersion "19.1" 6 | compileOptions.encoding = "UTF-8" 7 | 8 | defaultConfig { 9 | minSdkVersion 10 10 | targetSdkVersion 19 11 | } 12 | } 13 | dependencies { 14 | compile 'com.android.support:appcompat-v7:19.+' 15 | } 16 | 17 | 18 | -------------------------------------------------------------------------------- /PullScrollView/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 24 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /PullScrollView/src/main/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarkMjw/PullScrollView/0dbcbd2f4f17b4953f30a74d9481e1315e49e47e/PullScrollView/src/main/ic_launcher-web.png -------------------------------------------------------------------------------- /PullScrollView/src/main/java/com/markmao/pullscrollview/ui/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.markmao.pullscrollview.ui; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.view.View; 7 | 8 | import com.markmao.pullscrollview.R; 9 | 10 | /** 11 | * Demo 12 | * 13 | * @author markmjw 14 | * @date 2014-04-30 15 | */ 16 | public class MainActivity extends Activity { 17 | 18 | @Override 19 | protected void onCreate(Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | setContentView(R.layout.act_main); 22 | 23 | findViewById(R.id.pulldown_scrollview_btn).setOnClickListener(new View.OnClickListener() { 24 | @Override 25 | public void onClick(View v) { 26 | Intent intent = new Intent(MainActivity.this, PulldownViewActivity.class); 27 | startActivity(intent); 28 | } 29 | }); 30 | 31 | findViewById(R.id.stretch_scrollview_btn).setOnClickListener(new View.OnClickListener() { 32 | @Override 33 | public void onClick(View v) { 34 | Intent intent = new Intent(MainActivity.this, StretchViewActivity.class); 35 | startActivity(intent); 36 | } 37 | }); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /PullScrollView/src/main/java/com/markmao/pullscrollview/ui/PulldownViewActivity.java: -------------------------------------------------------------------------------- 1 | package com.markmao.pullscrollview.ui; 2 | 3 | import android.app.Activity; 4 | import android.graphics.Color; 5 | import android.os.Bundle; 6 | import android.view.Gravity; 7 | import android.view.View; 8 | import android.widget.ImageView; 9 | import android.widget.TableLayout; 10 | import android.widget.TableRow; 11 | import android.widget.TextView; 12 | import android.widget.Toast; 13 | 14 | import com.markmao.pullscrollview.R; 15 | import com.markmao.pullscrollview.ui.widget.PullScrollView; 16 | 17 | /** 18 | * Pull down ScrollView demo. 19 | * 20 | * @author markmjw 21 | * @date 2014-04-30 22 | */ 23 | public class PulldownViewActivity extends Activity implements PullScrollView.OnTurnListener { 24 | private PullScrollView mScrollView; 25 | private ImageView mHeadImg; 26 | 27 | private TableLayout mMainLayout; 28 | 29 | @Override 30 | protected void onCreate(Bundle savedInstanceState) { 31 | super.onCreate(savedInstanceState); 32 | setContentView(R.layout.act_pull_down); 33 | 34 | initView(); 35 | 36 | showTable(); 37 | } 38 | 39 | protected void initView() { 40 | mScrollView = (PullScrollView) findViewById(R.id.scroll_view); 41 | mHeadImg = (ImageView) findViewById(R.id.background_img); 42 | 43 | mMainLayout = (TableLayout) findViewById(R.id.table_layout); 44 | 45 | mScrollView.setHeader(mHeadImg); 46 | mScrollView.setOnTurnListener(this); 47 | } 48 | 49 | public void showTable() { 50 | TableRow.LayoutParams layoutParams = new TableRow.LayoutParams( 51 | TableRow.LayoutParams.MATCH_PARENT, 52 | TableRow.LayoutParams.WRAP_CONTENT); 53 | layoutParams.gravity = Gravity.CENTER; 54 | layoutParams.leftMargin = 30; 55 | layoutParams.bottomMargin = 10; 56 | layoutParams.topMargin = 10; 57 | 58 | for (int i = 0; i < 30; i++) { 59 | TableRow tableRow = new TableRow(this); 60 | TextView textView = new TextView(this); 61 | textView.setText("Test pull down scroll view " + i); 62 | textView.setTextSize(20); 63 | textView.setPadding(15, 15, 15, 15); 64 | 65 | tableRow.addView(textView, layoutParams); 66 | if (i % 2 != 0) { 67 | tableRow.setBackgroundColor(Color.LTGRAY); 68 | } else { 69 | tableRow.setBackgroundColor(Color.WHITE); 70 | } 71 | 72 | final int n = i; 73 | tableRow.setOnClickListener(new View.OnClickListener() { 74 | @Override 75 | public void onClick(View v) { 76 | Toast.makeText(PulldownViewActivity.this, "Click item " + n, Toast.LENGTH_SHORT).show(); 77 | } 78 | }); 79 | 80 | mMainLayout.addView(tableRow); 81 | } 82 | } 83 | 84 | @Override 85 | public void onTurn() { 86 | 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /PullScrollView/src/main/java/com/markmao/pullscrollview/ui/StretchViewActivity.java: -------------------------------------------------------------------------------- 1 | package com.markmao.pullscrollview.ui; 2 | 3 | import android.app.Activity; 4 | import android.graphics.Color; 5 | import android.os.Bundle; 6 | import android.view.Gravity; 7 | import android.view.View; 8 | import android.widget.TableLayout; 9 | import android.widget.TableRow; 10 | import android.widget.TextView; 11 | import android.widget.Toast; 12 | 13 | import com.markmao.pullscrollview.R; 14 | 15 | /** 16 | * Stretch ScrollView demo. 17 | * 18 | * @author markmjw 19 | * @date 2014-04-30 20 | */ 21 | public class StretchViewActivity extends Activity { 22 | private TableLayout mMainLayout; 23 | 24 | @Override 25 | protected void onCreate(Bundle savedInstanceState) { 26 | super.onCreate(savedInstanceState); 27 | setContentView(R.layout.act_stretch); 28 | 29 | mMainLayout = (TableLayout) findViewById(R.id.table_layout); 30 | showTable(); 31 | } 32 | 33 | public void showTable() { 34 | TableRow.LayoutParams layoutParams = new TableRow.LayoutParams( 35 | TableRow.LayoutParams.MATCH_PARENT, 36 | TableRow.LayoutParams.WRAP_CONTENT); 37 | layoutParams.gravity = Gravity.CENTER; 38 | layoutParams.leftMargin = 30; 39 | layoutParams.bottomMargin = 10; 40 | layoutParams.topMargin = 10; 41 | 42 | for (int i = 0; i < 40; i++) { 43 | TableRow tableRow = new TableRow(this); 44 | TextView textView = new TextView(this); 45 | textView.setText("Test stretch scroll view " + i); 46 | textView.setTextSize(20); 47 | textView.setPadding(15, 15, 15, 15); 48 | 49 | tableRow.addView(textView, layoutParams); 50 | if (i % 2 != 0) { 51 | tableRow.setBackgroundColor(Color.LTGRAY); 52 | } else { 53 | tableRow.setBackgroundColor(Color.WHITE); 54 | } 55 | 56 | final int n = i; 57 | tableRow.setOnClickListener(new View.OnClickListener() { 58 | @Override 59 | public void onClick(View v) { 60 | Toast.makeText(StretchViewActivity.this, "Click item " + n, Toast.LENGTH_SHORT).show(); 61 | } 62 | }); 63 | 64 | mMainLayout.addView(tableRow); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /PullScrollView/src/main/java/com/markmao/pullscrollview/ui/widget/PullScrollView.java: -------------------------------------------------------------------------------- 1 | package com.markmao.pullscrollview.ui.widget; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.PointF; 6 | import android.graphics.Rect; 7 | import android.util.AttributeSet; 8 | import android.util.Log; 9 | import android.view.MotionEvent; 10 | import android.view.View; 11 | import android.view.animation.TranslateAnimation; 12 | import android.widget.ScrollView; 13 | 14 | import com.markmao.pullscrollview.R; 15 | 16 | /** 17 | * 自定义ScrollView 18 | * 19 | * @author markmjw 20 | * @date 2013-09-13 21 | */ 22 | public class PullScrollView extends ScrollView { 23 | private static final String LOG_TAG = "PullScrollView"; 24 | /** 阻尼系数,越小阻力就越大. */ 25 | private static final float SCROLL_RATIO = 0.5f; 26 | 27 | /** 滑动至翻转的距离. */ 28 | private static final int TURN_DISTANCE = 100; 29 | 30 | /** 头部view. */ 31 | private View mHeader; 32 | 33 | /** 头部view高度. */ 34 | private int mHeaderHeight; 35 | 36 | /** 头部view显示高度. */ 37 | private int mHeaderVisibleHeight; 38 | 39 | /** ScrollView的content view. */ 40 | private View mContentView; 41 | 42 | /** ScrollView的content view矩形. */ 43 | private Rect mContentRect = new Rect(); 44 | 45 | /** 首次点击的Y坐标. */ 46 | private PointF mStartPoint = new PointF(); 47 | 48 | /** 是否开始移动. */ 49 | private boolean isMoving = false; 50 | 51 | /** 是否移动到顶部位置. */ 52 | private boolean isTop = false; 53 | 54 | /** 头部图片初始顶部和底部. */ 55 | private int mInitTop, mInitBottom; 56 | 57 | /** 头部图片拖动时顶部和底部. */ 58 | private int mCurrentTop, mCurrentBottom; 59 | 60 | /** 状态变化时的监听器. */ 61 | private OnTurnListener mOnTurnListener; 62 | 63 | private enum State { 64 | /**顶部*/ 65 | UP, 66 | /**底部*/ 67 | DOWN, 68 | /**正常*/ 69 | NORMAL 70 | } 71 | 72 | /** 状态. */ 73 | private State mState = State.NORMAL; 74 | 75 | public PullScrollView(Context context) { 76 | super(context); 77 | init(context, null); 78 | } 79 | 80 | public PullScrollView(Context context, AttributeSet attrs) { 81 | super(context, attrs); 82 | init(context, attrs); 83 | } 84 | 85 | public PullScrollView(Context context, AttributeSet attrs, int defStyle) { 86 | super(context, attrs, defStyle); 87 | init(context, attrs); 88 | } 89 | 90 | private void init(Context context, AttributeSet attrs) { 91 | // set scroll mode 92 | setOverScrollMode(OVER_SCROLL_NEVER); 93 | 94 | if (null != attrs) { 95 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.PullScrollView); 96 | 97 | if (ta != null) { 98 | mHeaderHeight = (int) ta.getDimension(R.styleable.PullScrollView_headerHeight, -1); 99 | mHeaderVisibleHeight = (int) ta.getDimension(R.styleable 100 | .PullScrollView_headerVisibleHeight, -1); 101 | ta.recycle(); 102 | } 103 | } 104 | } 105 | 106 | /** 107 | * 设置Header 108 | * 109 | * @param view 110 | */ 111 | public void setHeader(View view) { 112 | mHeader = view; 113 | } 114 | 115 | /** 116 | * 设置状态改变时的监听器 117 | * 118 | * @param turnListener 119 | */ 120 | public void setOnTurnListener(OnTurnListener turnListener) { 121 | mOnTurnListener = turnListener; 122 | } 123 | 124 | @Override 125 | protected void onFinishInflate() { 126 | if (getChildCount() > 0) { 127 | mContentView = getChildAt(0); 128 | } 129 | } 130 | 131 | @Override 132 | protected void onScrollChanged(int l, int t, int oldl, int oldt) { 133 | super.onScrollChanged(l, t, oldl, oldt); 134 | 135 | if (getScrollY() == 0) { 136 | isTop = true; 137 | } 138 | } 139 | 140 | @Override 141 | public boolean onInterceptTouchEvent(MotionEvent ev) { 142 | onTouchEvent(ev); 143 | return super.onInterceptTouchEvent(ev); 144 | } 145 | 146 | 147 | // @Override 148 | // public boolean onInterceptTouchEvent(MotionEvent ev) { 149 | // return onTouchEvent(ev) || super.onInterceptTouchEvent(ev); 150 | // } 151 | 152 | @Override 153 | public boolean onTouchEvent(MotionEvent ev) { 154 | if (mContentView != null) { 155 | int action = ev.getAction(); 156 | switch (action) { 157 | case MotionEvent.ACTION_DOWN: 158 | mStartPoint.set(ev.getX(), ev.getY()); 159 | mCurrentTop = mInitTop = mHeader.getTop(); 160 | mCurrentBottom = mInitBottom = mHeader.getBottom(); 161 | return super.onTouchEvent(ev); 162 | case MotionEvent.ACTION_MOVE: 163 | float deltaY = Math.abs(ev.getY() - mStartPoint.y); 164 | if (deltaY > 10 && deltaY > Math.abs(ev.getX() - mStartPoint.x)) { 165 | mHeader.clearAnimation(); 166 | mContentView.clearAnimation(); 167 | doActionMove(ev); 168 | } 169 | break; 170 | case MotionEvent.ACTION_UP: 171 | // 回滚动画 172 | if (isNeedAnimation()) { 173 | rollBackAnimation(); 174 | } 175 | 176 | if (getScrollY() == 0) { 177 | mState = State.NORMAL; 178 | } 179 | 180 | isMoving = false; 181 | break; 182 | default: 183 | break; 184 | 185 | } 186 | } 187 | 188 | // 禁止控件本身的滑动. 189 | boolean isHandle = isMoving; 190 | if (!isMoving) { 191 | try { 192 | isHandle = super.onTouchEvent(ev); 193 | } catch (Exception e) { 194 | Log.w(LOG_TAG, e); 195 | } 196 | } 197 | return isHandle; 198 | } 199 | 200 | /** 201 | * 执行移动动画 202 | * 203 | * @param event 204 | */ 205 | private void doActionMove(MotionEvent event) { 206 | // 当滚动到顶部时,将状态设置为正常,避免先向上拖动再向下拖动到顶端后首次触摸不响应的问题 207 | if (getScrollY() == 0) { 208 | mState = State.NORMAL; 209 | 210 | // 滑动经过顶部初始位置时,修正Touch down的坐标为当前Touch点的坐标 211 | if (isTop) { 212 | isTop = false; 213 | mStartPoint.y = event.getY(); 214 | } 215 | } 216 | 217 | float deltaY = event.getY() - mStartPoint.y; 218 | 219 | // 对于首次Touch操作要判断方位:UP OR DOWN 220 | if (deltaY < 0 && mState == State.NORMAL) { 221 | mState = State.UP; 222 | } else if (deltaY > 0 && mState == State.NORMAL) { 223 | mState = State.DOWN; 224 | } 225 | 226 | if (mState == State.UP) { 227 | deltaY = deltaY < 0 ? deltaY : 0; 228 | 229 | isMoving = false; 230 | 231 | } else if (mState == State.DOWN) { 232 | if (getScrollY() <= deltaY) { 233 | isMoving = true; 234 | } 235 | deltaY = deltaY < 0 ? 0 : (deltaY > mHeaderHeight ? mHeaderHeight : deltaY); 236 | } 237 | 238 | if (isMoving) { 239 | // 初始化content view矩形 240 | if (mContentRect.isEmpty()) { 241 | // 保存正常的布局位置 242 | mContentRect.set(mContentView.getLeft(), mContentView.getTop(), mContentView.getRight(), 243 | mContentView.getBottom()); 244 | } 245 | 246 | // 计算header移动距离(手势移动的距离*阻尼系数*0.5) 247 | float headerMoveHeight = deltaY * 0.5f * SCROLL_RATIO; 248 | mCurrentTop = (int) (mInitTop + headerMoveHeight); 249 | mCurrentBottom = (int) (mInitBottom + headerMoveHeight); 250 | 251 | // 计算content移动距离(手势移动的距离*阻尼系数) 252 | float contentMoveHeight = deltaY * SCROLL_RATIO; 253 | 254 | // 修正content移动的距离,避免超过header的底边缘 255 | int headerBottom = mCurrentBottom - mHeaderVisibleHeight; 256 | int top = (int) (mContentRect.top + contentMoveHeight); 257 | int bottom = (int) (mContentRect.bottom + contentMoveHeight); 258 | 259 | if (top <= headerBottom) { 260 | // 移动content view 261 | mContentView.layout(mContentRect.left, top, mContentRect.right, bottom); 262 | 263 | // 移动header view 264 | mHeader.layout(mHeader.getLeft(), mCurrentTop, mHeader.getRight(), mCurrentBottom); 265 | } 266 | } 267 | } 268 | 269 | private void rollBackAnimation() { 270 | TranslateAnimation tranAnim = new TranslateAnimation(0, 0, 271 | Math.abs(mInitTop - mCurrentTop), 0); 272 | tranAnim.setDuration(200); 273 | mHeader.startAnimation(tranAnim); 274 | 275 | mHeader.layout(mHeader.getLeft(), mInitTop, mHeader.getRight(), mInitBottom); 276 | 277 | // 开启移动动画 278 | TranslateAnimation innerAnim = new TranslateAnimation(0, 0, mContentView.getTop(), mContentRect.top); 279 | innerAnim.setDuration(200); 280 | mContentView.startAnimation(innerAnim); 281 | mContentView.layout(mContentRect.left, mContentRect.top, mContentRect.right, mContentRect.bottom); 282 | 283 | mContentRect.setEmpty(); 284 | 285 | // 回调监听器 286 | if (mCurrentTop > mInitTop + TURN_DISTANCE && mOnTurnListener != null){ 287 | mOnTurnListener.onTurn(); 288 | } 289 | } 290 | 291 | /** 292 | * 是否需要开启动画 293 | */ 294 | private boolean isNeedAnimation() { 295 | return !mContentRect.isEmpty() && isMoving; 296 | } 297 | 298 | /** 299 | * 翻转事件监听器 300 | * 301 | * @author markmjw 302 | */ 303 | public interface OnTurnListener { 304 | /** 305 | * 翻转回调方法 306 | */ 307 | public void onTurn(); 308 | } 309 | } 310 | -------------------------------------------------------------------------------- /PullScrollView/src/main/java/com/markmao/pullscrollview/ui/widget/StretchScrollView.java: -------------------------------------------------------------------------------- 1 | package com.markmao.pullscrollview.ui.widget; 2 | 3 | import android.content.Context; 4 | import android.os.Handler; 5 | import android.os.Message; 6 | import android.util.AttributeSet; 7 | import android.view.MotionEvent; 8 | import android.view.View; 9 | import android.widget.ScrollView; 10 | 11 | /** 12 | * A ScrollView which can scroll to (0,0) when pull down or up. 13 | * 14 | * @author markmjw 15 | * @date 2014-04-30 16 | */ 17 | public class StretchScrollView extends ScrollView { 18 | private static final int MSG_REST_POSITION = 0x01; 19 | 20 | /** The max scroll height. */ 21 | private static final int MAX_SCROLL_HEIGHT = 400; 22 | /** Damping, the smaller the greater the resistance */ 23 | private static final float SCROLL_RATIO = 0.4f; 24 | 25 | private View mChildRootView; 26 | 27 | private float mTouchY; 28 | private boolean mTouchStop = false; 29 | 30 | private int mScrollY = 0; 31 | private int mScrollDy = 0; 32 | 33 | private Handler mHandler = new Handler() { 34 | @Override 35 | public void handleMessage(Message msg) { 36 | if (MSG_REST_POSITION == msg.what) { 37 | if (mScrollY != 0 && mTouchStop) { 38 | mScrollY -= mScrollDy; 39 | 40 | if ((mScrollDy < 0 && mScrollY > 0) || (mScrollDy > 0 && mScrollY < 0)) { 41 | mScrollY = 0; 42 | } 43 | 44 | mChildRootView.scrollTo(0, mScrollY); 45 | // continue scroll after 20ms 46 | sendEmptyMessageDelayed(MSG_REST_POSITION, 20); 47 | } 48 | } 49 | } 50 | }; 51 | 52 | public StretchScrollView(Context context) { 53 | super(context); 54 | 55 | init(); 56 | } 57 | 58 | public StretchScrollView(Context context, AttributeSet attrs) { 59 | super(context, attrs); 60 | 61 | init(); 62 | } 63 | 64 | public StretchScrollView(Context context, AttributeSet attrs, int defStyle) { 65 | super(context, attrs, defStyle); 66 | 67 | init(); 68 | } 69 | 70 | private void init() { 71 | // set scroll mode 72 | setOverScrollMode(OVER_SCROLL_NEVER); 73 | } 74 | 75 | @Override 76 | protected void onFinishInflate() { 77 | if (getChildCount() > 0) { 78 | // when finished inflating from layout xml, get the first child view 79 | mChildRootView = getChildAt(0); 80 | } 81 | } 82 | 83 | @Override 84 | public boolean onInterceptTouchEvent(MotionEvent ev) { 85 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { 86 | mTouchY = ev.getY(); 87 | } 88 | return super.onInterceptTouchEvent(ev); 89 | } 90 | 91 | @Override 92 | public boolean onTouchEvent(MotionEvent ev) { 93 | if (null != mChildRootView) { 94 | doTouchEvent(ev); 95 | } 96 | return super.onTouchEvent(ev); 97 | } 98 | 99 | private void doTouchEvent(MotionEvent ev) { 100 | int action = ev.getAction(); 101 | 102 | switch (action) { 103 | case MotionEvent.ACTION_UP: 104 | mScrollY = mChildRootView.getScrollY(); 105 | if (mScrollY != 0) { 106 | mTouchStop = true; 107 | mScrollDy = (int) (mScrollY / 10.0f); 108 | mHandler.sendEmptyMessage(MSG_REST_POSITION); 109 | } 110 | break; 111 | 112 | case MotionEvent.ACTION_MOVE: 113 | float nowY = ev.getY(); 114 | int deltaY = (int) (mTouchY - nowY); 115 | mTouchY = nowY; 116 | if (isNeedMove()) { 117 | int offset = mChildRootView.getScrollY(); 118 | if (offset < MAX_SCROLL_HEIGHT && offset > -MAX_SCROLL_HEIGHT) { 119 | mChildRootView.scrollBy(0, (int) (deltaY * SCROLL_RATIO)); 120 | mTouchStop = false; 121 | } 122 | } 123 | break; 124 | 125 | default: 126 | break; 127 | } 128 | } 129 | 130 | private boolean isNeedMove() { 131 | int viewHeight = mChildRootView.getMeasuredHeight(); 132 | int scrollHeight = getHeight(); 133 | int offset = viewHeight - scrollHeight; 134 | int scrollY = getScrollY(); 135 | 136 | return scrollY == 0 || scrollY == offset; 137 | } 138 | } -------------------------------------------------------------------------------- /PullScrollView/src/main/res/drawable-hdpi/add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarkMjw/PullScrollView/0dbcbd2f4f17b4953f30a74d9481e1315e49e47e/PullScrollView/src/main/res/drawable-hdpi/add.png -------------------------------------------------------------------------------- /PullScrollView/src/main/res/drawable-hdpi/arrow_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarkMjw/PullScrollView/0dbcbd2f4f17b4953f30a74d9481e1315e49e47e/PullScrollView/src/main/res/drawable-hdpi/arrow_up.png -------------------------------------------------------------------------------- /PullScrollView/src/main/res/drawable-hdpi/avatar_default.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarkMjw/PullScrollView/0dbcbd2f4f17b4953f30a74d9481e1315e49e47e/PullScrollView/src/main/res/drawable-hdpi/avatar_default.png -------------------------------------------------------------------------------- /PullScrollView/src/main/res/drawable-hdpi/button_bg_normal.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarkMjw/PullScrollView/0dbcbd2f4f17b4953f30a74d9481e1315e49e47e/PullScrollView/src/main/res/drawable-hdpi/button_bg_normal.9.png -------------------------------------------------------------------------------- /PullScrollView/src/main/res/drawable-hdpi/button_bg_pressed.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarkMjw/PullScrollView/0dbcbd2f4f17b4953f30a74d9481e1315e49e47e/PullScrollView/src/main/res/drawable-hdpi/button_bg_pressed.9.png -------------------------------------------------------------------------------- /PullScrollView/src/main/res/drawable-hdpi/card_avatar_bar.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarkMjw/PullScrollView/0dbcbd2f4f17b4953f30a74d9481e1315e49e47e/PullScrollView/src/main/res/drawable-hdpi/card_avatar_bar.9.png -------------------------------------------------------------------------------- /PullScrollView/src/main/res/drawable-hdpi/card_whole.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarkMjw/PullScrollView/0dbcbd2f4f17b4953f30a74d9481e1315e49e47e/PullScrollView/src/main/res/drawable-hdpi/card_whole.9.png -------------------------------------------------------------------------------- /PullScrollView/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarkMjw/PullScrollView/0dbcbd2f4f17b4953f30a74d9481e1315e49e47e/PullScrollView/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /PullScrollView/src/main/res/drawable-hdpi/scrollview_header.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarkMjw/PullScrollView/0dbcbd2f4f17b4953f30a74d9481e1315e49e47e/PullScrollView/src/main/res/drawable-hdpi/scrollview_header.jpg -------------------------------------------------------------------------------- /PullScrollView/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarkMjw/PullScrollView/0dbcbd2f4f17b4953f30a74d9481e1315e49e47e/PullScrollView/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /PullScrollView/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarkMjw/PullScrollView/0dbcbd2f4f17b4953f30a74d9481e1315e49e47e/PullScrollView/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /PullScrollView/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarkMjw/PullScrollView/0dbcbd2f4f17b4953f30a74d9481e1315e49e47e/PullScrollView/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /PullScrollView/src/main/res/drawable/selector_btn.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /PullScrollView/src/main/res/layout/act_main.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 |