├── .gitignore
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── sys
│ │ └── blackcat
│ │ └── stickheaderlayout
│ │ └── demo
│ │ ├── DemoUtils.java
│ │ ├── GridViewDemo.java
│ │ ├── ListViewDemo.java
│ │ ├── MainActivity.java
│ │ ├── RecyclerViewDemo.java
│ │ ├── ScrollViewDemo.java
│ │ ├── ViewPagerDemo.java
│ │ ├── adapter
│ │ ├── FragmentAdapter.java
│ │ └── MyAdapter.java
│ │ └── fragment
│ │ ├── GridViewFragment.java
│ │ ├── ListViewFragment.java
│ │ ├── RecyclerViewFragment.java
│ │ └── ScrollViewFragment.java
│ └── res
│ ├── layout
│ ├── activity_grid_demo.xml
│ ├── activity_list_demo.xml
│ ├── activity_main.xml
│ ├── activity_recycler_view_demo.xml
│ ├── activity_scroll_view_demo.xml
│ ├── activity_view_pager_demo.xml
│ ├── list_head.xml
│ ├── recycler_view_fragment.xml
│ └── scroll_view_fragment.xml
│ ├── mipmap-hdpi
│ └── ic_launcher.png
│ ├── mipmap-mdpi
│ └── ic_launcher.png
│ ├── mipmap-xhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxxhdpi
│ └── ic_launcher.png
│ ├── values-w820dp
│ └── dimens.xml
│ └── values
│ ├── colors.xml
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
├── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── image
└── pp.gif
├── library
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── sys
│ │ └── blackcat
│ │ └── stickheaderlayout
│ │ └── ApplicationTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── sys
│ │ │ └── blackcat
│ │ │ └── stickheaderlayout
│ │ │ ├── IpmlScrollChangListener.java
│ │ │ └── StickHeaderLayout.java
│ └── res
│ │ └── values
│ │ ├── attrs.xml
│ │ └── strings.xml
│ └── test
│ └── java
│ └── com
│ └── sys
│ └── blackcat
│ └── stickheaderlayout
│ └── ExampleUnitTest.java
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 |
10 | # built application files
11 | *.apk
12 | *.ap_
13 |
14 | # files for the dex VM
15 | # Built application files
16 | *.apk
17 | *.ap_
18 |
19 | # Files for the Dalvik VM
20 | *.dex
21 |
22 | # Java class files
23 | *.class
24 |
25 | # built native files (uncomment if you build your own)
26 | # *.o
27 | # *.so
28 |
29 | # generated files
30 | bin/
31 | gen/
32 |
33 | # Ignore gradle files
34 | # Generated files
35 | bin/
36 | gen/
37 |
38 | # Gradle files
39 | .gradle/
40 | build/
41 |
42 | # Local configuration file (sdk path, etc)
43 | local.properties
44 |
45 | # Proguard folder generated by Eclipse
46 | proguard/
47 |
48 | # Eclipse Metadata
49 | .metadata/
50 |
51 | # Mac OS X clutter
52 | *.DS_Store
53 |
54 | # Windows clutter
55 | Thumbs.db
56 |
57 | # Intellij IDEA (see https://intellij-support.jetbrains.com/entries/23393067)
58 | .idea/workspace.xml
59 | .idea/tasks.xml
60 | .idea/datasources.xml
61 | .idea/dataSources.ids
62 | # Log Files
63 | *.log
64 |
65 | # Android Studio Navigation editor temp files
66 | .navigation/
67 |
68 | # Android Studio captures folder
69 | captures/
70 | .idea/gradle.xml
71 | .idea/misc.xml
72 | .idea/vcs.xml
73 |
74 | .idea/modules.xml
75 | .idea/compiler.xml
76 | .idea/encodings.xml
77 | .idea/runConfigurations.xml
78 | .idea/findbugs-idea.xml
79 | .idea/.name
80 |
81 | gradle.properties
82 | .idea/copyright/profiles_settings.xml
83 |
84 | .idea/*
85 | .idea
86 |
87 |
88 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Stickheaderlayout
2 | stickheaderlayout
3 |
4 | ## maven 引用
5 | ```
6 |
7 | com.sys.blackcat.stickheaderlayout
8 | library
9 | 1.3.5
10 | pom
11 |
12 | ```
13 | -------------------
14 | ## Gradle 引用
15 | compile 'com.sys.blackcat.stickheaderlayout:library:1.3.4'
16 |
17 | ### [  ](https://bintray.com/yang747046912/maven/StickHeaderLayout/_latestVersion)
18 |
19 |
20 | #自定义Stickheaderlayout介绍
21 | ##效果
22 | 
23 | ##原理
24 | Stickheaderlayout 继承 ViewGroup ,总体分为三块
25 | - 头部
26 | 可以放置上下不可滚动的view
27 | - 标题
28 | 可以放置上下不可滚动的view(不想出现标题则可以仅放一条横线)
29 | - 内容
30 | 可以放置GridView、ListView、RecyclerView、ScrollView、ViewPager并且兼容下拉刷新跟上拉自动加载更多
31 |
32 | 使用ViewDragHelper跟View事件的拦截相关的东西,ViewDragHelper不了解的可访问http://www.jianshu.com/p/bfe857e509c9
33 |
34 | ##IpmlScrollChangListener 介绍
35 | 包含四个方法
36 | - boolean isReadyForPull()
37 | 是否可以将head拉下来
38 | - void onStartScroll()
39 | 滚动开始
40 | - void onStopScroll()
41 | 滚动开始
42 | - void onScrollChange(int dy, int totallDy)
43 | 滚动距离的变化 dy:滚动的距离 totallDy:可滚动的最大距离 配合 setRetentionHeight(int )可以用于做渐变的titlebar (见viewPagerDemo效果图)
44 |
45 | ##StickHeaderLayout 方法介绍
46 | - setRetentionHeight(int retentionHeight)
47 | 设置向上滚动headView 保留的高度
48 | - setScroll(IpmlScrollChangListener scroll)
49 | 设置滚动回调
50 |
51 | ##ViewDragHelper初始化
52 | - 滚动状态监听
53 | ```
54 | public void onViewDragStateChanged(int state) {
55 | super.onViewDragStateChanged(state);
56 | if (scroll != null) {
57 | if (state == ViewDragHelper.STATE_IDLE) {
58 | scroll.onStopScroll();
59 | }
60 | if (state == ViewDragHelper.STATE_SETTLING) {
61 | scroll.onStartScroll();
62 | }
63 | }
64 | }
65 | ```
66 | - 判断ViewDragHelper是否触发滚动 头部、标题、内容都可以滑动
67 | ```
68 | public boolean tryCaptureView(View child, int pointerId) {
69 | return true;
70 | }
71 | ```
72 | - 手指按住contentView时滚动时,头部、标题、内容的位置变化,返回contentView是移动距离
73 | ```
74 | public int clampViewPositionVertical(View child, int top, int dy) {
75 | if (child == headView) {
76 | int headViewTop = headView.getTop() + dy;
77 | if (headViewTop > 0) {
78 | headViewTop = 0;
79 | } else if (headViewTop < -headHeight + retentionHeight) {
80 | headViewTop = -headHeight + retentionHeight;
81 | }
82 | return headViewTop;
83 | }
84 | if (child == titleView) {
85 | int titleTop = titleView.getTop() + dy;
86 | if (titleTop > headHeight) {
87 | titleTop = headHeight;
88 | } else if (titleTop < retentionHeight) {
89 | titleTop = retentionHeight;
90 | }
91 | return titleTop;
92 | }
93 | int contentTop = contentView.getTop() + dy;
94 | if (contentTop > headHeight + titleHeight) {
95 | contentTop = headHeight + titleHeight;
96 | } else if (contentTop < titleHeight + retentionHeight) {
97 | contentTop = titleHeight + retentionHeight;
98 | }
99 | return contentTop;
100 | }
101 | ```
102 | - 手指离开屏幕时,触发快速滚动
103 | ```
104 | public void onViewReleased(View releasedChild, float xvel, float yvel) {
105 | super.onViewReleased(releasedChild, xvel, yvel);
106 | if (dragEdge == DragEdge.Bottom) {
107 | if (yvel < -mDragHelper.getMinVelocity()) {
108 | mDragHelper.smoothSlideViewTo(contentView, 0, titleHeight + retentionHeight);
109 | } else if (releasedChild == headView) {
110 | mDragHelper.smoothSlideViewTo(headView, 0, -headHeight + retentionHeight);
111 | } else {
112 | mDragHelper.smoothSlideViewTo(titleView, 0, retentionHeight);
113 | }
114 | } else if (dragEdge == DragEdge.Top) {
115 | if (releasedChild == contentView) {
116 | mDragHelper.smoothSlideViewTo(contentView, 0, titleHeight + headHeight);
117 | } else if (releasedChild == headView) {
118 | mDragHelper.smoothSlideViewTo(headView, 0, 0);
119 | } else {
120 | mDragHelper.smoothSlideViewTo(titleView, 0, headHeight);
121 | }
122 | }
123 | invalidate();
124 | }
125 | ```
126 | - 触发快速滚动后头部、标题、内容的位置变化
127 | ```
128 | public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
129 | super.onViewPositionChanged(changedView, left, top, dx, dy);
130 | if (changedView == contentView) {
131 | int contentTop = contentView.getTop();
132 | titleView.layout(0, contentTop - titleHeight, titleView.getMeasuredWidth(), contentTop);
133 | headView.layout(0, contentTop - titleHeight - headHeight, titleView.getMeasuredWidth(), contentTop - titleHeight);
134 | } else if (changedView == headView) {
135 | int contentTop = headView.getTop();
136 | titleView.layout(0, contentTop + headHeight, titleView.getMeasuredWidth(), contentTop + headHeight + titleHeight);
137 | contentView.layout(0, contentTop + headHeight + titleHeight, contentView.getMeasuredWidth(), contentTop + headHeight + titleHeight + contentHeight);
138 | } else {
139 | int contentTop = titleView.getTop();
140 | headView.layout(0, contentTop - headHeight, headView.getMeasuredWidth(), contentTop);
141 | contentView.layout(0, contentTop + titleHeight, contentView.getMeasuredWidth(), contentTop + titleHeight + contentHeight);
142 | }
143 | if (scroll != null) {
144 | scroll.onScrollChange(Math.abs(headView.getTop()), headHeight - retentionHeight);
145 | }
146 | }
147 | ```
148 |
149 | ##手势的相关处理
150 | - 事件的拦截
151 | ```
152 | public boolean onInterceptTouchEvent(MotionEvent ev) {
153 | final int action = MotionEventCompat.getActionMasked(ev);
154 | switch (action) {
155 | case MotionEvent.ACTION_DOWN:
156 | mDragHelper.processTouchEvent(ev);
157 | sX = ev.getRawX();
158 | sY = ev.getRawY();
159 | mIsBeingDragged = false;
160 | break;
161 | case MotionEvent.ACTION_MOVE:
162 | checkCanDrag(ev);
163 | if (dragEdge == DragEdge.Left || dragEdge == DragEdge.Right || dragEdge == DragEdge.None) {
164 | return false;
165 | }
166 | if (scroll != null && !scroll.isReadyForPull()) {
167 | if (dragEdge == DragEdge.Bottom && titleView.getTop() == headHeight) {
168 | return true;
169 | }
170 | return false;
171 | } else {
172 | if (dragEdge == DragEdge.Bottom) {
173 | if (titleView.getTop() == retentionHeight) {
174 | mIsBeingDragged = false;
175 | dragEdge = DragEdge.None;
176 | }
177 | } else if (dragEdge == DragEdge.Top) {
178 | if (titleView.getTop() == headHeight) {
179 | dragEdge = DragEdge.None;
180 | mIsBeingDragged = false;
181 | }
182 | }
183 | }
184 | break;
185 | case MotionEvent.ACTION_UP:
186 | case MotionEvent.ACTION_CANCEL:
187 | mIsBeingDragged = false;
188 | mDragHelper.processTouchEvent(ev);
189 | break;
190 | default:
191 | mDragHelper.processTouchEvent(ev);
192 | break;
193 | }
194 | return mIsBeingDragged;
195 | }
196 | ```
197 | - 判断手势的滑动方向
198 | ```
199 | public enum DragEdge {
200 | None,
201 | Left,
202 | Top,
203 | Right,
204 | Bottom
205 | }
206 | private void checkCanDrag(MotionEvent ev) {
207 | float dx = ev.getRawX() - sX;
208 | float dy = ev.getRawY() - sY;
209 | float angle = Math.abs(dy / dx);
210 | angle = (float) Math.toDegrees(Math.atan(angle));
211 | if (angle < 45) {
212 | if (dx > 0) {
213 | dragEdge = DragEdge.Left;
214 | } else if (dx < 0) {
215 | dragEdge = DragEdge.Right;
216 | }
217 | } else {
218 | if (dy > 0) {
219 | dragEdge = DragEdge.Top;
220 | } else if (dy < 0) {
221 | dragEdge = DragEdge.Bottom;
222 | }
223 | }
224 | mIsBeingDragged = true;
225 | }
226 | ```
227 | - 事件的处理
228 | ```
229 | public boolean onTouchEvent(MotionEvent event) {
230 | int action = event.getActionMasked();
231 | switch (action) {
232 | case MotionEvent.ACTION_DOWN:
233 | mDragHelper.processTouchEvent(event);
234 | case MotionEvent.ACTION_MOVE: {
235 | checkCanDrag(event);
236 | if (mIsBeingDragged) {
237 | getParent().requestDisallowInterceptTouchEvent(true);
238 | try {
239 | mDragHelper.processTouchEvent(event);
240 | } catch (Exception e) {
241 | }
242 | }
243 | break;
244 | }
245 | case MotionEvent.ACTION_UP:
246 | case MotionEvent.ACTION_CANCEL:
247 | mIsBeingDragged = false;
248 | mDragHelper.processTouchEvent(event);
249 | break;
250 | default://handle other action, such as ACTION_POINTER_DOWN/UP
251 | mDragHelper.processTouchEvent(event);
252 | }
253 | return super.onTouchEvent(event) || mIsBeingDragged || action == MotionEvent.ACTION_DOWN;
254 | }
255 | ```
256 |
257 | ##使用 以ListView 为例子
258 | - 布局文件
259 | ```
260 |
261 |
265 |
266 |
273 |
274 |
281 |
282 |
287 |
288 | ```
289 | - Activity 逻辑处理
290 | ```
291 | public class ListViewDemo extends AppCompatActivity {
292 | private StickHeaderLayout layout;
293 | private ListView listView;
294 | @Override
295 | protected void onCreate(Bundle savedInstanceState) {
296 | super.onCreate(savedInstanceState);
297 | setContentView(R.layout.activity_list_demo);
298 | setTitle(R.string.list_example);
299 | layout = (StickHeaderLayout) findViewById(R.id.stick_header_layout);
300 | listView = (ListView) findViewById(R.id.list);
301 | layout.setScroll(new IpmlScrollChangListener() {
302 | @Override
303 | public boolean isReadyForPull() {
304 | return DemoUtils.isOnTop(listView);
305 | }
306 |
307 | @Override
308 | public void onStartScroll() {
309 |
310 | }
311 |
312 | @Override
313 | public void onStopScroll() {
314 |
315 | }
316 |
317 | @Override
318 | public void onScrollChange(int dy, int totallDy) {
319 |
320 | }
321 | });
322 | listView.addHeaderView(getLayoutInflater().inflate(R.layout.list_head, null));
323 | listView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, DemoUtils.getData()));
324 | }
325 | }
326 | ```
327 | - DemoUtils 判断内容部分是否可以滑动
328 | ```
329 | public static boolean isOnTop(ViewGroup viewGroup) {
330 | int[] groupLocation = new int[2];
331 | viewGroup.getLocationOnScreen(groupLocation);
332 | int[] itemLocation = new int[2];
333 | if (viewGroup.getChildAt(0) != null) {
334 | viewGroup.getChildAt(0).getLocationOnScreen(itemLocation);
335 | return groupLocation[1] == itemLocation[1];
336 | }
337 | return false;
338 | }
339 | ```
340 |
341 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.3"
6 |
7 | defaultConfig {
8 | applicationId "com.sys.blackcat.stickheaderlayout.demo"
9 | minSdkVersion 15
10 | targetSdkVersion 23
11 | versionCode 1
12 | versionName "1.0.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(include: ['*.jar'], dir: 'libs')
24 | testCompile 'junit:junit:4.12'
25 | compile 'com.android.support:appcompat-v7:23.3.0'
26 | compile 'com.android.support:recyclerview-v7:23.3.0'
27 | compile project(':library')
28 | }
29 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /home/yangcai/Android/Sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/app/src/main/java/com/sys/blackcat/stickheaderlayout/demo/DemoUtils.java:
--------------------------------------------------------------------------------
1 | package com.sys.blackcat.stickheaderlayout.demo;
2 |
3 | import android.view.ViewGroup;
4 |
5 | import java.util.ArrayList;
6 | import java.util.List;
7 |
8 | /**
9 | * Created by yangcai on 16-6-4.
10 | */
11 | public class DemoUtils {
12 |
13 | public static List getData() {
14 | List data = new ArrayList<>();
15 | for (int i = 0; i < 100; i++) {
16 | data.add("我是第" + i + "条数据");
17 | }
18 | return data;
19 | }
20 |
21 | /**
22 | * 判断listView 是否可以滑动
23 | *
24 | * @param viewGroup
25 | * @return
26 | */
27 | public static boolean isOnTop(ViewGroup viewGroup) {
28 | int[] groupLocation = new int[2];
29 | viewGroup.getLocationOnScreen(groupLocation);
30 | int[] itemLocation = new int[2];
31 | if (viewGroup.getChildAt(0) != null) {
32 | viewGroup.getChildAt(0).getLocationOnScreen(itemLocation);
33 | return groupLocation[1] == itemLocation[1];
34 | }
35 | return false;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/app/src/main/java/com/sys/blackcat/stickheaderlayout/demo/GridViewDemo.java:
--------------------------------------------------------------------------------
1 | package com.sys.blackcat.stickheaderlayout.demo;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.widget.ArrayAdapter;
6 | import android.widget.GridView;
7 |
8 | import com.sys.blackcat.stickheaderlayout.IpmlScrollChangListener;
9 | import com.sys.blackcat.stickheaderlayout.StickHeaderLayout;
10 |
11 | public class GridViewDemo extends AppCompatActivity {
12 |
13 |
14 | private StickHeaderLayout layout;
15 | private GridView gridView;
16 |
17 | @Override
18 | protected void onCreate(Bundle savedInstanceState) {
19 | super.onCreate(savedInstanceState);
20 | setContentView(R.layout.activity_grid_demo);
21 | setTitle(R.string.grid_example);
22 | layout = (StickHeaderLayout) findViewById(R.id.stick_header_layout);
23 | gridView = (GridView) findViewById(R.id.grid);
24 | layout.setScroll(new IpmlScrollChangListener() {
25 | @Override
26 | public boolean isReadyForPull() {
27 | return DemoUtils.isOnTop(gridView);
28 | }
29 |
30 | @Override
31 | public void onStartScroll() {
32 |
33 | }
34 |
35 | @Override
36 | public void onStopScroll() {
37 |
38 | }
39 |
40 | @Override
41 | public void onScrollChange(int dy, int totallDy) {
42 |
43 | }
44 | });
45 | gridView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, DemoUtils.getData()));
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/app/src/main/java/com/sys/blackcat/stickheaderlayout/demo/ListViewDemo.java:
--------------------------------------------------------------------------------
1 | package com.sys.blackcat.stickheaderlayout.demo;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.widget.ArrayAdapter;
6 | import android.widget.ListView;
7 |
8 | import com.sys.blackcat.stickheaderlayout.IpmlScrollChangListener;
9 | import com.sys.blackcat.stickheaderlayout.StickHeaderLayout;
10 |
11 | public class ListViewDemo extends AppCompatActivity {
12 |
13 | private StickHeaderLayout layout;
14 | private ListView listView;
15 |
16 | @Override
17 | protected void onCreate(Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | setContentView(R.layout.activity_list_demo);
20 | setTitle(R.string.list_example);
21 | layout = (StickHeaderLayout) findViewById(R.id.stick_header_layout);
22 | listView = (ListView) findViewById(R.id.list);
23 | layout.setScroll(new IpmlScrollChangListener() {
24 | @Override
25 | public boolean isReadyForPull() {
26 | return DemoUtils.isOnTop(listView);
27 | }
28 |
29 | @Override
30 | public void onStartScroll() {
31 |
32 | }
33 |
34 | @Override
35 | public void onStopScroll() {
36 |
37 | }
38 |
39 | @Override
40 | public void onScrollChange(int dy, int totallDy) {
41 |
42 | }
43 | });
44 | listView.addHeaderView(getLayoutInflater().inflate(R.layout.list_head, null));
45 | listView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, DemoUtils.getData()));
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/app/src/main/java/com/sys/blackcat/stickheaderlayout/demo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.sys.blackcat.stickheaderlayout.demo;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.view.View;
7 |
8 | public class MainActivity extends AppCompatActivity {
9 |
10 | @Override
11 | protected void onCreate(Bundle savedInstanceState) {
12 | super.onCreate(savedInstanceState);
13 | setContentView(R.layout.activity_main);
14 | }
15 |
16 |
17 | public void onClick(View view) {
18 | switch (view.getId()) {
19 | case R.id.btn_list:
20 | openActivty(ListViewDemo.class);
21 | break;
22 | case R.id.btn_grid:
23 | openActivty(GridViewDemo.class);
24 | break;
25 | case R.id.btn_scrollview:
26 | openActivty(ScrollViewDemo.class);
27 | break;
28 | case R.id.btn_recycler_view:
29 | openActivty(RecyclerViewDemo.class);
30 | break;
31 | case R.id.btn_view_pager:
32 | openActivty(ViewPagerDemo.class);
33 | break;
34 | }
35 | }
36 |
37 | private void openActivty(Class cl) {
38 | Intent intent = new Intent(this, cl);
39 | startActivity(intent);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/app/src/main/java/com/sys/blackcat/stickheaderlayout/demo/RecyclerViewDemo.java:
--------------------------------------------------------------------------------
1 | package com.sys.blackcat.stickheaderlayout.demo;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.support.v7.widget.RecyclerView;
6 | import android.support.v7.widget.StaggeredGridLayoutManager;
7 |
8 | import com.sys.blackcat.stickheaderlayout.IpmlScrollChangListener;
9 | import com.sys.blackcat.stickheaderlayout.StickHeaderLayout;
10 | import com.sys.blackcat.stickheaderlayout.demo.adapter.MyAdapter;
11 |
12 | public class RecyclerViewDemo extends AppCompatActivity {
13 |
14 |
15 | private StickHeaderLayout layout;
16 | private RecyclerView recyclerView;
17 |
18 | @Override
19 | protected void onCreate(Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 | setContentView(R.layout.activity_recycler_view_demo);
22 | setTitle(R.string.recyclerView_example);
23 | layout = (StickHeaderLayout) findViewById(R.id.stick_header_layout);
24 | recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
25 | layout.setScroll(new IpmlScrollChangListener() {
26 | @Override
27 | public boolean isReadyForPull() {
28 | return DemoUtils.isOnTop(recyclerView);
29 | }
30 |
31 | @Override
32 | public void onStartScroll() {
33 |
34 | }
35 |
36 | @Override
37 | public void onStopScroll() {
38 |
39 | }
40 |
41 | @Override
42 | public void onScrollChange(int dy, int totallDy) {
43 |
44 | }
45 | });
46 |
47 | MyAdapter myAdapter = new MyAdapter();
48 | myAdapter.addAll(DemoUtils.getData());
49 | // LinearLayoutManager manager = new LinearLayoutManager(this);
50 | // manager.setOrientation(LinearLayoutManager.VERTICAL);
51 | // GridLayoutManager manager = new GridLayoutManager(this,3);
52 | StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
53 | recyclerView.setLayoutManager(manager);
54 | recyclerView.setAdapter(myAdapter);
55 | }
56 |
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/app/src/main/java/com/sys/blackcat/stickheaderlayout/demo/ScrollViewDemo.java:
--------------------------------------------------------------------------------
1 | package com.sys.blackcat.stickheaderlayout.demo;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.widget.ScrollView;
6 |
7 | import com.sys.blackcat.stickheaderlayout.IpmlScrollChangListener;
8 | import com.sys.blackcat.stickheaderlayout.StickHeaderLayout;
9 |
10 | public class ScrollViewDemo extends AppCompatActivity {
11 |
12 |
13 | private StickHeaderLayout layout;
14 | private ScrollView scrollView;
15 |
16 | @Override
17 | protected void onCreate(Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | setContentView(R.layout.activity_scroll_view_demo);
20 | setTitle(R.string.scrollView_example);
21 | layout = (StickHeaderLayout) findViewById(R.id.stick_header_layout);
22 | scrollView = (ScrollView) findViewById(R.id.scroll_view);
23 | layout.setScroll(new IpmlScrollChangListener() {
24 | @Override
25 | public boolean isReadyForPull() {
26 | return DemoUtils.isOnTop(scrollView);
27 | }
28 |
29 | @Override
30 | public void onStartScroll() {
31 |
32 | }
33 |
34 | @Override
35 | public void onStopScroll() {
36 |
37 | }
38 |
39 | @Override
40 | public void onScrollChange(int dy, int totallDy) {
41 |
42 | }
43 | });
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/app/src/main/java/com/sys/blackcat/stickheaderlayout/demo/ViewPagerDemo.java:
--------------------------------------------------------------------------------
1 | package com.sys.blackcat.stickheaderlayout.demo;
2 |
3 | import android.graphics.Color;
4 | import android.os.Bundle;
5 | import android.support.v4.app.Fragment;
6 | import android.support.v4.view.ViewPager;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.view.ViewGroup;
9 | import android.widget.TextView;
10 |
11 | import com.sys.blackcat.stickheaderlayout.IpmlScrollChangListener;
12 | import com.sys.blackcat.stickheaderlayout.StickHeaderLayout;
13 | import com.sys.blackcat.stickheaderlayout.demo.adapter.FragmentAdapter;
14 | import com.sys.blackcat.stickheaderlayout.demo.fragment.GridViewFragment;
15 | import com.sys.blackcat.stickheaderlayout.demo.fragment.ListViewFragment;
16 | import com.sys.blackcat.stickheaderlayout.demo.fragment.RecyclerViewFragment;
17 | import com.sys.blackcat.stickheaderlayout.demo.fragment.ScrollViewFragment;
18 |
19 | import java.util.ArrayList;
20 | import java.util.List;
21 |
22 | public class ViewPagerDemo extends AppCompatActivity {
23 |
24 | private StickHeaderLayout layout;
25 | private ViewPager viewPager;
26 | private FragmentAdapter adapter;
27 | private TextView txt;
28 |
29 | @Override
30 | protected void onCreate(Bundle savedInstanceState) {
31 | super.onCreate(savedInstanceState);
32 | setContentView(R.layout.activity_view_pager_demo);
33 | setTitle(R.string.viewPager_example);
34 | layout = (StickHeaderLayout) findViewById(R.id.stick_header_layout);
35 | adapter = new FragmentAdapter(getSupportFragmentManager(), getFragment());
36 | viewPager = (ViewPager) findViewById(R.id.vp_view_pager);
37 | txt = (TextView) findViewById(R.id.tv_txt);
38 | layout.setScroll(new IpmlScrollChangListener() {
39 | private int rColor = 0xff;
40 | private int gColor = 0xff;
41 | private int bColor = 0x00;
42 |
43 | @Override
44 | public boolean isReadyForPull() {
45 | ViewGroup view = (ViewGroup) adapter.getItem(viewPager.getCurrentItem()).getView();
46 | if (view != null) {
47 | return DemoUtils.isOnTop(view);
48 | }
49 | return true;
50 | }
51 |
52 | @Override
53 | public void onStartScroll() {
54 |
55 | }
56 |
57 | @Override
58 | public void onStopScroll() {
59 |
60 | }
61 |
62 | @Override
63 | public void onScrollChange(int dy, int totallDy) {
64 | int aColor = (dy * 255) / totallDy;
65 | txt.setBackgroundColor(Color.argb(aColor, rColor, gColor, bColor));
66 | }
67 | });
68 | viewPager.setAdapter(adapter);
69 | }
70 |
71 |
72 | private List getFragment() {
73 | List list = new ArrayList<>();
74 | list.add(new GridViewFragment());
75 | list.add(new ListViewFragment());
76 | list.add(new ScrollViewFragment());
77 | list.add(new RecyclerViewFragment());
78 | return list;
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/app/src/main/java/com/sys/blackcat/stickheaderlayout/demo/adapter/FragmentAdapter.java:
--------------------------------------------------------------------------------
1 | package com.sys.blackcat.stickheaderlayout.demo.adapter;
2 |
3 | import android.support.v4.app.Fragment;
4 | import android.support.v4.app.FragmentManager;
5 | import android.support.v4.app.FragmentPagerAdapter;
6 |
7 | import java.util.ArrayList;
8 | import java.util.List;
9 |
10 | /**
11 | * Created by Administrator on 2016/6/9.
12 | */
13 | public class FragmentAdapter extends FragmentPagerAdapter {
14 | private List list = new ArrayList<>();
15 |
16 | public FragmentAdapter(FragmentManager fm, List list) {
17 | super(fm);
18 | this.list = list;
19 | }
20 |
21 | @Override
22 | public Fragment getItem(int position) {
23 | return list.get(position);
24 | }
25 |
26 | @Override
27 | public int getCount() {
28 | return list.size();
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/app/src/main/java/com/sys/blackcat/stickheaderlayout/demo/adapter/MyAdapter.java:
--------------------------------------------------------------------------------
1 | package com.sys.blackcat.stickheaderlayout.demo.adapter;
2 |
3 | import android.support.v7.widget.RecyclerView;
4 | import android.view.View;
5 | import android.view.ViewGroup;
6 | import android.widget.TextView;
7 |
8 | import java.util.ArrayList;
9 | import java.util.List;
10 |
11 | /**
12 | * Created by Administrator on 2016/6/9.
13 | */
14 | public class MyAdapter extends RecyclerView.Adapter {
15 |
16 | private List data = new ArrayList<>();
17 |
18 | @Override
19 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
20 | View view = View.inflate(parent.getContext(), android.R.layout.simple_list_item_1, null);
21 | return new Holder(view);
22 | }
23 |
24 | @Override
25 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
26 | ((TextView) (holder.itemView)).setText(data.get(position));
27 | }
28 |
29 | @Override
30 | public int getItemCount() {
31 | return data.size();
32 | }
33 |
34 | public void addAll(List data) {
35 | this.data.addAll(data);
36 | notifyDataSetChanged();
37 | }
38 |
39 | class Holder extends RecyclerView.ViewHolder {
40 |
41 | public Holder(View itemView) {
42 | super(itemView);
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/app/src/main/java/com/sys/blackcat/stickheaderlayout/demo/fragment/GridViewFragment.java:
--------------------------------------------------------------------------------
1 | package com.sys.blackcat.stickheaderlayout.demo.fragment;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v4.app.Fragment;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.AbsListView;
10 | import android.widget.ArrayAdapter;
11 | import android.widget.GridView;
12 |
13 | import com.sys.blackcat.stickheaderlayout.demo.DemoUtils;
14 |
15 | /**
16 | * Created by Administrator on 2016/6/9.
17 | */
18 | public class GridViewFragment extends Fragment {
19 | @Nullable
20 | @Override
21 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
22 | GridView mGridView = new GridView(getActivity());
23 | mGridView.setNumColumns(4);
24 | mGridView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT));
25 | mGridView.setAdapter(new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, DemoUtils.getData()));
26 | return mGridView;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/app/src/main/java/com/sys/blackcat/stickheaderlayout/demo/fragment/ListViewFragment.java:
--------------------------------------------------------------------------------
1 | package com.sys.blackcat.stickheaderlayout.demo.fragment;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v4.app.Fragment;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.AbsListView;
10 | import android.widget.ArrayAdapter;
11 | import android.widget.ListView;
12 |
13 | import com.sys.blackcat.stickheaderlayout.demo.DemoUtils;
14 |
15 | /**
16 | * Created by Administrator on 2016/6/9.
17 | */
18 | public class ListViewFragment extends Fragment {
19 | @Nullable
20 | @Override
21 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
22 | ListView mListView = new ListView(getActivity());
23 | mListView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT));
24 | mListView.setAdapter(new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, DemoUtils.getData()));
25 | return mListView;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/app/src/main/java/com/sys/blackcat/stickheaderlayout/demo/fragment/RecyclerViewFragment.java:
--------------------------------------------------------------------------------
1 | package com.sys.blackcat.stickheaderlayout.demo.fragment;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v4.app.Fragment;
6 | import android.support.v7.widget.RecyclerView;
7 | import android.support.v7.widget.StaggeredGridLayoutManager;
8 | import android.view.LayoutInflater;
9 | import android.view.View;
10 | import android.view.ViewGroup;
11 |
12 | import com.sys.blackcat.stickheaderlayout.demo.DemoUtils;
13 | import com.sys.blackcat.stickheaderlayout.demo.R;
14 | import com.sys.blackcat.stickheaderlayout.demo.adapter.MyAdapter;
15 |
16 | /**
17 | * Created by Administrator on 2016/6/9.
18 | */
19 | public class RecyclerViewFragment extends Fragment {
20 | private RecyclerView recyclerView;
21 |
22 | @Nullable
23 | @Override
24 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
25 | return inflater.inflate(R.layout.recycler_view_fragment, null);
26 | }
27 |
28 | @Override
29 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
30 | super.onActivityCreated(savedInstanceState);
31 | recyclerView = (RecyclerView) getView();
32 | MyAdapter myAdapter = new MyAdapter();
33 | myAdapter.addAll(DemoUtils.getData());
34 | // LinearLayoutManager manager = new LinearLayoutManager(this);
35 | // manager.setOrientation(LinearLayoutManager.VERTICAL);
36 | // GridLayoutManager manager = new GridLayoutManager(this,3);
37 | StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
38 | recyclerView.setLayoutManager(manager);
39 | recyclerView.setAdapter(myAdapter);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/app/src/main/java/com/sys/blackcat/stickheaderlayout/demo/fragment/ScrollViewFragment.java:
--------------------------------------------------------------------------------
1 | package com.sys.blackcat.stickheaderlayout.demo.fragment;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v4.app.Fragment;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 |
10 | import com.sys.blackcat.stickheaderlayout.demo.R;
11 |
12 | /**
13 | * Created by Administrator on 2016/6/9.
14 | */
15 | public class ScrollViewFragment extends Fragment {
16 | @Nullable
17 | @Override
18 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
19 | return inflater.inflate(R.layout.scroll_view_fragment, null);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_grid_demo.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
14 |
15 |
22 |
23 |
29 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_list_demo.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
14 |
15 |
22 |
23 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
16 |
17 |
25 |
26 |
34 |
35 |
43 |
44 |
52 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_recycler_view_demo.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
14 |
15 |
22 |
23 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_scroll_view_demo.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
14 |
15 |
22 |
23 |
28 |
29 |
33 |
34 |
41 |
42 |
49 |
50 |
57 |
58 |
65 |
66 |
73 |
74 |
81 |
82 |
89 |
90 |
97 |
98 |
105 |
106 |
113 |
114 |
121 |
122 |
129 |
130 |
137 |
138 |
145 |
146 |
153 |
154 |
161 |
162 |
169 |
170 |
177 |
178 |
185 |
186 |
193 |
194 |
201 |
202 |
209 |
210 |
217 |
218 |
225 |
226 |
233 |
234 |
241 |
242 |
249 |
250 |
257 |
258 |
265 |
266 |
273 |
274 |
281 |
282 |
289 |
290 |
297 |
298 |
305 |
306 |
313 |
314 |
321 |
322 |
329 |
330 |
337 |
338 |
345 |
346 |
353 |
354 |
361 |
362 |
369 |
370 |
377 |
378 |
385 |
386 |
393 |
394 |
401 |
402 |
409 |
410 |
417 |
418 |
425 |
426 |
433 |
434 |
441 |
442 |
449 |
450 |
457 |
458 |
465 |
466 |
473 |
474 |
481 |
482 |
489 |
490 |
497 |
498 |
505 |
506 |
513 |
514 |
521 |
522 |
529 |
530 |
537 |
538 |
545 |
546 |
553 |
554 |
561 |
562 |
569 |
570 |
577 |
578 |
585 |
586 |
593 |
594 |
601 |
602 |
609 |
610 |
617 |
618 |
625 |
626 |
633 |
634 |
641 |
642 |
649 |
650 |
657 |
658 |
665 |
666 |
673 |
674 |
681 |
682 |
689 |
690 |
697 |
698 |
705 |
706 |
713 |
714 |
721 |
722 |
729 |
730 |
737 |
738 |
739 |
740 |
741 |
742 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_view_pager_demo.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
13 |
20 |
21 |
28 |
29 |
34 |
35 |
36 |
44 |
45 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/list_head.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/recycler_view_fragment.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/scroll_view_fragment.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
12 |
13 |
20 |
21 |
28 |
29 |
36 |
37 |
44 |
45 |
52 |
53 |
60 |
61 |
68 |
69 |
76 |
77 |
84 |
85 |
92 |
93 |
100 |
101 |
108 |
109 |
116 |
117 |
124 |
125 |
132 |
133 |
140 |
141 |
148 |
149 |
156 |
157 |
164 |
165 |
172 |
173 |
180 |
181 |
188 |
189 |
196 |
197 |
204 |
205 |
212 |
213 |
220 |
221 |
228 |
229 |
236 |
237 |
244 |
245 |
252 |
253 |
260 |
261 |
268 |
269 |
276 |
277 |
284 |
285 |
292 |
293 |
300 |
301 |
308 |
309 |
316 |
317 |
324 |
325 |
332 |
333 |
340 |
341 |
348 |
349 |
356 |
357 |
364 |
365 |
372 |
373 |
380 |
381 |
388 |
389 |
396 |
397 |
404 |
405 |
412 |
413 |
420 |
421 |
428 |
429 |
436 |
437 |
444 |
445 |
452 |
453 |
460 |
461 |
468 |
469 |
476 |
477 |
484 |
485 |
492 |
493 |
500 |
501 |
508 |
509 |
516 |
517 |
524 |
525 |
532 |
533 |
540 |
541 |
548 |
549 |
556 |
557 |
564 |
565 |
572 |
573 |
580 |
581 |
588 |
589 |
596 |
597 |
604 |
605 |
612 |
613 |
620 |
621 |
628 |
629 |
636 |
637 |
644 |
645 |
652 |
653 |
660 |
661 |
668 |
669 |
676 |
677 |
684 |
685 |
692 |
693 |
700 |
701 |
708 |
709 |
716 |
717 |
718 |
719 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yang747046912/Stickheaderlayout/a31aa48d42cdf6849c93222fa4584fd28420ca39/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yang747046912/Stickheaderlayout/a31aa48d42cdf6849c93222fa4584fd28420ca39/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yang747046912/Stickheaderlayout/a31aa48d42cdf6849c93222fa4584fd28420ca39/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yang747046912/Stickheaderlayout/a31aa48d42cdf6849c93222fa4584fd28420ca39/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yang747046912/Stickheaderlayout/a31aa48d42cdf6849c93222fa4584fd28420ca39/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 | #FFFFFF
7 | #00a
8 | #a00
9 | #0a0
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | StickHeaderLayout
3 | listViewExample
4 | gridViewExample
5 | scrollViewExample
6 | recyclerViewExample
7 | viewPagerDemo
8 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
14 |
15 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.2.3'
9 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
10 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
11 | // NOTE: Do not place your application dependencies here;0 they belong
12 | // in the individual module build.gradle files
13 | }
14 | }
15 |
16 | allprojects {
17 | repositories {
18 | jcenter()
19 | }
20 | }
21 |
22 | task clean(type: Delete) {
23 | delete rootProject.buildDir
24 | }
25 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yang747046912/Stickheaderlayout/a31aa48d42cdf6849c93222fa4584fd28420ca39/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Oct 25 17:11:30 CST 2016
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/image/pp.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yang747046912/Stickheaderlayout/a31aa48d42cdf6849c93222fa4584fd28420ca39/image/pp.gif
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 | apply plugin: 'com.jfrog.bintray'
4 | // 这个version是区分library版本的,因此当我们需要更新library时记得修改这个version
5 | version = "1.3.5" //gradle bintrayUpload
6 | android {
7 | compileSdkVersion 23
8 | buildToolsVersion "23.0.3"
9 | resourcePrefix "StickHeaderLayout"
10 | defaultConfig {
11 | minSdkVersion 14
12 | targetSdkVersion 23
13 | versionCode 1
14 | versionName version
15 | }
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 | lintOptions {
23 | abortOnError false
24 | }
25 |
26 | }
27 | dependencies {
28 | compile fileTree(include: ['*.jar'], dir: 'libs')
29 | compile 'com.android.support:support-v4:23.3.0'
30 | testCompile 'junit:junit:4.12'
31 | }
32 | def siteUrl = 'https://github.com/yang747046912/Stickheaderlayout' // 项目的主页
33 | def gitUrl = 'https://github.com/yang747046912/Stickheaderlayout.git' // Git仓库的url
34 | group = "com.sys.blackcat.stickheaderlayout" // Maven Group ID for the artifact,一般填你唯一的包名
35 | install {
36 | repositories.mavenInstaller {
37 | // This generates POM.xml with proper parameters
38 | pom {
39 | project {
40 | packaging 'aar'
41 | // Add your description here
42 | name 'StickHeaderLayout' //项目描述
43 | url siteUrl
44 | // Set your license
45 | licenses {
46 | license {
47 | name 'The Apache Software License, Version 2.0'
48 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
49 | }
50 | }
51 | developers {
52 | developer {
53 | id 'yang747046912' //填写开发者基本信息
54 | name '杨才'
55 | email '2377599811@qq.com'
56 | }
57 | }
58 | scm {
59 | connection gitUrl
60 | developerConnection gitUrl
61 | url siteUrl
62 | }
63 | }
64 | }
65 | }
66 | }
67 | task sourcesJar(type: Jar) {
68 | from android.sourceSets.main.java.srcDirs
69 | classifier = 'sources'
70 | }
71 | task javadoc(type: Javadoc) {
72 | source = android.sourceSets.main.java.srcDirs
73 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
74 | }
75 | task javadocJar(type: Jar, dependsOn: javadoc) {
76 | classifier = 'javadoc'
77 | from javadoc.destinationDir
78 | }
79 | artifacts {
80 | archives javadocJar
81 | archives sourcesJar
82 | }
83 |
84 | bintray {
85 | user = "yang747046912"
86 | key = "7b635d1710016fb8afeff4136850be1927d00480"
87 | configurations = ['archives']
88 | pkg {
89 | repo = "maven" //发布到Bintray的那个仓库里,默认账户有四个库,我们这里上传到maven库
90 | name = "StickHeaderLayout" //发布到Bintray上的项目名字
91 | websiteUrl = siteUrl
92 | vcsUrl = gitUrl
93 | licenses = ["Apache-2.0"]
94 | publish = true
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /home/yangcai/Android/Sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/library/src/androidTest/java/com/sys/blackcat/stickheaderlayout/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.sys.blackcat.stickheaderlayout;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/library/src/main/java/com/sys/blackcat/stickheaderlayout/IpmlScrollChangListener.java:
--------------------------------------------------------------------------------
1 | package com.sys.blackcat.stickheaderlayout;
2 |
3 |
4 | public interface IpmlScrollChangListener {
5 | /**
6 | * 是否可以将head拉下来
7 | */
8 | boolean isReadyForPull();
9 |
10 | /**
11 | * 滚动开始
12 | */
13 | void onStartScroll();
14 |
15 | /**滚动结束*/
16 | void onStopScroll();
17 |
18 | /**
19 | * 滚动距离的变化
20 | * @param dy 滚动的距离
21 | * @param totallDy 滚动的最大距离
22 | */
23 | void onScrollChange(int dy, int totallDy);
24 | }
25 |
--------------------------------------------------------------------------------
/library/src/main/java/com/sys/blackcat/stickheaderlayout/StickHeaderLayout.java:
--------------------------------------------------------------------------------
1 | package com.sys.blackcat.stickheaderlayout;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.support.v4.view.MotionEventCompat;
6 | import android.support.v4.view.ViewCompat;
7 | import android.support.v4.widget.ViewDragHelper;
8 | import android.util.AttributeSet;
9 | import android.view.MotionEvent;
10 | import android.view.View;
11 | import android.view.ViewGroup;
12 |
13 |
14 | public class StickHeaderLayout extends ViewGroup {
15 |
16 | /**
17 | * 向上滚动headView 保留的高度
18 | */
19 | private int retentionHeight = 0;
20 | /**
21 | * 是否第一次布局
22 | */
23 | private boolean firstLayout = true;
24 | private boolean scrollable = true;
25 |
26 | private DragEdge dragEdge = DragEdge.None;
27 | private IpmlScrollChangListener scroll;
28 |
29 | private View headView;
30 | private int headHeight;
31 |
32 | private View titleView;
33 | private int titleHeight;
34 |
35 | private View contentView;
36 | private int contentHeight;
37 |
38 |
39 | private ViewDragHelper mDragHelper;
40 |
41 | public StickHeaderLayout(Context context) {
42 | this(context, null);
43 | }
44 |
45 | public StickHeaderLayout(Context context, AttributeSet attrs) {
46 | this(context, attrs, 0);
47 | }
48 |
49 | public StickHeaderLayout(Context context, AttributeSet attrs, int defStyleAttr) {
50 | super(context, attrs, defStyleAttr);
51 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.StickHeaderLayout);
52 | retentionHeight = typedArray.getDimensionPixelSize(R.styleable.StickHeaderLayout_retentionHeight, 0);
53 | scrollable = typedArray.getBoolean(R.styleable.StickHeaderLayout_scrollable, true);
54 | typedArray.recycle();
55 | mDragHelper = ViewDragHelper.create(this, 1f, callback);
56 | }
57 |
58 | private ViewDragHelper.Callback callback = new ViewDragHelper.Callback() {
59 |
60 | @Override
61 | public void onViewDragStateChanged(int state) {
62 | super.onViewDragStateChanged(state);
63 | if (scroll != null) {
64 | if (state == ViewDragHelper.STATE_IDLE) {
65 | scroll.onStopScroll();
66 | }
67 | if (state == ViewDragHelper.STATE_SETTLING) {
68 | scroll.onStartScroll();
69 | }
70 | }
71 | }
72 |
73 | @Override
74 | public boolean tryCaptureView(View child, int pointerId) {
75 | return true;
76 | }
77 |
78 | @Override
79 | public int clampViewPositionVertical(View child, int top, int dy) {
80 | if (child == headView) {
81 | int headViewTop = headView.getTop() + dy;
82 | if (headViewTop > 0) {
83 | headViewTop = 0;
84 | } else if (headViewTop < -headHeight + retentionHeight) {
85 | headViewTop = -headHeight + retentionHeight;
86 | }
87 | return headViewTop;
88 | }
89 | if (child == titleView) {
90 | int titleTop = titleView.getTop() + dy;
91 | if (titleTop > headHeight) {
92 | titleTop = headHeight;
93 | } else if (titleTop < retentionHeight) {
94 | titleTop = retentionHeight;
95 | }
96 | return titleTop;
97 | }
98 | int contentTop = contentView.getTop() + dy;
99 | if (contentTop > headHeight + titleHeight) {
100 | contentTop = headHeight + titleHeight;
101 | } else if (contentTop < titleHeight + retentionHeight) {
102 | contentTop = titleHeight + retentionHeight;
103 | }
104 | return contentTop;
105 | }
106 |
107 |
108 | @Override
109 | public void onViewReleased(View releasedChild, float xvel, float yvel) {
110 | super.onViewReleased(releasedChild, xvel, yvel);
111 | if (dragEdge == DragEdge.Bottom) {
112 | if (yvel < -mDragHelper.getMinVelocity()) {
113 | if (releasedChild == contentView) {
114 | mDragHelper.smoothSlideViewTo(contentView, 0, titleHeight + retentionHeight);
115 | } else if (releasedChild == headView) {
116 | mDragHelper.smoothSlideViewTo(headView, 0, -headHeight + retentionHeight);
117 | } else {
118 | mDragHelper.smoothSlideViewTo(titleView, 0, retentionHeight);
119 | }
120 | }
121 | } else if (dragEdge == DragEdge.Top) {
122 | if (yvel > mDragHelper.getMinVelocity()) {
123 | if (releasedChild == contentView) {
124 | mDragHelper.smoothSlideViewTo(contentView, 0, titleHeight + headHeight);
125 | } else if (releasedChild == headView) {
126 | mDragHelper.smoothSlideViewTo(headView, 0, 0);
127 | } else {
128 | mDragHelper.smoothSlideViewTo(titleView, 0, headHeight);
129 | }
130 | }
131 | }
132 | invalidate();
133 | }
134 |
135 | @Override
136 | public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
137 | super.onViewPositionChanged(changedView, left, top, dx, dy);
138 | if (changedView == contentView) {
139 | int contentTop = contentView.getTop();
140 | titleView.layout(0, contentTop - titleHeight, titleView.getMeasuredWidth(), contentTop);
141 | headView.layout(0, contentTop - titleHeight - headHeight, titleView.getMeasuredWidth(), contentTop - titleHeight);
142 | } else if (changedView == headView) {
143 | int contentTop = headView.getTop();
144 | titleView.layout(0, contentTop + headHeight, titleView.getMeasuredWidth(), contentTop + headHeight + titleHeight);
145 | contentView.layout(0, contentTop + headHeight + titleHeight, contentView.getMeasuredWidth(), contentTop + headHeight + titleHeight + contentHeight);
146 | } else {
147 | int contentTop = titleView.getTop();
148 | headView.layout(0, contentTop - headHeight, headView.getMeasuredWidth(), contentTop);
149 | contentView.layout(0, contentTop + titleHeight, contentView.getMeasuredWidth(), contentTop + titleHeight + contentHeight);
150 | }
151 | if (scroll != null) {
152 | scroll.onScrollChange(Math.abs(headView.getTop()), headHeight - retentionHeight);
153 | }
154 | }
155 | };
156 |
157 |
158 | @Override
159 | public void computeScroll() {
160 | super.computeScroll();
161 | if (mDragHelper.continueSettling(true)) {
162 | ViewCompat.postInvalidateOnAnimation(this);
163 | }
164 | }
165 |
166 | @Override
167 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
168 | int height = MeasureSpec.getSize(heightMeasureSpec);
169 | headView = getChildAt(0);
170 | measureChild(headView, widthMeasureSpec, heightMeasureSpec);
171 | headHeight = headView.getMeasuredHeight();
172 | titleView = getChildAt(1);
173 | measureChild(titleView, widthMeasureSpec, heightMeasureSpec);
174 | titleHeight = titleView.getMeasuredHeight();
175 | contentHeight = height - titleHeight - retentionHeight;
176 | contentView = getChildAt(2);
177 | contentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY));
178 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
179 | }
180 |
181 | @Override
182 | protected void onLayout(boolean changed, int l, int t, int r, int b) {
183 | if (!firstLayout) {
184 | dragEdge = DragEdge.None;
185 | mIsBeingDragged = false;
186 | headView.layout(0, headView.getTop(), headView.getMeasuredWidth(), headView.getTop() + headHeight);
187 | titleView.layout(0, headView.getTop() + headHeight, titleView.getMeasuredWidth(), headView.getTop() + headHeight + titleHeight);
188 | contentView.layout(0, headView.getTop() + headHeight + titleHeight, contentView.getMeasuredWidth(), headView.getTop() + headHeight + titleHeight + contentHeight);
189 | } else {
190 | firstLayout = false;
191 | headView.layout(0, 0, headView.getMeasuredWidth(), headHeight);
192 | titleView.layout(0, headHeight, titleView.getMeasuredWidth(), headHeight + titleHeight);
193 | contentView.layout(0, headHeight + titleHeight, contentView.getMeasuredWidth(), headHeight + titleHeight + contentHeight);
194 | }
195 | }
196 |
197 | private float sX = -1, sY = -1;
198 | private boolean mIsBeingDragged;
199 |
200 | @Override
201 | public boolean onInterceptTouchEvent(MotionEvent ev) {
202 | if (!scrollable) {
203 | return super.onInterceptTouchEvent(ev);
204 | }
205 | final int action = MotionEventCompat.getActionMasked(ev);
206 | switch (action) {
207 | case MotionEvent.ACTION_DOWN:
208 | mDragHelper.processTouchEvent(ev);
209 | sX = ev.getRawX();
210 | sY = ev.getRawY();
211 | mIsBeingDragged = false;
212 | break;
213 | case MotionEvent.ACTION_MOVE:
214 | checkCanDrag(ev);
215 | if (dragEdge == DragEdge.Left || dragEdge == DragEdge.Right || dragEdge == DragEdge.None) {
216 | return false;
217 | }
218 | if (scroll != null && !scroll.isReadyForPull()) {
219 | if (dragEdge == DragEdge.Bottom && titleView.getTop() == headHeight) {
220 | return true;
221 | }
222 | return false;
223 | } else {
224 | if (dragEdge == DragEdge.Bottom) {
225 | if (titleView.getTop() == retentionHeight) {
226 | mIsBeingDragged = false;
227 | dragEdge = DragEdge.None;
228 | }
229 | } else if (dragEdge == DragEdge.Top) {
230 | if (titleView.getTop() == headHeight) {
231 | dragEdge = DragEdge.None;
232 | mIsBeingDragged = false;
233 | }
234 | }
235 | }
236 | break;
237 | case MotionEvent.ACTION_UP:
238 | case MotionEvent.ACTION_CANCEL:
239 | mIsBeingDragged = false;
240 | mDragHelper.processTouchEvent(ev);
241 | break;
242 | default:
243 | mDragHelper.processTouchEvent(ev);
244 | break;
245 | }
246 | return mIsBeingDragged;
247 | }
248 |
249 | private void checkCanDrag(MotionEvent ev) {
250 | float dx = ev.getRawX() - sX;
251 | float dy = ev.getRawY() - sY;
252 | float angle = Math.abs(dy / dx);
253 | angle = (float) Math.toDegrees(Math.atan(angle));
254 | if (Float.isNaN(angle)) {
255 | dragEdge = DragEdge.None;
256 | } else if (angle < 45) {
257 | if (dx > 0) {
258 | dragEdge = DragEdge.Left;
259 | } else if (dx < 0) {
260 | dragEdge = DragEdge.Right;
261 | }
262 | } else {
263 | if (dy > 0) {
264 | dragEdge = DragEdge.Top;
265 | } else if (dy < 0) {
266 | dragEdge = DragEdge.Bottom;
267 | }
268 | }
269 | mIsBeingDragged = true;
270 | }
271 |
272 | @Override
273 | public boolean onTouchEvent(MotionEvent event) {
274 | if (!scrollable) {
275 | return super.onTouchEvent(event);
276 | }
277 | int action = event.getActionMasked();
278 | switch (action) {
279 | case MotionEvent.ACTION_DOWN:
280 | mDragHelper.processTouchEvent(event);
281 | case MotionEvent.ACTION_MOVE: {
282 | checkCanDrag(event);
283 | if (mIsBeingDragged) {
284 | getParent().requestDisallowInterceptTouchEvent(true);
285 | try {
286 | mDragHelper.processTouchEvent(event);
287 | } catch (Exception e) {
288 | }
289 | }
290 | break;
291 | }
292 | case MotionEvent.ACTION_UP:
293 | case MotionEvent.ACTION_CANCEL:
294 | mIsBeingDragged = false;
295 | mDragHelper.processTouchEvent(event);
296 | break;
297 | default://handle other action, such as ACTION_POINTER_DOWN/UP
298 | mDragHelper.processTouchEvent(event);
299 | }
300 | return super.onTouchEvent(event) || mIsBeingDragged || action == MotionEvent.ACTION_DOWN;
301 | }
302 |
303 |
304 | public enum DragEdge {
305 | None,
306 | Left,
307 | Top,
308 | Right,
309 | Bottom
310 | }
311 |
312 | /**
313 | * 设置向上滚动headView 保留的高度
314 | *
315 | * @param retentionHeight
316 | */
317 | public void setRetentionHeight(int retentionHeight) {
318 | this.retentionHeight = retentionHeight;
319 | }
320 |
321 | /**
322 | * 获取向上滚动headView 保留的高度
323 | */
324 | public int getRetentionHeight() {
325 | return retentionHeight;
326 | }
327 |
328 | public void setScrollable(boolean scrollable) {
329 | this.scrollable = scrollable;
330 | }
331 |
332 | public void setScroll(IpmlScrollChangListener scroll) {
333 | this.scroll = scroll;
334 | }
335 | }
336 |
--------------------------------------------------------------------------------
/library/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Library
3 |
4 |
--------------------------------------------------------------------------------
/library/src/test/java/com/sys/blackcat/stickheaderlayout/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.sys.blackcat.stickheaderlayout;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':library'
2 |
--------------------------------------------------------------------------------