├── .gitignore ├── README.md ├── art └── demo.gif ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── ehamutcu │ │ └── sectionpickersample │ │ ├── activity │ │ └── MainActivity.java │ │ ├── adapter │ │ └── CountriesRecyclerViewAdapter.java │ │ └── model │ │ ├── CountriesRecyclerViewModel.java │ │ └── Country.java │ └── res │ ├── drawable │ └── shape_indicator_background.xml │ ├── layout │ ├── activity_main.xml │ └── row_recyclerview_country.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── sectionpicker ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── ehamutcu │ │ └── sectionpicker │ │ ├── SectionPicker.java │ │ ├── helper │ │ └── AttrHelper.java │ │ └── util │ │ └── DisplayUtil.java │ └── res │ └── values │ ├── attrs.xml │ └── strings.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SectionPicker 2 | A Custom Android view for fast scroll with sections in lists. It uses [SectionIndexer](https://developer.android.com/reference/android/widget/SectionIndexer.html) android widget. It is nice to have in our lists with large size like contacts or a country list. 3 | 4 | ## Demo 5 | 6 | ![Demo](/art/demo.gif?raw=true) 7 | 8 | ## Download 9 | 10 | Add it in your root build.gradle at the end of repositories: 11 | 12 | ```gradle 13 | allprojects { 14 | repositories { 15 | maven { url 'https://jitpack.io' } 16 | } 17 | } 18 | ``` 19 | 20 | Add the dependency in your build.gradle in the app module: 21 | ```gradle 22 | dependencies { 23 | compile 'com.github.egemenhamutcu:SectionPicker:1.0.0' 24 | } 25 | ``` 26 | 27 | ## Usage 28 | 29 | Suppose we have a ```RecyclerView```. We need to implement ```SectionIndexer``` in our ```RecyclerView.Adapter```: 30 | 31 | ```java 32 | public class CountriesRecyclerViewAdapter extends RecyclerView.Adapter implements SectionIndexer { 33 | public static final int TYPE_COUNTRY = 0; 34 | public static final int TYPE_LETTER = 1; 35 | 36 | private List countries; 37 | ... 38 | } 39 | ``` 40 | 41 | ```TYPE_COUNTRY``` and ```TYPE_LETTER``` are view types. You can see it in demo. 42 | 43 | ```SectionIndexer``` will implement 3 methods: ```getSections(), getPositionForSection(int sectionIndex), getSectionForPosition(int position)```. We will need all of them: 44 | 45 | ```java 46 | @Override 47 | public Object[] getSections() { 48 | List sectionList = new ArrayList<>(); 49 | 50 | for (CountriesRecyclerViewModel country : countries) { 51 | if (country.getType() == TYPE_LETTER) { 52 | sectionList.add(country.getLetter()); 53 | } 54 | } 55 | 56 | return sectionList.toArray(); 57 | } 58 | 59 | @Override 60 | public int getPositionForSection(int sectionIndex) { 61 | for (int i = 0, size = countries.size(); i < size; i++) { 62 | CountriesRecyclerViewModel countriesRecyclerViewModel = countries.get(i); 63 | if (countriesRecyclerViewModel.getType() == TYPE_LETTER) { 64 | String sortStr = countriesRecyclerViewModel.getLetter(); 65 | char firstChar = sortStr.toUpperCase().charAt(0); 66 | if (firstChar == sectionIndex) { 67 | return i; 68 | } 69 | } 70 | } 71 | 72 | return -1; 73 | } 74 | 75 | @Override 76 | public int getSectionForPosition(int position) { 77 | int realSize = getItemCount(); 78 | if (position >= realSize) { 79 | position = realSize - 1; 80 | } 81 | 82 | CountriesRecyclerViewModel countriesRecyclerViewModel = countries.get(position); 83 | Object[] sectionArray = getSections(); 84 | 85 | String letter = ""; 86 | switch (countriesRecyclerViewModel.getType()) { 87 | case TYPE_COUNTRY: { 88 | Country country = countriesRecyclerViewModel.getCountry(); 89 | if (country != null) { 90 | letter = country.getName().substring(0, 1); 91 | } 92 | break; 93 | } 94 | case TYPE_LETTER: { 95 | letter = countriesRecyclerViewModel.getLetter(); 96 | break; 97 | } 98 | } 99 | 100 | for (int i = 0; i < sectionArray.length; i++) { 101 | if (sectionArray[i].toString().equals(letter)) { 102 | return i; 103 | } 104 | } 105 | return -1; 106 | } 107 | ``` 108 | 109 | - ```getSections()``` determines the section list which will show up at the right of the screen. Our sections are listed in ```countries``` with the view type of ```TYPE_LETTER```. 110 | 111 | - ```getPositionForSection(int sectionIndex)``` returns position of the section in the list. It uses char values to determine the right position of the section 112 | 113 | - ```getSectionForPosition(int position)``` returns section position in the list. We need to find the right section by using position parameter 114 | 115 | We can add ```SectionPicker``` in our xml layout like this: 116 | 117 | ```xml 118 | 128 | ``` 129 | 130 | ```app``` attributes are optional: 131 | - ```textColor, textSize```: You can style text attributes with these 132 | - ```chosenColor, chosenStyle```: When you slide your finger on sections, the chosen one can be styled by these attributes 133 | 134 | Now we initialize the ```SectionPicker``` like this: 135 | 136 | ```java 137 | private void initSectionPicker() { 138 | Object[] sectionsAsObject = adapter.getSections(); 139 | String[] sections = Arrays.copyOf(sectionsAsObject, sectionsAsObject.length, String[].class); 140 | 141 | sectionPickerCountries.setTextViewIndicator(textViewSection); 142 | sectionPickerCountries.setSections(sections); 143 | sectionPickerCountries.setOnTouchingLetterChangedListener(new SectionPicker.OnTouchingLetterChangedListener() { 144 | @Override 145 | public void onTouchingLetterChanged(String s) { 146 | int position = adapter.getPositionForSection(s.charAt(0)); 147 | 148 | if (position != -1) { 149 | linearLayoutManager.scrollToPositionWithOffset(position, 0); 150 | } 151 | } 152 | }); 153 | } 154 | ``` 155 | 156 | You may be confused of ```setTextViewIndicator(textViewSection)```. This method is optional. You can add any ```TextView``` to the ```SectionPicker```. It will show up when you slide your finger on sections. Please see demo. 157 | 158 | To bind ```SectionPicker``` and ```RecyclerView``` we get the section array from adapter, and call ```setSections(sections)``` in ```SectionPicker```. 159 | 160 | To enable fast scroll we need to implement ```SectionPicker.OnTouchingLetterChangedListener()```. It will give us a string containing the section that we are touching. We will use the string to find and scroll to the ```RecyclerView``` position of the chosen section. 161 | 162 | ## License 163 | ``` 164 | Copyright 2017 Egemen Hamutçu 165 | 166 | Licensed under the Apache License, Version 2.0 (the "License"); 167 | you may not use this file except in compliance with the License. 168 | You may obtain a copy of the License at 169 | 170 | http://www.apache.org/licenses/LICENSE-2.0 171 | 172 | Unless required by applicable law or agreed to in writing, software 173 | distributed under the License is distributed on an "AS IS" BASIS, 174 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 175 | See the License for the specific language governing permissions and 176 | limitations under the License. 177 | ``` 178 | -------------------------------------------------------------------------------- /art/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nightwolf738/SectionPicker/eed43c3ec84c91981dfb73cf4f4397e59e1a2441/art/demo.gif -------------------------------------------------------------------------------- /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.3.3' 9 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' 10 | 11 | // NOTE: Do not place your application dependencies here; 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.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/nightwolf738/SectionPicker/eed43c3ec84c91981dfb73cf4f4397e59e1a2441/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Jun 02 13:09:49 EEST 2017 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-3.3-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 | -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.3" 6 | defaultConfig { 7 | applicationId "com.ehamutcu.sectionpickesample" 8 | minSdkVersion 16 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(dir: 'libs', include: ['*.jar']) 24 | compile project(':sectionpicker') 25 | compile 'com.android.support:appcompat-v7:25.3.1' 26 | compile "com.android.support:recyclerview-v7:25.3.1" 27 | compile 'com.jwang123.flagkit:flagkit:1.0' 28 | } 29 | -------------------------------------------------------------------------------- /sample/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 C:\Users\EgemenH\AppData\Local\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 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /sample/src/main/java/com/ehamutcu/sectionpickersample/activity/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.ehamutcu.sectionpickersample.activity; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.support.v7.widget.LinearLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.text.TextUtils; 8 | import android.view.View; 9 | import android.widget.TextView; 10 | import android.widget.Toast; 11 | 12 | import com.ehamutcu.sectionpicker.SectionPicker; 13 | import com.ehamutcu.sectionpickersample.adapter.CountriesRecyclerViewAdapter; 14 | import com.ehamutcu.sectionpickersample.model.CountriesRecyclerViewModel; 15 | import com.ehamutcu.sectionpickersample.model.Country; 16 | import com.ehamutcu.sectionpickesample.R; 17 | 18 | import java.util.ArrayList; 19 | import java.util.Arrays; 20 | import java.util.List; 21 | 22 | public class MainActivity extends AppCompatActivity implements CountriesRecyclerViewAdapter.RowClickListener { 23 | 24 | private List countriesRecyclerViewModels; 25 | private RecyclerView recyclerViewCountries; 26 | private SectionPicker sectionPickerCountries; 27 | private TextView textViewSection; 28 | private CountriesRecyclerViewAdapter adapter; 29 | private LinearLayoutManager linearLayoutManager; 30 | 31 | @Override 32 | protected void onCreate(Bundle savedInstanceState) { 33 | super.onCreate(savedInstanceState); 34 | setContentView(R.layout.activity_main); 35 | 36 | textViewSection = (TextView) findViewById(R.id.textview_section); 37 | sectionPickerCountries = (SectionPicker) findViewById(R.id.sectionpicker_countries); 38 | recyclerViewCountries = (RecyclerView) findViewById(R.id.recyclerview_countries); 39 | 40 | setRecyclerViewLayoutManager(); 41 | populateRecyclerView(); 42 | initSectionPicker(); 43 | } 44 | 45 | public void setRecyclerViewLayoutManager() { 46 | linearLayoutManager = new LinearLayoutManager(this); 47 | recyclerViewCountries.setLayoutManager(linearLayoutManager); 48 | } 49 | 50 | private void populateRecyclerView() { 51 | List countries = new ArrayList<>(); 52 | 53 | // TODO DUMMY 54 | countries.add(new Country("ATurkey", "TR")); 55 | countries.add(new Country("ATurkey", "TR")); 56 | countries.add(new Country("ATurkey", "TR")); 57 | countries.add(new Country("ATurkey", "TR")); 58 | countries.add(new Country("ATurkey", "TR")); 59 | countries.add(new Country("ATurkey", "TR")); 60 | countries.add(new Country("ATurkey", "TR")); 61 | countries.add(new Country("ATurkey", "TR")); 62 | 63 | countries.add(new Country("BTurkey", "TR")); 64 | countries.add(new Country("BTurkey", "TR")); 65 | countries.add(new Country("BTurkey", "TR")); 66 | countries.add(new Country("BTurkey", "TR")); 67 | countries.add(new Country("BTurkey", "TR")); 68 | countries.add(new Country("BTurkey", "TR")); 69 | countries.add(new Country("BTurkey", "TR")); 70 | countries.add(new Country("BTurkey", "TR")); 71 | countries.add(new Country("BTurkey", "TR")); 72 | countries.add(new Country("BTurkey", "TR")); 73 | 74 | countries.add(new Country("CTurkey", "TR")); 75 | countries.add(new Country("CTurkey", "TR")); 76 | countries.add(new Country("CTurkey", "TR")); 77 | countries.add(new Country("CTurkey", "TR")); 78 | countries.add(new Country("CTurkey", "TR")); 79 | countries.add(new Country("CTurkey", "TR")); 80 | countries.add(new Country("CTurkey", "TR")); 81 | countries.add(new Country("CTurkey", "TR")); 82 | 83 | countries.add(new Country("DTurkey", "TR")); 84 | countries.add(new Country("DTurkey", "TR")); 85 | countries.add(new Country("DTurkey", "TR")); 86 | countries.add(new Country("DTurkey", "TR")); 87 | countries.add(new Country("DTurkey", "TR")); 88 | countries.add(new Country("DTurkey", "TR")); 89 | countries.add(new Country("DTurkey", "TR")); 90 | countries.add(new Country("DTurkey", "TR")); 91 | 92 | countries.add(new Country("ETurkey", "TR")); 93 | countries.add(new Country("ETurkey", "TR")); 94 | countries.add(new Country("ETurkey", "TR")); 95 | countries.add(new Country("ETurkey", "TR")); 96 | countries.add(new Country("ETurkey", "TR")); 97 | countries.add(new Country("ETurkey", "TR")); 98 | countries.add(new Country("ETurkey", "TR")); 99 | countries.add(new Country("ETurkey", "TR")); 100 | 101 | countries.add(new Country("FTurkey", "TR")); 102 | countries.add(new Country("FTurkey", "TR")); 103 | countries.add(new Country("FTurkey", "TR")); 104 | countries.add(new Country("FTurkey", "TR")); 105 | countries.add(new Country("FTurkey", "TR")); 106 | countries.add(new Country("FTurkey", "TR")); 107 | countries.add(new Country("FTurkey", "TR")); 108 | countries.add(new Country("FTurkey", "TR")); 109 | 110 | countries.add(new Country("GTurkey", "TR")); 111 | countries.add(new Country("GTurkey", "TR")); 112 | countries.add(new Country("GTurkey", "TR")); 113 | countries.add(new Country("GTurkey", "TR")); 114 | countries.add(new Country("GTurkey", "TR")); 115 | countries.add(new Country("GTurkey", "TR")); 116 | countries.add(new Country("GTurkey", "TR")); 117 | countries.add(new Country("GTurkey", "TR")); 118 | 119 | countries.add(new Country("HTurkey", "TR")); 120 | countries.add(new Country("HTurkey", "TR")); 121 | countries.add(new Country("HTurkey", "TR")); 122 | countries.add(new Country("HTurkey", "TR")); 123 | countries.add(new Country("HTurkey", "TR")); 124 | countries.add(new Country("HTurkey", "TR")); 125 | countries.add(new Country("HTurkey", "TR")); 126 | countries.add(new Country("HTurkey", "TR")); 127 | 128 | countries.add(new Country("ITurkey", "TR")); 129 | countries.add(new Country("ITurkey", "TR")); 130 | countries.add(new Country("ITurkey", "TR")); 131 | countries.add(new Country("ITurkey", "TR")); 132 | countries.add(new Country("ITurkey", "TR")); 133 | countries.add(new Country("ITurkey", "TR")); 134 | countries.add(new Country("ITurkey", "TR")); 135 | countries.add(new Country("ITurkey", "TR")); 136 | 137 | countries.add(new Country("JTurkey", "TR")); 138 | countries.add(new Country("JTurkey", "TR")); 139 | countries.add(new Country("JTurkey", "TR")); 140 | countries.add(new Country("JTurkey", "TR")); 141 | countries.add(new Country("JTurkey", "TR")); 142 | countries.add(new Country("JTurkey", "TR")); 143 | countries.add(new Country("JTurkey", "TR")); 144 | countries.add(new Country("JTurkey", "TR")); 145 | 146 | countries.add(new Country("KTurkey", "TR")); 147 | countries.add(new Country("KTurkey", "TR")); 148 | countries.add(new Country("KTurkey", "TR")); 149 | countries.add(new Country("KTurkey", "TR")); 150 | countries.add(new Country("KTurkey", "TR")); 151 | countries.add(new Country("KTurkey", "TR")); 152 | countries.add(new Country("KTurkey", "TR")); 153 | countries.add(new Country("KTurkey", "TR")); 154 | 155 | countries.add(new Country("LTurkey", "TR")); 156 | countries.add(new Country("LTurkey", "TR")); 157 | countries.add(new Country("LTurkey", "TR")); 158 | countries.add(new Country("LTurkey", "TR")); 159 | countries.add(new Country("LTurkey", "TR")); 160 | countries.add(new Country("LTurkey", "TR")); 161 | countries.add(new Country("LTurkey", "TR")); 162 | countries.add(new Country("LTurkey", "TR")); 163 | 164 | countries.add(new Country("MTurkey", "TR")); 165 | countries.add(new Country("MTurkey", "TR")); 166 | countries.add(new Country("MTurkey", "TR")); 167 | countries.add(new Country("MTurkey", "TR")); 168 | countries.add(new Country("MTurkey", "TR")); 169 | countries.add(new Country("MTurkey", "TR")); 170 | countries.add(new Country("MTurkey", "TR")); 171 | countries.add(new Country("MTurkey", "TR")); 172 | 173 | countries.add(new Country("NTurkey", "TR")); 174 | countries.add(new Country("NTurkey", "TR")); 175 | countries.add(new Country("NTurkey", "TR")); 176 | countries.add(new Country("NTurkey", "TR")); 177 | countries.add(new Country("NTurkey", "TR")); 178 | countries.add(new Country("NTurkey", "TR")); 179 | countries.add(new Country("NTurkey", "TR")); 180 | countries.add(new Country("NTurkey", "TR")); 181 | 182 | countries.add(new Country("OTurkey", "TR")); 183 | countries.add(new Country("OTurkey", "TR")); 184 | countries.add(new Country("OTurkey", "TR")); 185 | countries.add(new Country("OTurkey", "TR")); 186 | countries.add(new Country("OTurkey", "TR")); 187 | countries.add(new Country("OTurkey", "TR")); 188 | countries.add(new Country("OTurkey", "TR")); 189 | countries.add(new Country("OTurkey", "TR")); 190 | 191 | countries.add(new Country("PTurkey", "TR")); 192 | countries.add(new Country("PTurkey", "TR")); 193 | countries.add(new Country("PTurkey", "TR")); 194 | countries.add(new Country("PTurkey", "TR")); 195 | countries.add(new Country("PTurkey", "TR")); 196 | countries.add(new Country("PTurkey", "TR")); 197 | countries.add(new Country("PTurkey", "TR")); 198 | countries.add(new Country("PTurkey", "TR")); 199 | 200 | countries.add(new Country("RTurkey", "TR")); 201 | countries.add(new Country("RTurkey", "TR")); 202 | countries.add(new Country("RTurkey", "TR")); 203 | countries.add(new Country("RTurkey", "TR")); 204 | countries.add(new Country("RTurkey", "TR")); 205 | countries.add(new Country("RTurkey", "TR")); 206 | countries.add(new Country("RTurkey", "TR")); 207 | countries.add(new Country("RTurkey", "TR")); 208 | 209 | countries.add(new Country("STurkey", "TR")); 210 | countries.add(new Country("STurkey", "TR")); 211 | countries.add(new Country("STurkey", "TR")); 212 | countries.add(new Country("STurkey", "TR")); 213 | countries.add(new Country("STurkey", "TR")); 214 | countries.add(new Country("STurkey", "TR")); 215 | countries.add(new Country("STurkey", "TR")); 216 | countries.add(new Country("STurkey", "TR")); 217 | 218 | countries.add(new Country("Turkey", "TR")); 219 | countries.add(new Country("Turkey", "TR")); 220 | countries.add(new Country("Turkey", "TR")); 221 | countries.add(new Country("Turkey", "TR")); 222 | countries.add(new Country("Turkey", "TR")); 223 | countries.add(new Country("Turkey", "TR")); 224 | countries.add(new Country("Turkey", "TR")); 225 | countries.add(new Country("Turkey", "TR")); 226 | 227 | countries.add(new Country("UTurkey", "TR")); 228 | countries.add(new Country("UTurkey", "TR")); 229 | countries.add(new Country("UTurkey", "TR")); 230 | countries.add(new Country("UTurkey", "TR")); 231 | countries.add(new Country("UTurkey", "TR")); 232 | countries.add(new Country("UTurkey", "TR")); 233 | countries.add(new Country("UTurkey", "TR")); 234 | countries.add(new Country("UTurkey", "TR")); 235 | 236 | countries.add(new Country("VTurkey", "TR")); 237 | countries.add(new Country("VTurkey", "TR")); 238 | countries.add(new Country("VTurkey", "TR")); 239 | countries.add(new Country("VTurkey", "TR")); 240 | countries.add(new Country("VTurkey", "TR")); 241 | countries.add(new Country("VTurkey", "TR")); 242 | countries.add(new Country("VTurkey", "TR")); 243 | countries.add(new Country("VTurkey", "TR")); 244 | 245 | countries.add(new Country("YTurkey", "TR")); 246 | countries.add(new Country("YTurkey", "TR")); 247 | countries.add(new Country("YTurkey", "TR")); 248 | countries.add(new Country("YTurkey", "TR")); 249 | countries.add(new Country("YTurkey", "TR")); 250 | countries.add(new Country("YTurkey", "TR")); 251 | countries.add(new Country("YTurkey", "TR")); 252 | countries.add(new Country("YTurkey", "TR")); 253 | 254 | countries.add(new Country("ZTurkey", "TR")); 255 | countries.add(new Country("ZTurkey", "TR")); 256 | countries.add(new Country("ZTurkey", "TR")); 257 | countries.add(new Country("ZTurkey", "TR")); 258 | countries.add(new Country("ZTurkey", "TR")); 259 | countries.add(new Country("ZTurkey", "TR")); 260 | countries.add(new Country("ZTurkey", "TR")); 261 | countries.add(new Country("ZTurkey", "TR")); 262 | // TODO DUMMY END 263 | 264 | adapter = new CountriesRecyclerViewAdapter(transformCountriesForRecyclerView(countries), this, this); 265 | recyclerViewCountries.setAdapter(adapter); 266 | } 267 | 268 | private void initSectionPicker() { 269 | Object[] sectionsAsObject = adapter.getSections(); 270 | String[] sections = Arrays.copyOf(sectionsAsObject, sectionsAsObject.length, String[].class); 271 | 272 | sectionPickerCountries.setTextViewIndicator(textViewSection); 273 | sectionPickerCountries.setSections(sections); 274 | sectionPickerCountries.setOnTouchingLetterChangedListener(new SectionPicker.OnTouchingLetterChangedListener() { 275 | @Override 276 | public void onTouchingLetterChanged(String s) { 277 | int position = adapter.getPositionForSection(s.charAt(0)); 278 | 279 | if (position != -1) { 280 | linearLayoutManager.scrollToPositionWithOffset(position, 0); 281 | } 282 | } 283 | }); 284 | } 285 | 286 | private List transformCountriesForRecyclerView(List countries) { 287 | countriesRecyclerViewModels = new ArrayList<>(); 288 | if ((countries != null) && !countries.isEmpty()) { 289 | String letter = ""; 290 | for (Country country : countries) { 291 | String countryLetter = country.getName().substring(0, 1); 292 | if (TextUtils.isEmpty(letter) || !letter.equals(countryLetter)) { 293 | countriesRecyclerViewModels.add(new CountriesRecyclerViewModel(null, countryLetter, CountriesRecyclerViewAdapter.TYPE_LETTER)); 294 | letter = countryLetter; 295 | } 296 | countriesRecyclerViewModels.add(new CountriesRecyclerViewModel(country, null, CountriesRecyclerViewAdapter.TYPE_COUNTRY)); 297 | } 298 | } 299 | return countriesRecyclerViewModels; 300 | } 301 | 302 | @Override 303 | public void onRowClick(View view, int position) { 304 | CountriesRecyclerViewModel countriesRecyclerViewModel = countriesRecyclerViewModels.get(position); 305 | Country country = countriesRecyclerViewModel.getCountry(); 306 | if (country != null) { 307 | Toast.makeText(this, country.getName(), Toast.LENGTH_SHORT).show(); 308 | } 309 | } 310 | } 311 | -------------------------------------------------------------------------------- /sample/src/main/java/com/ehamutcu/sectionpickersample/adapter/CountriesRecyclerViewAdapter.java: -------------------------------------------------------------------------------- 1 | package com.ehamutcu.sectionpickersample.adapter; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.SectionIndexer; 10 | import android.widget.TextView; 11 | 12 | import com.ehamutcu.sectionpickersample.model.CountriesRecyclerViewModel; 13 | import com.ehamutcu.sectionpickersample.model.Country; 14 | import com.ehamutcu.sectionpickesample.R; 15 | import com.jwang123.flagkit.FlagKit; 16 | 17 | import java.util.ArrayList; 18 | import java.util.List; 19 | 20 | /** 21 | * Created by EgemenH on 02.06.2017. 22 | */ 23 | 24 | public class CountriesRecyclerViewAdapter extends RecyclerView.Adapter implements SectionIndexer { 25 | public static final int TYPE_COUNTRY = 0; 26 | public static final int TYPE_LETTER = 1; 27 | 28 | private List countries; 29 | private Context context; 30 | private final RowClickListener rowClickListener; 31 | 32 | public CountriesRecyclerViewAdapter(@NonNull List countries, @NonNull Context context, @NonNull RowClickListener rowClickListener) { 33 | this.countries = countries; 34 | this.context = context; 35 | this.rowClickListener = rowClickListener; 36 | } 37 | 38 | @Override 39 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 40 | Context context = parent.getContext(); 41 | switch (viewType) { 42 | case TYPE_COUNTRY: { 43 | View view = LayoutInflater.from(context).inflate(R.layout.row_recyclerview_country, parent, false); 44 | final CountryViewHolder viewHolder = new CountryViewHolder(view); 45 | 46 | viewHolder.textViewCountry.setOnClickListener(new View.OnClickListener() { 47 | @Override 48 | public void onClick(View v) { 49 | int position = viewHolder.getAdapterPosition(); 50 | if (position != RecyclerView.NO_POSITION) { 51 | rowClickListener.onRowClick(v, position); 52 | } 53 | } 54 | }); 55 | 56 | return viewHolder; 57 | } 58 | case TYPE_LETTER: { 59 | View view = LayoutInflater.from(context).inflate(R.layout.row_recyclerview_country, parent, false); 60 | return new LetterViewHolder(view); 61 | } 62 | } 63 | return null; 64 | } 65 | 66 | @Override 67 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 68 | CountriesRecyclerViewModel countriesRecyclerViewModel = countries.get(position); 69 | if (countriesRecyclerViewModel != null) { 70 | switch (getItemViewType(position)) { 71 | case TYPE_COUNTRY: { 72 | Country country = countriesRecyclerViewModel.getCountry(); 73 | if (country != null) { 74 | ((CountryViewHolder) holder).bindTo(countriesRecyclerViewModel.getCountry(), context); 75 | } 76 | break; 77 | } 78 | case TYPE_LETTER: { 79 | ((LetterViewHolder) holder).bindTo(countriesRecyclerViewModel.getLetter()); 80 | break; 81 | } 82 | } 83 | } 84 | } 85 | 86 | @Override 87 | public int getItemCount() { 88 | return (countries != null) ? countries.size() : 0; 89 | } 90 | 91 | @Override 92 | public int getItemViewType(int position) { 93 | CountriesRecyclerViewModel country = countries.get(position); 94 | if (country != null) { 95 | return country.getType(); 96 | } 97 | return super.getItemViewType(position); 98 | } 99 | 100 | @Override 101 | public Object[] getSections() { 102 | List sectionList = new ArrayList<>(); 103 | 104 | for (CountriesRecyclerViewModel country : countries) { 105 | if (country.getType() == TYPE_LETTER) { 106 | sectionList.add(country.getLetter()); 107 | } 108 | } 109 | 110 | return sectionList.toArray(); 111 | } 112 | 113 | @Override 114 | public int getPositionForSection(int sectionIndex) { 115 | for (int i = 0, size = countries.size(); i < size; i++) { 116 | CountriesRecyclerViewModel countriesRecyclerViewModel = countries.get(i); 117 | if (countriesRecyclerViewModel.getType() == TYPE_LETTER) { 118 | String sortStr = countriesRecyclerViewModel.getLetter(); 119 | char firstChar = sortStr.toUpperCase().charAt(0); 120 | if (firstChar == sectionIndex) { 121 | return i; 122 | } 123 | } 124 | } 125 | 126 | return -1; 127 | } 128 | 129 | @Override 130 | public int getSectionForPosition(int position) { 131 | int realSize = getItemCount(); 132 | if (position >= realSize) { 133 | position = realSize - 1; 134 | } 135 | 136 | CountriesRecyclerViewModel countriesRecyclerViewModel = countries.get(position); 137 | Object[] sectionArray = getSections(); 138 | 139 | String letter = ""; 140 | switch (countriesRecyclerViewModel.getType()) { 141 | case TYPE_COUNTRY: { 142 | Country country = countriesRecyclerViewModel.getCountry(); 143 | if (country != null) { 144 | letter = country.getName().substring(0, 1); 145 | } 146 | break; 147 | } 148 | case TYPE_LETTER: { 149 | letter = countriesRecyclerViewModel.getLetter(); 150 | break; 151 | } 152 | } 153 | 154 | for (int i = 0; i < sectionArray.length; i++) { 155 | if (sectionArray[i].toString().equals(letter)) { 156 | return i; 157 | } 158 | } 159 | return -1; 160 | } 161 | 162 | public static class CountryViewHolder extends RecyclerView.ViewHolder { 163 | 164 | TextView textViewCountry; 165 | 166 | public CountryViewHolder(View itemView) { 167 | super(itemView); 168 | textViewCountry = (TextView) itemView.findViewById(R.id.textview_country); 169 | } 170 | 171 | public void bindTo(@NonNull Country country, @NonNull Context context) { 172 | textViewCountry.setText(country.getName()); 173 | textViewCountry.setCompoundDrawablesWithIntrinsicBounds(FlagKit.drawableWithFlag(context, country.getCountryCode().toLowerCase()), null, null, null); 174 | } 175 | } 176 | 177 | public static class LetterViewHolder extends RecyclerView.ViewHolder { 178 | 179 | TextView textViewLetter; 180 | 181 | public LetterViewHolder(View itemView) { 182 | super(itemView); 183 | textViewLetter = (TextView) itemView.findViewById(R.id.textview_country); 184 | } 185 | 186 | public void bindTo(@NonNull String letter) { 187 | textViewLetter.setText(letter); 188 | } 189 | } 190 | 191 | public interface RowClickListener { 192 | void onRowClick(View view, int position); 193 | } 194 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/ehamutcu/sectionpickersample/model/CountriesRecyclerViewModel.java: -------------------------------------------------------------------------------- 1 | package com.ehamutcu.sectionpickersample.model; 2 | 3 | import android.support.annotation.Nullable; 4 | 5 | /** 6 | * Created by EgemenH on 02.06.2017. 7 | */ 8 | 9 | public class CountriesRecyclerViewModel { 10 | private Country country; 11 | private String letter; 12 | private int type; 13 | 14 | public CountriesRecyclerViewModel(@Nullable Country country, String letter, int type) { 15 | this.country = country; 16 | this.letter = letter; 17 | this.type = type; 18 | } 19 | 20 | @Nullable 21 | public Country getCountry() { 22 | return country; 23 | } 24 | 25 | public String getLetter() { 26 | return letter; 27 | } 28 | 29 | public int getType() { 30 | return type; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /sample/src/main/java/com/ehamutcu/sectionpickersample/model/Country.java: -------------------------------------------------------------------------------- 1 | package com.ehamutcu.sectionpickersample.model; 2 | 3 | import android.os.Parcel; 4 | import android.os.Parcelable; 5 | 6 | /** 7 | * Created by EgemenH on 02.06.2017. 8 | */ 9 | 10 | public class Country implements Parcelable { 11 | private String name; 12 | private String countryCode; 13 | 14 | public Country(String name, String countryCode) { 15 | this.name = name; 16 | this.countryCode = countryCode; 17 | } 18 | 19 | public String getName() { 20 | return name; 21 | } 22 | 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | 27 | public String getCountryCode() { 28 | return countryCode; 29 | } 30 | 31 | public void setCountryCode(String countryCode) { 32 | this.countryCode = countryCode; 33 | } 34 | 35 | 36 | @Override 37 | public int describeContents() { 38 | return 0; 39 | } 40 | 41 | @Override 42 | public void writeToParcel(Parcel dest, int flags) { 43 | dest.writeString(this.name); 44 | dest.writeString(this.countryCode); 45 | } 46 | 47 | protected Country(Parcel in) { 48 | this.name = in.readString(); 49 | this.countryCode = in.readString(); 50 | } 51 | 52 | public static final Creator CREATOR = new Creator() { 53 | @Override 54 | public Country createFromParcel(Parcel source) { 55 | return new Country(source); 56 | } 57 | 58 | @Override 59 | public Country[] newArray(int size) { 60 | return new Country[size]; 61 | } 62 | }; 63 | } 64 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/shape_indicator_background.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 16 | 17 | 27 | 28 | 40 | 41 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/row_recyclerview_country.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nightwolf738/SectionPicker/eed43c3ec84c91981dfb73cf4f4397e59e1a2441/sample/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nightwolf738/SectionPicker/eed43c3ec84c91981dfb73cf4f4397e59e1a2441/sample/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nightwolf738/SectionPicker/eed43c3ec84c91981dfb73cf4f4397e59e1a2441/sample/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nightwolf738/SectionPicker/eed43c3ec84c91981dfb73cf4f4397e59e1a2441/sample/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nightwolf738/SectionPicker/eed43c3ec84c91981dfb73cf4f4397e59e1a2441/sample/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nightwolf738/SectionPicker/eed43c3ec84c91981dfb73cf4f4397e59e1a2441/sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nightwolf738/SectionPicker/eed43c3ec84c91981dfb73cf4f4397e59e1a2441/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nightwolf738/SectionPicker/eed43c3ec84c91981dfb73cf4f4397e59e1a2441/sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nightwolf738/SectionPicker/eed43c3ec84c91981dfb73cf4f4397e59e1a2441/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nightwolf738/SectionPicker/eed43c3ec84c91981dfb73cf4f4397e59e1a2441/sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #A50404 4 | #630606 5 | #A50404 6 | 7 | #80A50404 8 | 9 | -------------------------------------------------------------------------------- /sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | SectionPickerSample 3 | 4 | -------------------------------------------------------------------------------- /sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /sectionpicker/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sectionpicker/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.github.dcendents.android-maven' 3 | 4 | group='com.github.egemenhamutcu' 5 | 6 | ext { 7 | PUBLISH_GROUP_ID = 'com.ehamutcu.sectionpicker' 8 | PUBLISH_ARTIFACT_ID = 'sectionpicker' 9 | PUBLISH_VERSION = '1.0.0' 10 | } 11 | 12 | android { 13 | compileSdkVersion 26 14 | buildToolsVersion "26.0.2" 15 | 16 | defaultConfig { 17 | minSdkVersion 16 18 | targetSdkVersion 26 19 | versionCode 1 20 | versionName "1.0.0" 21 | } 22 | buildTypes { 23 | release { 24 | minifyEnabled false 25 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 26 | } 27 | } 28 | } 29 | 30 | dependencies { 31 | compile fileTree(dir: 'libs', include: ['*.jar']) 32 | } -------------------------------------------------------------------------------- /sectionpicker/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 C:\Users\EgemenH\AppData\Local\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 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /sectionpicker/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /sectionpicker/src/main/java/com/ehamutcu/sectionpicker/SectionPicker.java: -------------------------------------------------------------------------------- 1 | package com.ehamutcu.sectionpicker; 2 | 3 | import android.content.Context; 4 | import android.content.res.ColorStateList; 5 | import android.graphics.Canvas; 6 | import android.graphics.Color; 7 | import android.graphics.Paint; 8 | import android.graphics.Typeface; 9 | import android.util.AttributeSet; 10 | import android.view.MotionEvent; 11 | import android.view.View; 12 | import android.widget.TextView; 13 | 14 | import com.ehamutcu.sectionpicker.helper.AttrHelper; 15 | 16 | /** 17 | * Created by EgemenH on 02.06.2017. 18 | */ 19 | 20 | public class SectionPicker extends View { 21 | 22 | public String[] sections = {"A", "B", "C", "D", "E", "F", "G", "H", "I", 23 | "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", 24 | "W", "X", "Y", "Z"}; 25 | private TextView textViewIndicator; 26 | private int fontSize; 27 | private ColorStateList color; 28 | private Typeface typeFace; 29 | private ColorStateList chosenColor; 30 | private Typeface chosenTypeFace; 31 | private boolean indicatorEnabled = true; 32 | private OnTouchingLetterChangedListener onTouchingLetterChangedListener; 33 | private int chosenIndex = -1; 34 | private Paint paint = new Paint(); 35 | 36 | 37 | public SectionPicker(Context context, AttributeSet attrs, int defStyle) { 38 | super(context, attrs, defStyle); 39 | init(attrs); 40 | } 41 | 42 | public SectionPicker(Context context, AttributeSet attrs) { 43 | super(context, attrs); 44 | init(attrs); 45 | } 46 | 47 | public SectionPicker(Context context) { 48 | super(context); 49 | } 50 | 51 | private void init(AttributeSet attrs) { 52 | if (attrs != null) { 53 | setAttrs(attrs); 54 | } 55 | } 56 | 57 | protected void setAttrs(AttributeSet attrs) { 58 | Context context = getContext(); 59 | 60 | int textSize = AttrHelper.getFontSize(context, attrs, R.styleable.SectionPicker, R.styleable.SectionPicker_textSize); 61 | setFontSize(textSize); 62 | 63 | ColorStateList color = AttrHelper.getFontColor(context, attrs, R.styleable.SectionPicker, R.styleable.SectionPicker_textColor); 64 | setColor(color); 65 | 66 | int textStyle = AttrHelper.getFontStyle(context, attrs, R.styleable.SectionPicker, R.styleable.SectionPicker_textStyle); 67 | setTypeFace(Typeface.create(Typeface.DEFAULT, textStyle)); 68 | 69 | ColorStateList chosenColor = AttrHelper.getFontColor(context, attrs, R.styleable.SectionPicker, R.styleable.SectionPicker_chosenColor); 70 | setChosenColor(chosenColor); 71 | 72 | int chosenStyle = AttrHelper.getFontStyle(context, attrs, R.styleable.SectionPicker, R.styleable.SectionPicker_chosenStyle); 73 | setChosenTypeFace(Typeface.create(Typeface.DEFAULT, chosenStyle)); 74 | 75 | boolean isIndicatorEnabled = AttrHelper.getIndicatorEnabled(context, attrs, R.styleable.SectionPicker, R.styleable.SectionPicker_indicatorEnabled); 76 | setIndicatorEnabled(isIndicatorEnabled); 77 | } 78 | 79 | protected void onDraw(Canvas canvas) { 80 | super.onDraw(canvas); 81 | int height = getHeight(); 82 | int width = getWidth(); 83 | int singleHeight = height / sections.length; 84 | 85 | for (int i = 0; i < sections.length; i++) { 86 | paint.setAntiAlias(true); 87 | paint.setTextSize(fontSize); 88 | 89 | if (i == chosenIndex) { 90 | paintText(chosenColor, chosenTypeFace); 91 | } else { 92 | paintText(color, typeFace); 93 | } 94 | 95 | float xPos = width / 2 - paint.measureText(sections[i]) / 2; 96 | float yPos = singleHeight * i + singleHeight; 97 | canvas.drawText(sections[i], xPos, yPos, paint); 98 | paint.reset(); 99 | } 100 | 101 | } 102 | 103 | private void paintText(ColorStateList colorStateList, Typeface typeFace) { 104 | paint.setColor((colorStateList != null) ? colorStateList.getDefaultColor() : Color.BLACK); 105 | paint.setTypeface((typeFace != null) ? typeFace : Typeface.create(Typeface.DEFAULT, Typeface.NORMAL)); 106 | } 107 | 108 | @Override 109 | public boolean dispatchTouchEvent(MotionEvent event) { 110 | final int action = event.getAction(); 111 | final float y = event.getY(); 112 | final int oldChoose = chosenIndex; 113 | final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener; 114 | final int c = (int) (y / getHeight() * sections.length); 115 | 116 | switch (action) { 117 | case MotionEvent.ACTION_UP: { 118 | chosenIndex = -1; 119 | invalidate(); 120 | if (indicatorEnabled && (textViewIndicator != null)) { 121 | textViewIndicator.setVisibility(View.INVISIBLE); 122 | } 123 | break; 124 | } 125 | 126 | default: { 127 | if (oldChoose != c) { 128 | if (c >= 0 && c < sections.length) { 129 | if (listener != null) { 130 | listener.onTouchingLetterChanged(sections[c]); 131 | } 132 | if (indicatorEnabled && (textViewIndicator != null)) { 133 | textViewIndicator.setText(sections[c]); 134 | textViewIndicator.setVisibility(View.VISIBLE); 135 | } 136 | 137 | chosenIndex = c; 138 | invalidate(); 139 | } 140 | } 141 | break; 142 | } 143 | } 144 | return true; 145 | } 146 | 147 | public TextView getTextViewIndicator() { 148 | return textViewIndicator; 149 | } 150 | 151 | public void setTextViewIndicator(TextView textViewIndicator) { 152 | this.textViewIndicator = textViewIndicator; 153 | } 154 | 155 | public Typeface getTypeFace() { 156 | return typeFace; 157 | } 158 | 159 | public void setTypeFace(Typeface typeFace) { 160 | this.typeFace = typeFace; 161 | } 162 | 163 | public int getFontSize() { 164 | return fontSize; 165 | } 166 | 167 | public void setFontSize(int fontSize) { 168 | this.fontSize = fontSize; 169 | } 170 | 171 | public String[] getSections() { 172 | return sections; 173 | } 174 | 175 | public void setSections(String[] sections) { 176 | this.sections = sections; 177 | } 178 | 179 | public ColorStateList getColor() { 180 | return color; 181 | } 182 | 183 | public void setColor(ColorStateList color) { 184 | this.color = color; 185 | } 186 | 187 | public ColorStateList getChosenColor() { 188 | return chosenColor; 189 | } 190 | 191 | public void setChosenColor(ColorStateList chosenColor) { 192 | this.chosenColor = chosenColor; 193 | } 194 | 195 | public Typeface getChosenTypeFace() { 196 | return chosenTypeFace; 197 | } 198 | 199 | public void setChosenTypeFace(Typeface chosenTypeFace) { 200 | this.chosenTypeFace = chosenTypeFace; 201 | } 202 | 203 | public boolean isIndicatorEnabled() { 204 | return indicatorEnabled; 205 | } 206 | 207 | public void setIndicatorEnabled(boolean indicatorEnabled) { 208 | this.indicatorEnabled = indicatorEnabled; 209 | } 210 | 211 | public void setOnTouchingLetterChangedListener( 212 | OnTouchingLetterChangedListener onTouchingLetterChangedListener) { 213 | this.onTouchingLetterChangedListener = onTouchingLetterChangedListener; 214 | } 215 | 216 | public interface OnTouchingLetterChangedListener { 217 | void onTouchingLetterChanged(String s); 218 | } 219 | } 220 | -------------------------------------------------------------------------------- /sectionpicker/src/main/java/com/ehamutcu/sectionpicker/helper/AttrHelper.java: -------------------------------------------------------------------------------- 1 | package com.ehamutcu.sectionpicker.helper; 2 | 3 | import android.content.Context; 4 | import android.content.res.ColorStateList; 5 | import android.content.res.TypedArray; 6 | import android.graphics.Typeface; 7 | import android.util.AttributeSet; 8 | 9 | import com.ehamutcu.sectionpicker.util.DisplayUtil; 10 | 11 | /** 12 | * Created by EgemenH on 02.06.2017. 13 | */ 14 | 15 | public class AttrHelper { 16 | 17 | /** 18 | * Gets the textSize attribute's value from the given attribute set 19 | * 20 | * @param context a context 21 | * @param attrs attribute set of a view (e.g {@link com.ehamutcu.sectionpicker.SectionPicker}) 22 | * @param defStyleAttr (e.g R.styleable.SectionPicker) 23 | * @param sizeIndex font index of the view (e.g R.styleable.SectionPicker_textSize) 24 | * @return the given integer pixel value of textSize 25 | */ 26 | public static int getFontSize(Context context, AttributeSet attrs, int defStyleAttr[], int sizeIndex) { 27 | TypedArray typedArray = context.obtainStyledAttributes( 28 | attrs, 29 | defStyleAttr, 30 | 0, 31 | 0); 32 | 33 | try { 34 | return typedArray.getDimensionPixelSize(sizeIndex, DisplayUtil.spToPx(10, context)); 35 | } finally { 36 | typedArray.recycle(); 37 | } 38 | } 39 | 40 | /** 41 | * Gets the textColor attribute's value from the given attribute set 42 | * 43 | * @param context a context 44 | * @param attrs attribute set of a view (e.g {@link com.ehamutcu.sectionpicker.SectionPicker}) 45 | * @param defStyleAttr (e.g R.styleable.SectionPicker) 46 | * @param colorIndex font index of the view (e.g R.styleable.SectionPicker_textColor) 47 | * @return the given integer ColorStateList value of textColor 48 | */ 49 | public static ColorStateList getFontColor(Context context, AttributeSet attrs, int defStyleAttr[], int colorIndex) { 50 | TypedArray typedArray = context.obtainStyledAttributes( 51 | attrs, 52 | defStyleAttr, 53 | 0, 54 | 0); 55 | 56 | try { 57 | return typedArray.getColorStateList(colorIndex); 58 | } finally { 59 | typedArray.recycle(); 60 | } 61 | } 62 | 63 | /** 64 | * Gets the textSize attribute's value from the given attribute set 65 | * 66 | * @param context a context 67 | * @param attrs attribute set of a view (e.g {@link com.ehamutcu.sectionpicker.SectionPicker}) 68 | * @param defStyleAttr (e.g R.styleable.SectionPicker) 69 | * @param styleIndex style index of the view (e.g R.styleable.SectionPicker_textStyle) 70 | * @return one of {@link Typeface#NORMAL}, {@link Typeface#BOLD}, and {@link Typeface#ITALIC} 71 | */ 72 | public static int getFontStyle(Context context, AttributeSet attrs, int defStyleAttr[], int styleIndex) { 73 | TypedArray typedArray = context.obtainStyledAttributes( 74 | attrs, 75 | defStyleAttr, 76 | 0, 77 | 0); 78 | 79 | try { 80 | return typedArray.getInt(styleIndex, Typeface.NORMAL); 81 | } finally { 82 | typedArray.recycle(); 83 | } 84 | } 85 | 86 | /** 87 | * Gets the textColor attribute's value from the given attribute set 88 | * 89 | * @param context a context 90 | * @param attrs attribute set of a view {@link com.ehamutcu.sectionpicker.SectionPicker}) 91 | * @param defStyleAttr (e.g R.styleable.SectionPicker) 92 | * @param colorIndex font index of the view (e.g R.styleable.SectionPicker_textColor) 93 | * @return the given boolean value of indicatorEnabled 94 | */ 95 | public static boolean getIndicatorEnabled(Context context, AttributeSet attrs, int defStyleAttr[], int colorIndex) { 96 | TypedArray typedArray = context.obtainStyledAttributes( 97 | attrs, 98 | defStyleAttr, 99 | 0, 100 | 0); 101 | 102 | try { 103 | return typedArray.getBoolean(colorIndex, true); 104 | } finally { 105 | typedArray.recycle(); 106 | } 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /sectionpicker/src/main/java/com/ehamutcu/sectionpicker/util/DisplayUtil.java: -------------------------------------------------------------------------------- 1 | package com.ehamutcu.sectionpicker.util; 2 | 3 | import android.content.Context; 4 | import android.util.TypedValue; 5 | 6 | /** 7 | * Created by EgemenH on 02.06.2017. 8 | */ 9 | 10 | public class DisplayUtil { 11 | 12 | public static int spToPx(float sp, Context context) { 13 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /sectionpicker/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /sectionpicker/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | SectionPicker 3 | 4 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':sample', ':sectionpicker' 2 | --------------------------------------------------------------------------------