├── .gitignore
├── PinnedSectionRecyclerView.iml
├── README.md
├── build.gradle
├── example.gif
├── example
├── build.gradle
├── example.iml
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── vk
│ │ └── pinnedsectionrecyclerview
│ │ └── example
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── vk
│ │ │ └── pinnedsectionrecyclerview
│ │ │ └── example
│ │ │ └── ExampleActivity.java
│ └── res
│ │ ├── layout
│ │ ├── activity_example.xml
│ │ ├── activity_gridview.xml
│ │ └── item.xml
│ │ ├── menu
│ │ └── menu_main.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-v21
│ │ └── styles.xml
│ │ ├── values-w820dp
│ │ └── dimens.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── vk
│ └── pinnedsectionrecyclerview
│ └── example
│ └── ExampleUnitTest.java
├── example_gridview.png
├── example_listview.png
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── library
├── build.gradle
├── library.iml
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── vk
│ │ └── pinnedsectionrecyclerview
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── vk
│ │ │ └── pinnedsectionrecyclerview
│ │ │ └── PinnedSectionRecyclerView.java
│ └── res
│ │ └── values
│ │ └── strings.xml
│ └── test
│ └── java
│ └── com
│ └── vk
│ └── pinnedsectionrecyclerview
│ └── ExampleUnitTest.java
├── local.properties
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | /.gradle
2 | example/example.iml
3 | library/library.iml
4 | .idea
5 | build
6 | example/build
7 | library/build
--------------------------------------------------------------------------------
/PinnedSectionRecyclerView.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # PinnedSectionRecyclerView
2 | Simple RecyclerView with pinned sections for Android.
3 |
4 | # ScreenShot
5 | 
6 | 
7 |
8 | # Gif
9 | 
10 |
11 | # Features
12 | 1. Support ListView and GridView style.
13 | 2. Support Section click and long click
14 |
15 | # Contact
16 | QQ:531372655
17 |
18 | Email:531372655@qq.com
19 |
20 | QQ学习群:476288569
21 |
22 | License
23 | -------
24 |
25 | Copyright 2017 VK Tan
26 |
27 | Licensed under the Apache License, Version 2.0 (the "License");
28 | you may not use this file except in compliance with the License.
29 | You may obtain a copy of the License at
30 |
31 | http://www.apache.org/licenses/LICENSE-2.0
32 |
33 | Unless required by applicable law or agreed to in writing, software
34 | distributed under the License is distributed on an "AS IS" BASIS,
35 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 | See the License for the specific language governing permissions and
37 | limitations under the License.
38 |
--------------------------------------------------------------------------------
/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 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/example.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VK2012/PinnedSectionRecyclerView/dd252c6735ead21f53f7b3168950c271b2bbc27a/example.gif
--------------------------------------------------------------------------------
/example/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.1"
6 | defaultConfig {
7 | applicationId "com.vk.pinnedsectionrecyclerview.example"
8 | minSdkVersion 19
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
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 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25 | exclude group: 'com.android.support', module: 'support-annotations'
26 | })
27 | compile project(':library')
28 | compile 'com.android.support:appcompat-v7:25.1.0'
29 | compile 'com.android.support:design:25.1.0'
30 | testCompile 'junit:junit:4.12'
31 | }
32 |
--------------------------------------------------------------------------------
/example/example.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | generateDebugSources
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
--------------------------------------------------------------------------------
/example/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 F:\VK\work_space\android_space\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 |
--------------------------------------------------------------------------------
/example/src/androidTest/java/com/vk/pinnedsectionrecyclerview/example/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.vk.pinnedsectionrecyclerview.example;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.vk.pinnedsectionrecyclerview.example", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/example/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/example/src/main/java/com/vk/pinnedsectionrecyclerview/example/ExampleActivity.java:
--------------------------------------------------------------------------------
1 | package com.vk.pinnedsectionrecyclerview.example;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.support.v7.widget.GridLayoutManager;
6 | import android.support.v7.widget.LinearLayoutManager;
7 | import android.support.v7.widget.RecyclerView;
8 | import android.util.Log;
9 | import android.view.LayoutInflater;
10 | import android.view.Menu;
11 | import android.view.MenuItem;
12 | import android.view.View;
13 | import android.view.ViewGroup;
14 | import android.widget.LinearLayout;
15 | import android.widget.TextView;
16 | import android.widget.Toast;
17 |
18 | import com.vk.pinnedsectionrecyclerview.PinnedSectionRecyclerView;
19 |
20 | import java.util.ArrayList;
21 | import java.util.List;
22 | import java.util.Locale;
23 |
24 | public class ExampleActivity extends AppCompatActivity {
25 |
26 | public static final String TAG = "ExampleActivity";
27 |
28 | private static final int[] COLORS = new int[] {
29 | R.color.green_light, R.color.orange_light,
30 | R.color.blue_light, R.color.red_light };
31 |
32 | private List- mList = new ArrayList<>();
33 |
34 | MyAdapter myAdapter;
35 |
36 | PinnedSectionRecyclerView mPinnedRecyclerView;
37 |
38 | private static final int V_LISTVIEW = 0;
39 |
40 | private static final int V_GRIDVIEW = 2;
41 |
42 | @Override
43 | protected void onCreate(Bundle savedInstanceState) {
44 | super.onCreate(savedInstanceState);
45 | setContentView(R.layout.activity_example);
46 |
47 | mPinnedRecyclerView = (PinnedSectionRecyclerView)findViewById(R.id.recyclerView);
48 |
49 | mPinnedRecyclerView.setOnPinnedSectionTouchListener(mOnPinnedSectionTouchListener);
50 |
51 | updateLayoutManager(V_LISTVIEW);
52 |
53 | genData('A','Z');
54 |
55 | myAdapter = new MyAdapter();
56 |
57 | mPinnedRecyclerView.setAdapter(myAdapter);
58 |
59 | }
60 |
61 | private void updateLayoutManager(int mode){
62 | switch (mode){
63 | //-----listview
64 | case V_LISTVIEW:
65 | mPinnedRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayout.VERTICAL,false));
66 | break;
67 |
68 | //--------grid view
69 | case V_GRIDVIEW:
70 | GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3,LinearLayoutManager.VERTICAL,false);
71 | gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
72 | @Override
73 | public int getSpanSize(int position) {
74 | if( mList.get(position).type == Item.SECTION)
75 | return 3;
76 | else return 1;
77 | }
78 | });
79 | mPinnedRecyclerView.setLayoutManager(gridLayoutManager);
80 | break;
81 | }
82 |
83 | }
84 |
85 | @Override
86 | public boolean onOptionsItemSelected(MenuItem item) {
87 | // Handle action bar item clicks here. The action bar will
88 | // automatically handle clicks on the Home/Up button, so long
89 | // as you specify a parent activity in AndroidManifest.xml.
90 | int id = item.getItemId();
91 |
92 | //noinspection SimplifiableIfStatement
93 | boolean ret = false;
94 | switch (id){
95 | case R.id.action_vertical_listview:
96 | updateLayoutManager(V_LISTVIEW);
97 | ret = true;
98 | break;
99 | case R.id.action_vertical_gridview:
100 | updateLayoutManager(V_GRIDVIEW);
101 | ret = true;
102 | break;
103 | }
104 |
105 | if(ret)
106 | return true;
107 | return super.onOptionsItemSelected(item);
108 | }
109 |
110 | @Override
111 | public boolean onCreateOptionsMenu(Menu menu) {
112 | getMenuInflater().inflate(R.menu.menu_main,menu);
113 | return true;
114 | }
115 |
116 | private void genData(char from, char to){
117 |
118 | mList.clear();
119 | final int sectionsNumber = to - from + 1;
120 |
121 | int sectionPosition = 0, listPosition = 0;
122 | int preSectionPosition = -1;
123 | for (char i=0; i -1){
128 | Item preSection = mList.get(preSectionPosition);
129 | preSection.nextSectionPosition = sectionPosition;
130 | }
131 | mList.add(section);
132 |
133 | final int itemsNumber = (int) Math.abs((Math.cos(2f*Math.PI/3f * sectionsNumber / (i+1f)) * 25f));
134 | for (int j=0;j implements PinnedSectionRecyclerView.Adapter{
189 |
190 | @Override
191 | public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
192 | View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
193 | MyAdapter.MyViewHolder myViewHolder = new MyAdapter.MyViewHolder(itemView,mItemOnClickListener,mItemOnLongClickListener);
194 | return myViewHolder;
195 | }
196 |
197 | @Override
198 | public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position) {
199 | Item item = mList.get(position);
200 |
201 | if (item.type == Item.SECTION) {
202 | //view.setOnClickListener(PinnedSectionListActivity.this);
203 | int color = holder.itemView.getContext().getResources().getColor(COLORS[item.sectionPosition % COLORS.length]);
204 | holder.mTextView.setBackgroundColor(color);
205 | }
206 | holder.mTextView.setText(mList.get(position).text);
207 | holder.itemView.setTag(position);
208 | }
209 |
210 | @Override
211 | public int getItemCount() {
212 | return mList.size();
213 | }
214 |
215 | @Override
216 | public int getItemViewType(int position) {
217 | return mList.get(position).type;
218 | }
219 |
220 | @Override
221 | public boolean isPinnedSectionItem(int position) {
222 | return getItemViewType(position) == Item.SECTION;
223 | }
224 |
225 | @Override
226 | public int findSectionPosition(int position) {
227 | return mList.get(position).sectionPosition;
228 | }
229 |
230 | @Override
231 | public int findNextSectionPosition(int position) {
232 | Log.d(TAG, "findNextSectionPosition: "+position+" , "+mList.get(position).toString());
233 | return mList.get(position).nextSectionPosition;
234 | }
235 |
236 | class MyViewHolder extends RecyclerView.ViewHolder{
237 |
238 | TextView mTextView;
239 | public MyViewHolder(View itemView,View.OnClickListener itemOnClickListener,View.OnLongClickListener itemOnLongClickListener) {
240 | super(itemView);
241 | mTextView = (TextView) itemView.findViewById(R.id.tv);
242 | itemView.setOnClickListener(itemOnClickListener);
243 | itemView.setOnLongClickListener(itemOnLongClickListener);
244 | }
245 |
246 | }
247 | }
248 |
249 | static class Item {
250 |
251 | public static final int ITEM = 0;
252 | public static final int SECTION = 1;
253 |
254 | public final int type;
255 | public final String text;
256 |
257 | public int sectionPosition;
258 | public int nextSectionPosition = -1;
259 | public int listPosition;
260 |
261 | public Item(int type, String text) {
262 | this.type = type;
263 | this.text = text;
264 | }
265 |
266 | @Override
267 | public String toString() {
268 | return "Item{" +
269 | "type=" + type +
270 | ", text='" + text + '\'' +
271 | ", sectionPosition=" + sectionPosition +
272 | ", nextSectionPosition=" + nextSectionPosition +
273 | ", listPosition=" + listPosition +
274 | '}';
275 | }
276 | }
277 |
278 | }
279 |
--------------------------------------------------------------------------------
/example/src/main/res/layout/activity_example.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/example/src/main/res/layout/activity_gridview.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
13 |
18 |
--------------------------------------------------------------------------------
/example/src/main/res/layout/item.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
14 |
--------------------------------------------------------------------------------
/example/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
30 |
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VK2012/PinnedSectionRecyclerView/dd252c6735ead21f53f7b3168950c271b2bbc27a/example/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VK2012/PinnedSectionRecyclerView/dd252c6735ead21f53f7b3168950c271b2bbc27a/example/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VK2012/PinnedSectionRecyclerView/dd252c6735ead21f53f7b3168950c271b2bbc27a/example/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VK2012/PinnedSectionRecyclerView/dd252c6735ead21f53f7b3168950c271b2bbc27a/example/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VK2012/PinnedSectionRecyclerView/dd252c6735ead21f53f7b3168950c271b2bbc27a/example/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/values-v21/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
9 |
10 |
--------------------------------------------------------------------------------
/example/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/example/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 | #ffff4444
8 | #ff99cc00
9 | #ffffbb33
10 | #ff33b5e5
11 |
12 |
--------------------------------------------------------------------------------
/example/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 | 16dp
6 |
7 |
--------------------------------------------------------------------------------
/example/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | PinnedSectionRecyclerView
3 | Main2Activity
4 |
5 | Vertical ListView
6 | Horizontal ListView
7 | Vertical GridView
8 | Horizontal GridView
9 |
10 |
--------------------------------------------------------------------------------
/example/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/example/src/test/java/com/vk/pinnedsectionrecyclerview/example/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.vk.pinnedsectionrecyclerview.example;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/example_gridview.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VK2012/PinnedSectionRecyclerView/dd252c6735ead21f53f7b3168950c271b2bbc27a/example_gridview.png
--------------------------------------------------------------------------------
/example_listview.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VK2012/PinnedSectionRecyclerView/dd252c6735ead21f53f7b3168950c271b2bbc27a/example_listview.png
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VK2012/PinnedSectionRecyclerView/dd252c6735ead21f53f7b3168950c271b2bbc27a/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
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 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.1"
6 |
7 | defaultConfig {
8 | minSdkVersion 19
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 |
13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 |
15 | }
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 | }
23 |
24 | dependencies {
25 | compile fileTree(dir: 'libs', include: ['*.jar'])
26 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
27 | exclude group: 'com.android.support', module: 'support-annotations'
28 | })
29 | compile 'com.android.support:appcompat-v7:25.1.0'
30 | compile 'com.jcodecraeer:xrecyclerview:1.2.7'
31 | testCompile 'junit:junit:4.12'
32 | }
33 |
--------------------------------------------------------------------------------
/library/library.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | generateDebugSources
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
--------------------------------------------------------------------------------
/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 F:\VK\work_space\android_space\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/vk/pinnedsectionrecyclerview/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.vk.pinnedsectionrecyclerview;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.vk.pinnedrecyclerview.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/library/src/main/java/com/vk/pinnedsectionrecyclerview/PinnedSectionRecyclerView.java:
--------------------------------------------------------------------------------
1 | package com.vk.pinnedsectionrecyclerview;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.Color;
6 | import android.graphics.Rect;
7 | import android.graphics.drawable.GradientDrawable;
8 | import android.support.annotation.Nullable;
9 | import android.support.v7.widget.LinearLayoutManager;
10 | import android.support.v7.widget.RecyclerView;
11 | import android.util.AttributeSet;
12 | import android.util.Log;
13 | import android.view.GestureDetector;
14 | import android.view.HapticFeedbackConstants;
15 | import android.view.MotionEvent;
16 | import android.view.SoundEffectConstants;
17 | import android.view.View;
18 | import android.view.ViewGroup;
19 | import android.view.accessibility.AccessibilityEvent;
20 | import android.widget.Toast;
21 |
22 | /**
23 | * Created by VK on 2017/2/14.
24 | * -
25 | */
26 |
27 | public class PinnedSectionRecyclerView extends RecyclerView {
28 | public static final String TAG = "PinnedSectRecyclerView";
29 |
30 | /**
31 | * shadow instance with a pinned view, can be null.
32 | */
33 | PinnedSection mPinnedSection;
34 |
35 | /**
36 | * Shadow for being recycled, can be null.
37 | */
38 | PinnedSection mRecycleSection;
39 |
40 | // fields used for drawing shadow under a pinned section
41 | private GradientDrawable mShadowDrawable;
42 | private int mSectionsDistanceY;
43 | private int mShadowHeight;
44 |
45 | /**
46 | * Pinned view Y-translation. We use it to stick pinned view to the next section.
47 | */
48 | int mTranslateY;
49 |
50 | // fields used for handling touch events
51 | private final Rect mTouchRect = new Rect();
52 | private View mTouchTarget;
53 |
54 | //用于检测touch事件落点在pinnedview的手势情况
55 | private GestureDetector mGestureDetector;
56 |
57 | private OnPinnedSectionTouchListener mOnPinnedSectionTouchListener;
58 |
59 | public PinnedSectionRecyclerView(Context context) {
60 | super(context);
61 | initView();
62 | }
63 |
64 | public PinnedSectionRecyclerView(Context context, @Nullable AttributeSet attrs) {
65 | super(context, attrs);
66 | initView();
67 | }
68 |
69 | public PinnedSectionRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
70 | super(context, attrs, defStyle);
71 | initView();
72 | }
73 |
74 | Toast mToast;
75 |
76 | private void showToast(String text) {
77 | boolean ret = true;
78 | if (ret)
79 | return;
80 | if (mToast == null) {
81 | mToast = Toast.makeText(getContext().getApplicationContext(), text, Toast.LENGTH_SHORT);
82 | } else {
83 | mToast.setText(text);
84 | }
85 |
86 | mToast.show();
87 |
88 | }
89 |
90 | private void initView() {
91 | addOnScrollListener(mOnScrollListener);
92 | initShadow(true);
93 | mGestureDetector = new GestureDetector(getContext().getApplicationContext(), new GestureDetector.OnGestureListener() {
94 | @Override
95 | public boolean onDown(MotionEvent e) {
96 | return true;
97 | }
98 |
99 | @Override
100 | public void onShowPress(MotionEvent e) {
101 | showToast("onShowPress");
102 | }
103 |
104 | @Override
105 | public boolean onSingleTapUp(MotionEvent e) {
106 | playSoundEffect(SoundEffectConstants.CLICK);
107 | if (mPinnedSection.holder.itemView != null) {
108 | mPinnedSection.holder.itemView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
109 | }
110 | if (mOnPinnedSectionTouchListener != null)
111 | mOnPinnedSectionTouchListener.onClick(mPinnedSection.holder.itemView, mPinnedSection.position);
112 | return true;
113 | }
114 |
115 | @Override
116 | public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
117 | return true;
118 | }
119 |
120 | @Override
121 | public void onLongPress(MotionEvent e) {
122 | if (mPinnedSection.holder.itemView != null) {
123 | mPinnedSection.holder.itemView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
124 | performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
125 | }
126 | if (mOnPinnedSectionTouchListener != null)
127 | mOnPinnedSectionTouchListener.onLongClick(mPinnedSection.holder.itemView, mPinnedSection.position);
128 | }
129 |
130 | @Override
131 | public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
132 | return true;
133 | }
134 | });
135 | }
136 |
137 | //-- public API methods
138 |
139 | public void setShadowVisible(boolean visible) {
140 | initShadow(visible);
141 | if (mPinnedSection != null) {
142 | View v = mPinnedSection.holder.itemView;
143 | invalidate(v.getLeft(), v.getTop(), v.getRight(), v.getBottom() + mShadowHeight);
144 | }
145 | }
146 |
147 | //-- pinned section drawing methods
148 |
149 | public void initShadow(boolean visible) {
150 | if (visible) {
151 | if (mShadowDrawable == null) {
152 | mShadowDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
153 | new int[]{Color.parseColor("#ffa0a0a0"), Color.parseColor("#50a0a0a0"), Color.parseColor("#00a0a0a0")});
154 | mShadowHeight = (int) (8 * getResources().getDisplayMetrics().density);
155 | }
156 | } else {
157 | if (mShadowDrawable != null) {
158 | mShadowDrawable = null;
159 | mShadowHeight = 0;
160 | }
161 | }
162 | }
163 |
164 | RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {
165 | @Override
166 | public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
167 | super.onScrollStateChanged(recyclerView, newState);
168 | }
169 |
170 | @Override
171 | public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
172 | super.onScrolled(recyclerView, dx, dy);
173 | checkOnScrolled();
174 | }
175 | };
176 |
177 | @Override
178 | protected void dispatchDraw(Canvas canvas) {
179 | super.dispatchDraw(canvas);
180 |
181 | if (mPinnedSection != null) {
182 | canvas.save();
183 |
184 | View view = mPinnedSection.holder.itemView;
185 |
186 | int pLeft = getPaddingLeft();
187 |
188 | int pTop = getPaddingTop();
189 |
190 | int clipHeight = view.getHeight() + (mShadowDrawable == null ? 0 : Math.min(mShadowHeight, mSectionsDistanceY));
191 | canvas.clipRect(0, 0, view.getWidth(), clipHeight);
192 |
193 | canvas.translate(pLeft, pTop + mTranslateY);
194 | drawChild(canvas, view, getDrawingTime());
195 |
196 | //绘制阴影
197 | if (mShadowDrawable != null && mSectionsDistanceY > 0) {
198 | mShadowDrawable.setBounds(view.getLeft(), view.getBottom(), view.getRight(), view.getBottom() + mShadowHeight);
199 | mShadowDrawable.draw(canvas);
200 | }
201 | canvas.restore();
202 | }
203 |
204 | }
205 |
206 | private void createPinnedSection(int position) {
207 |
208 | removePinnedSection();
209 | Log.d(TAG, "createPinnedSection: " + position);
210 | // try to recycle shadow
211 | PinnedSection pinnedShadow = mRecycleSection;
212 | mRecycleSection = null;
213 | mTranslateY = 0;
214 | mSectionsDistanceY = 0;
215 | if (mRecycleSection == null) {
216 | pinnedShadow = new PinnedSection();
217 | pinnedShadow.position = position;
218 | }
219 |
220 | RecyclerView.Adapter adapter = getAdapter();
221 | ViewHolder viewHolder = adapter.createViewHolder(this, adapter.getItemViewType(position));
222 | adapter.bindViewHolder(viewHolder, position);
223 |
224 | // read layout parameters
225 | ViewGroup.LayoutParams layoutParams = viewHolder.itemView.getLayoutParams();
226 | if (layoutParams == null) {
227 | layoutParams = generateDefaultLayoutParams();
228 | viewHolder.itemView.setLayoutParams(layoutParams);
229 | }
230 |
231 | int heightMode = MeasureSpec.getMode(layoutParams.height);
232 | int heightSize = MeasureSpec.getSize(layoutParams.height);
233 |
234 | if (heightMode == MeasureSpec.UNSPECIFIED) heightMode = MeasureSpec.EXACTLY;
235 |
236 | int maxHeight = getHeight() - getPaddingTop() - getPaddingBottom();
237 | if (heightSize > maxHeight) heightSize = maxHeight;
238 |
239 | // measure & layout
240 | int ws = MeasureSpec.makeMeasureSpec(getWidth() - getPaddingLeft() - getPaddingRight(), MeasureSpec.EXACTLY);
241 | int hs = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
242 | viewHolder.itemView.measure(ws, hs);
243 | viewHolder.itemView.layout(0, 0, viewHolder.itemView.getMeasuredWidth(), viewHolder.itemView.getMeasuredHeight());
244 |
245 | pinnedShadow.holder = viewHolder;
246 |
247 | mPinnedSection = pinnedShadow;
248 |
249 | updatePinnedSectionLocation();
250 |
251 | }
252 |
253 | /**
254 | * 更新悬挂视图Section
255 | *
256 | * @param firstVisibleItemPosition
257 | * @param firstCompletelyVisibleItemPosition
258 | */
259 | private void updatePinnedSection(int firstVisibleItemPosition, int firstCompletelyVisibleItemPosition) {
260 | //找出目标悬挂section位置
261 | int pinnedSectionPosition = findPinnedSection(firstVisibleItemPosition);
262 |
263 | if (mPinnedSection != null && mPinnedSection.position == firstCompletelyVisibleItemPosition) {
264 | //若目标悬挂section位置相等于第一个完全显示的item位置,移除悬挂
265 | Log.d(TAG, "updatePinnedSection: removePinnedSection");
266 | removePinnedSection();
267 | }
268 |
269 | if (mPinnedSection != null && mPinnedSection.position == pinnedSectionPosition) {
270 | //若当前绘制的悬挂位置已经等于目标悬挂位置,更新具体位置
271 | Log.d(TAG, "updatePinnedSection: update position " + pinnedSectionPosition);
272 |
273 | updatePinnedSectionLocation();
274 | } else {
275 | createPinnedSection(pinnedSectionPosition);
276 | }
277 | }
278 |
279 | /**
280 | * 更新悬挂视图的垂直位置
281 | */
282 | private void updatePinnedSectionLocation() {
283 |
284 | int nextSectionPosition = findNextSectionByPinnedSection();
285 | LinearLayoutManager layoutManager = (LinearLayoutManager) getLayoutManager();
286 | View nexSectionView = layoutManager.findViewByPosition(nextSectionPosition);
287 | if (nexSectionView == null) {
288 | Log.d(TAG, "updatePinnedSectionLocation: nextSectionPosition:" + nextSectionPosition);
289 | int firstCompletelyVisibleItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition();
290 | int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
291 | //在看不到下一个section时,保证绘制阴影高度为正常高度
292 | mSectionsDistanceY = firstVisibleItemPosition == firstCompletelyVisibleItemPosition ? 0 : mShadowHeight;
293 | return;
294 | }
295 |
296 | int cBottom = mPinnedSection.holder.itemView.getBottom();
297 | int nTop = nexSectionView.getTop();
298 |
299 | mSectionsDistanceY = nTop - cBottom;
300 | Log.d(TAG, "updatePinnedSectionLocation: distance=" + mSectionsDistanceY + " ,nTop=" + nTop + " ,cBottom=" + cBottom);
301 | if (mSectionsDistanceY < 0)
302 | mTranslateY = mSectionsDistanceY;
303 | else
304 | mTranslateY = 0;
305 |
306 | }
307 |
308 | /**
309 | * 移除pinnedSection并回收复用
310 | */
311 | private void removePinnedSection() {
312 | Log.d(TAG, "removePinnedSection: ");
313 | mSectionsDistanceY = 0;
314 | if (mPinnedSection != null) {
315 | mRecycleSection = mPinnedSection;
316 | mPinnedSection = null;
317 | }
318 | }
319 |
320 | /**
321 | * 找出悬挂的Section
322 | *
323 | * @param position
324 | * @return
325 | */
326 | private int findPinnedSection(int position) {
327 | Adapter adapter = (Adapter) getAdapter();
328 | int sectionPosition = adapter.findSectionPosition(position);
329 | return sectionPosition;
330 | }
331 |
332 | private int findNextSectionByPinnedSection() {
333 | Adapter adapter = (Adapter) getAdapter();
334 | int sectionPosition = adapter.findNextSectionPosition(mPinnedSection.position);
335 | return sectionPosition;
336 | }
337 |
338 | /**
339 | * 发生滚动时检查
340 | */
341 | private void checkOnScrolled() {
342 | LinearLayoutManager layoutManager = (LinearLayoutManager) getLayoutManager();
343 | int firstCompletelyVisibleItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition();
344 | int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
345 |
346 | RecyclerView.Adapter adapter = getAdapter();
347 | if (adapter instanceof Adapter) {
348 | updatePinnedSection(firstVisibleItemPosition, firstCompletelyVisibleItemPosition);
349 | } else
350 | throw new IllegalArgumentException("Does your adapter implement PinnedSectRecyclerView.Adapter?");
351 | }
352 |
353 | /**
354 | * Wrapper class for pinned section view and its position in the list.
355 | */
356 | static class PinnedSection {
357 | public ViewHolder holder;
358 | public int position;
359 | public long id;
360 | }
361 |
362 | public interface Adapter {
363 | boolean isPinnedSectionItem(int position);
364 |
365 | /**
366 | * 找出position所属的section位置
367 | *
368 | * @param position
369 | * @return
370 | */
371 | int findSectionPosition(int position);
372 |
373 | int findNextSectionPosition(int position);
374 |
375 | }
376 |
377 | public void setOnPinnedSectionTouchListener(OnPinnedSectionTouchListener onPinnedSectionTouchListener) {
378 | mOnPinnedSectionTouchListener = onPinnedSectionTouchListener;
379 | }
380 |
381 | public interface OnPinnedSectionTouchListener {
382 | void onClick(View pinnedItemView, int position);
383 |
384 | void onLongClick(View pinnedItemView, int position);
385 | }
386 |
387 | private void clearTouchTarget() {
388 | mTouchTarget = null;
389 | }
390 |
391 | private boolean isPinnedViewTouched(View view, float x, float y) {
392 | view.getHitRect(mTouchRect);
393 |
394 | // by taping top or bottom padding, the list performs on click on a border item.
395 | // we don't add top padding here to keep behavior consistent.
396 | mTouchRect.top += mTranslateY;
397 |
398 | mTouchRect.bottom += mTranslateY + getPaddingTop();
399 | mTouchRect.left += getPaddingLeft();
400 | mTouchRect.right -= getPaddingRight();
401 | return mTouchRect.contains((int) x, (int) y);
402 | }
403 |
404 | @Override
405 | public boolean dispatchTouchEvent(MotionEvent ev) {
406 |
407 | final float x = ev.getX();
408 | final float y = ev.getY();
409 | final int action = ev.getAction();
410 |
411 | if (action == MotionEvent.ACTION_DOWN
412 | && mTouchTarget == null
413 | && mPinnedSection != null
414 | && isPinnedViewTouched(mPinnedSection.holder.itemView, x, y)) {
415 |
416 | // user touched pinned view
417 | mTouchTarget = mPinnedSection.holder.itemView;
418 | }
419 |
420 | if (mTouchTarget != null) {
421 | boolean ret = mGestureDetector.onTouchEvent(ev);
422 | Log.d(TAG, "dispatchTouchEvent: ret=" + ret);
423 | switch (action) {
424 | case MotionEvent.ACTION_CANCEL:
425 | case MotionEvent.ACTION_UP:
426 | clearTouchTarget();
427 | // MotionEvent event = MotionEvent.obtain(ev);
428 | // event.setAction(MotionEvent.ACTION_CANCEL);
429 | super.dispatchTouchEvent(ev);
430 | // event.recycle();
431 | break;
432 | }
433 | return true;
434 | }
435 | return super.dispatchTouchEvent(ev);
436 | }
437 |
438 | @Override
439 | public void setAdapter(RecyclerView.Adapter adapter) {
440 | // assert adapter in debug mode
441 | if (adapter != null) {
442 | if (!(adapter instanceof Adapter))
443 | throw new IllegalArgumentException("Does your adapter implement PinnedSectionListAdapter?");
444 | if (adapter.getItemCount() < 2)
445 | throw new IllegalArgumentException("Does your adapter handle at least two types" +
446 | " of views in getViewTypeCount() method: items and sections?");
447 | }
448 |
449 | // unregister observer at old adapter and register on new one
450 | RecyclerView.Adapter oldAdapter = getAdapter();
451 | if (oldAdapter != null) oldAdapter.unregisterAdapterDataObserver(mAdapterDataObserver);
452 | if (adapter != null) adapter.registerAdapterDataObserver(mAdapterDataObserver);
453 |
454 | // destroy pinned shadow, if new adapter is not same as old one
455 | if (oldAdapter != adapter) removePinnedSection();
456 |
457 | super.setAdapter(adapter);
458 | }
459 |
460 | private final AdapterDataObserver mAdapterDataObserver = new AdapterDataObserver() {
461 | @Override
462 | public void onChanged() {
463 | removePinnedSection();
464 | }
465 |
466 | @Override
467 | public void onItemRangeChanged(int positionStart, int itemCount) {
468 | removePinnedSection();
469 | }
470 |
471 | @Override
472 | public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
473 | removePinnedSection();
474 | }
475 |
476 | @Override
477 | public void onItemRangeInserted(int positionStart, int itemCount) {
478 | removePinnedSection();
479 | }
480 |
481 | @Override
482 | public void onItemRangeRemoved(int positionStart, int itemCount) {
483 | removePinnedSection();
484 | }
485 |
486 | @Override
487 | public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
488 | removePinnedSection();
489 | }
490 | };
491 |
492 | }
493 |
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Library
3 |
4 |
--------------------------------------------------------------------------------
/library/src/test/java/com/vk/pinnedsectionrecyclerview/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.vk.pinnedsectionrecyclerview;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/local.properties:
--------------------------------------------------------------------------------
1 | ## This file is automatically generated by Android Studio.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must *NOT* be checked into Version Control Systems,
5 | # as it contains information specific to your local configuration.
6 | #
7 | # Location of the SDK. This is only used by Gradle.
8 | # For customization when using a Version Control System, please read the
9 | # header note.
10 | #Wed Feb 15 16:45:45 CST 2017
11 | ndk.dir=F\:\\VK\\work_space\\android_space\\sdk\\ndk-bundle
12 | sdk.dir=F\:\\VK\\work_space\\android_space\\sdk
13 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':example', ':library'
2 |
--------------------------------------------------------------------------------