├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── drawable-hdpi │ │ │ │ ├── icon_ir.png │ │ │ │ └── icon_led.png │ │ │ ├── drawable-mdpi │ │ │ │ ├── icon_ir.png │ │ │ │ └── icon_led.png │ │ │ ├── drawable-xhdpi │ │ │ │ ├── icon_ir.png │ │ │ │ └── icon_led.png │ │ │ ├── drawable │ │ │ │ ├── ic_launcher.png │ │ │ │ ├── ic_black_launcher.png │ │ │ │ ├── selector_src.xml │ │ │ │ └── selector_background_example.xml │ │ │ ├── drawable-xxhdpi │ │ │ │ ├── icon_ir.png │ │ │ │ └── icon_led.png │ │ │ ├── drawable-xxxhdpi │ │ │ │ ├── icon_ir.png │ │ │ │ └── icon_led.png │ │ │ ├── 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 │ │ │ │ ├── colors.xml │ │ │ │ ├── dimens.xml │ │ │ │ ├── strings.xml │ │ │ │ └── styles.xml │ │ │ ├── color │ │ │ │ └── selector_text_view.xml │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ └── activity_main.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── goodiebag │ │ │ └── example │ │ │ └── horizontalpicker │ │ │ └── MainActivity.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── goodiebag │ │ │ └── example │ │ │ └── horizontalpicker │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── goodiebag │ │ └── example │ │ └── horizontalpicker │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── horizontalpicker ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ └── attrs.xml │ │ │ ├── color │ │ │ │ └── selector_tv.xml │ │ │ └── drawable │ │ │ │ └── selector_background.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── goodiebag │ │ │ └── horizontalpicker │ │ │ └── HorizontalPicker.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── goodiebag │ │ │ └── horizontalpicker │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── goodiebag │ │ └── horizontalpicker │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── gif ├── HPtap.gif └── HPslide.gif ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── LICENSE.txt ├── gradlew.bat ├── README.md └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /horizontalpicker/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':horizontalpicker' 2 | -------------------------------------------------------------------------------- /gif/HPtap.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/gif/HPtap.gif -------------------------------------------------------------------------------- /gif/HPslide.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/gif/HPslide.gif -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /horizontalpicker/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | HorizontalPicker 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/icon_ir.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/app/src/main/res/drawable-hdpi/icon_ir.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/icon_led.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/app/src/main/res/drawable-hdpi/icon_led.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-mdpi/icon_ir.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/app/src/main/res/drawable-mdpi/icon_ir.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-mdpi/icon_led.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/app/src/main/res/drawable-mdpi/icon_led.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/icon_ir.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/app/src/main/res/drawable-xhdpi/icon_ir.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/app/src/main/res/drawable/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/icon_led.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/app/src/main/res/drawable-xhdpi/icon_led.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/icon_ir.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/app/src/main/res/drawable-xxhdpi/icon_ir.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/icon_led.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/app/src/main/res/drawable-xxhdpi/icon_led.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxxhdpi/icon_ir.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/app/src/main/res/drawable-xxxhdpi/icon_ir.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxxhdpi/icon_led.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/app/src/main/res/drawable-xxxhdpi/icon_led.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_black_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/app/src/main/res/drawable/ic_black_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodieBag/HorizontalPicker/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/color/selector_text_view.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /horizontalpicker/src/main/res/color/selector_tv.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/selector_src.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /horizontalpicker/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | HorizontalPicker 3 | 4 | 5 | S1 6 | S2 7 | S3 8 | S4 9 | 10 | 11 | 12 | ic 13 | ic 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /horizontalpicker/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /horizontalpicker/src/test/java/com/goodiebag/horizontalpicker/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.goodiebag.horizontalpicker; 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 | } -------------------------------------------------------------------------------- /app/src/test/java/com/goodiebag/example/horizontalpicker/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.goodiebag.example.horizontalpicker; 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 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable/selector_background_example.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | # Files for the ART/Dalvik VM 5 | *.dex 6 | # Java class files 7 | *.class 8 | # Generated files 9 | bin/ 10 | gen/ 11 | out/ 12 | # Gradle files 13 | .gradle/ 14 | build/ 15 | # Local configuration file (sdk path, etc) 16 | local.properties 17 | # Proguard folder generated by Eclipse 18 | proguard/ 19 | # Log Files 20 | *.log 21 | # Android Studio Navigation editor temp files 22 | .navigation/ 23 | # Android Studio captures folder 24 | captures/ 25 | # Intellij 26 | *.iml 27 | .idea 28 | # Keystore files 29 | *.jks 30 | # External native build folder generated in Android Studio 2.2 and later 31 | .externalNativeBuild -------------------------------------------------------------------------------- /horizontalpicker/src/main/res/drawable/selector_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/Kai/Library/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 | -------------------------------------------------------------------------------- /horizontalpicker/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 /Users/Kai/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /horizontalpicker/src/androidTest/java/com/goodiebag/horizontalpicker/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.goodiebag.horizontalpicker; 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.goodiebag.horizontalpicker.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/goodiebag/example/horizontalpicker/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.goodiebag.example.horizontalpicker; 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.goodiebag.example.horizontalpicker", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /horizontalpicker/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | 7 | defaultConfig { 8 | minSdkVersion 15 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.1' 30 | testCompile 'junit:junit:4.12' 31 | } 32 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "com.goodiebag.example.horizontalpicker" 8 | minSdkVersion 15 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 'com.android.support:appcompat-v7:25.1.1' 28 | testCompile 'junit:junit:4.12' 29 | compile project(':horizontalpicker') 30 | } 31 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 GoodieBag 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 17 | 18 | 19 | 30 | 31 | 32 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/goodiebag/example/horizontalpicker/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.goodiebag.example.horizontalpicker; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | import android.widget.TextView; 6 | import android.widget.Toast; 7 | 8 | import com.goodiebag.horizontalpicker.HorizontalPicker; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | public class MainActivity extends AppCompatActivity { 14 | 15 | @Override 16 | protected void onCreate(Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | setContentView(R.layout.activity_main); 19 | final TextView textView = (TextView) findViewById(R.id.tv); 20 | HorizontalPicker hpText = (HorizontalPicker) findViewById(R.id.hpText); 21 | HorizontalPicker hpImage = (HorizontalPicker) findViewById(R.id.hpImage); 22 | HorizontalPicker.OnSelectionChangeListener listener = new HorizontalPicker.OnSelectionChangeListener() { 23 | @Override 24 | public void onItemSelect(HorizontalPicker picker, int index) { 25 | HorizontalPicker.PickerItem selected = picker.getSelectedItem(); 26 | textView.setText((selected.hasDrawable() ? "Item at " + (picker.getSelectedIndex() + 1) + " is selected" : selected.getText() + " is selected")); 27 | //Toast.makeText(MainActivity.this, selected.hasDrawable() ? "Item at " + (picker.getSelectedIndex() + 1) + " is selected" : selected.getText() + " is selected", Toast.LENGTH_SHORT).show(); 28 | } 29 | 30 | }; 31 | 32 | List textItems = new ArrayList<>(); 33 | for(int i=1;i<=4;i++){ 34 | textItems.add(new HorizontalPicker.TextItem("S"+i)); 35 | } 36 | // textItems.add(new HorizontalPicker.TextItem("S1")); 37 | // textItems.add(new HorizontalPicker.TextItem("S2")); 38 | // textItems.add(new HorizontalPicker.TextItem("S3")); 39 | // textItems.add(new HorizontalPicker.TextItem("S4")); 40 | // textItems.add(new HorizontalPicker.TextItem("S5")); 41 | 42 | hpText.setItems(textItems,3); 43 | 44 | List imageItems = new ArrayList<>(); 45 | imageItems.add(new HorizontalPicker.DrawableItem(R.drawable.icon_led)); 46 | imageItems.add(new HorizontalPicker.DrawableItem(R.drawable.icon_ir)); 47 | 48 | hpImage.setItems(imageItems); 49 | hpImage.setSelectedIndex(0); 50 | 51 | hpText.setChangeListener(listener); 52 | hpImage.setChangeListener(listener); 53 | 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Horizontal Picker 2 | 3 | [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-HorizontalPicker-blue.svg?style=flat)](https://android-arsenal.com/details/1/5373) 4 | [![Release](https://jitpack.io/v/GoodieBag/HorizontalPicker.svg)](https://jitpack.io/#GoodieBag/HorizontalPicker) 5 | [![API](https://img.shields.io/badge/API-15%2B-blue.svg?style=flat)](https://android-arsenal.com/api?level=15) 6 | 7 | A Horizontal picker library for android which supports both text and icons . :pouting_cat: 8 | 9 | ![alt tag](https://github.com/GoodieBag/HorizontalPicker/blob/master/gif/HPtap.gif?raw=true) ![alt tag](https://github.com/GoodieBag/HorizontalPicker/blob/master/gif/HPslide.gif?raw=true) 10 | 11 | ## Gradle Dependency 12 | 13 | Add this in your root build.gradle file at the end of repositories: 14 | ```java 15 | allprojects { 16 | repositories { 17 | ... 18 | maven { url 'https://jitpack.io' } 19 | } 20 | } 21 | ``` 22 | Add the dependency : 23 | ```java 24 | dependencies { 25 | compile 'com.github.GoodieBag:HorizontalPicker:v1.0' 26 | } 27 | ``` 28 | Sync the gradle and that's it! :+1: 29 | 30 | ## Features : 31 | * Supports icons and text or a mixture of both as items of the picker. 32 | * Tap or scroll horizontally to select the items. 33 | * Supports drawables, selectors and text highlighting using selectors. 34 | * Selection change listener to monitor the current selected item. 35 | 36 | ## Usage 37 | 38 | ### XML : 39 | ```xml 40 | 50 | ``` 51 | 52 | P.S : ```itemWidth``` and ```itemHeight``` are not mandatory. If ignored, it is WRAP_CONTENT by default.
53 | ```textColorSelector``` is only applicable when the items are textItems. 54 | 55 | 56 | ### Java : 57 | ```java 58 | //If your picker needs to have text as items : 59 | HorizontalPicker hpText = (HorizontalPicker) findViewById(R.id.picker); 60 | 61 | List textItems = new ArrayList<>(); 62 | for(int i=1;i<=4;i++){ 63 | textItems.add(new HorizontalPicker.TextItem("S"+i)); 64 | } 65 | hpText.setItems(textItems,3); //3 here signifies the default selected item. Use : hpText.setItems(textItems) if none of the items are selected by default. 66 | 67 | //If your picker takes images as items : 68 | HorizontalPicker hpImage = (HorizontalPicker) findViewById(R.id.hpImage); 69 | 70 | List imageItems = new ArrayList<>(); 71 | imageItems.add(new HorizontalPicker.DrawableItem(R.drawable.example)); 72 | imageItems.add(new HorizontalPicker.DrawableItem(R.drawable.example2)); 73 | hpImage.setItems(imageItems); 74 | ``` 75 | ###### Use ```hpText.setSelectedIndex(index)``` to select an item programmatically. 76 | ## Listeners : 77 | A listener exists to monitor the item selection change on slide or on tap. 78 | 79 | ```java 80 | HorizontalPicker.OnSelectionChangeListener listener = new HorizontalPicker.OnSelectionChangeListener() { 81 | @Override 82 | public void onItemSelect(HorizontalPicker picker, int index) { 83 | HorizontalPicker.PickerItem selected = picker.getSelectedItem(); 84 | Toast.makeText(MainActivity.this, selected.hasDrawable() ? "Item at " + (picker.getSelectedIndex() + 1) + " is selected" : selected.getText() + " is selected", Toast.LENGTH_SHORT).show(); 85 | } 86 | hpText.setChangeListener(listener); 87 | 88 | ``` 89 | ### Known Issues : 90 | When the HorizontalPicker is wrapped in an HorizontalScrollView the scroll to select feature of the picker does not work as expected. This will be fixed soon. 91 | 92 | ## LICENSE : 93 | ``` 94 | MIT License 95 | 96 | Copyright (c) 2017 GoodieBag 97 | 98 | Permission is hereby granted, free of charge, to any person obtaining a copy 99 | of this software and associated documentation files (the "Software"), to deal 100 | in the Software without restriction, including without limitation the rights 101 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 102 | copies of the Software, and to permit persons to whom the Software is 103 | furnished to do so, subject to the following conditions: 104 | 105 | The above copyright notice and this permission notice shall be included in all 106 | copies or substantial portions of the Software. 107 | 108 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 109 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 110 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 111 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 112 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 113 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 114 | SOFTWARE. 115 | ``` 116 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /horizontalpicker/src/main/java/com/goodiebag/horizontalpicker/HorizontalPicker.java: -------------------------------------------------------------------------------- 1 | /* 2 | MIT License 3 | 4 | Copyright (c) 2017 GoodieBag 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | */ 24 | 25 | 26 | package com.goodiebag.horizontalpicker; 27 | import android.content.Context; 28 | import android.content.res.TypedArray; 29 | import android.graphics.Rect; 30 | import android.support.annotation.ColorRes; 31 | import android.support.annotation.DrawableRes; 32 | import android.support.v4.content.ContextCompat; 33 | import android.util.AttributeSet; 34 | import android.util.TypedValue; 35 | import android.view.Gravity; 36 | import android.view.MotionEvent; 37 | import android.view.View; 38 | import android.view.ViewGroup; 39 | import android.widget.ImageView; 40 | import android.widget.LinearLayout; 41 | import android.widget.TextView; 42 | import java.util.ArrayList; 43 | import java.util.List; 44 | 45 | /** 46 | * This class implements a horizontal picker views for android. 47 | * It can be used as a widget in android to select an item from a list of items. 48 | * It is extended from a LinearLayout and it is responsive to touch as well as scroll. 49 | * Supports DrawableItem(s) as well as TextItem(s) as items for the picker (supports mixture of both as well). 50 | * Also monitors the selection change event using listeners. 51 | * Date : 11/02/17 52 | * @author Krishanu 53 | * @author Pavan 54 | * @author Koushik 55 | */ 56 | public class HorizontalPicker extends LinearLayout implements View.OnTouchListener { 57 | private final float DENSITY = getContext().getResources().getDisplayMetrics().density; 58 | /** 59 | * List of attributes with their default values. 60 | * They can be set either from XML or using their respective setters. 61 | */ 62 | @DrawableRes 63 | private int backgroundSelector = R.drawable.selector_background; 64 | @ColorRes 65 | private int colorSelector = R.color.selector_tv; 66 | private int textSize = 12; 67 | private int selectedIndex=-1; 68 | private int itemHeight = ViewGroup.LayoutParams.WRAP_CONTENT; 69 | private int itemWidth = ViewGroup.LayoutParams.WRAP_CONTENT; 70 | private int itemMargin = 20; 71 | 72 | private List items = new ArrayList<>(); 73 | OnSelectionChangeListener changeListener; 74 | 75 | /** 76 | * Constructor 77 | * @param context 78 | */ 79 | public HorizontalPicker(Context context) { 80 | this(context, null); 81 | } 82 | 83 | public HorizontalPicker(Context context, AttributeSet attrs) { 84 | this(context, attrs, 0); 85 | } 86 | 87 | public HorizontalPicker(Context context, AttributeSet attrs, int defStyleAttr) { 88 | super(context, attrs, defStyleAttr); 89 | initAttributes(context, attrs, defStyleAttr); 90 | this.setGravity(Gravity.CENTER); 91 | } 92 | 93 | /** 94 | * A method to initialise the attributes from the XML or the default values. 95 | * textSize and itemMargin are multiplied with DENSITY to maintain consistency of the views across various device resolutions. 96 | * @param context 97 | * @param attrs 98 | * @param defStyleAttr 99 | */ 100 | private void initAttributes(Context context, AttributeSet attrs, int defStyleAttr) { 101 | 102 | textSize *= DENSITY; 103 | itemMargin *= DENSITY; 104 | selectedIndex = -1; 105 | /** 106 | * Getting values of the attributes from the XML. 107 | * The default value of the attribute is retained if it is not set from the XML or setters. 108 | */ 109 | if (attrs != null) { 110 | final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.HorizontalPicker, defStyleAttr, 0); 111 | backgroundSelector = array.getResourceId(R.styleable.HorizontalPicker_backgroundSelector, backgroundSelector); 112 | colorSelector = array.getResourceId(R.styleable.HorizontalPicker_textColorSelector, colorSelector); 113 | textSize = array.getDimensionPixelSize(R.styleable.HorizontalPicker_textSize, textSize); 114 | itemHeight = array.getDimensionPixelSize(R.styleable.HorizontalPicker_itemHeight, itemHeight); 115 | itemWidth = array.getDimensionPixelSize(R.styleable.HorizontalPicker_itemWidth, itemWidth); 116 | itemMargin = array.getDimensionPixelSize(R.styleable.HorizontalPicker_itemMargin, itemMargin); 117 | array.recycle(); 118 | } 119 | 120 | } 121 | 122 | /** 123 | * A method to initialise the views. 124 | * The LayoutParams listeners are initialised in this method. 125 | * All the items are iterated to check if the current item is a DrawableItem or a TextItem and it takes respective action. 126 | * This mechanism supports a mixture of DrawableItem(s) as well as TextItem(s). 127 | */ 128 | private void initViews() { 129 | removeAllViews(); 130 | this.setOrientation(HORIZONTAL); 131 | this.setOnTouchListener(this); 132 | 133 | TextView textView; 134 | ImageView imageView; 135 | for (PickerItem pickerItem : items) { 136 | if (pickerItem.hasDrawable()) { 137 | imageView = new ImageView(getContext()); 138 | this.addView(imageView); 139 | imageView.setImageResource(pickerItem.getDrawable()); 140 | imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 141 | initStyle(imageView); 142 | } else { 143 | if (pickerItem.getText() != null) { 144 | textView = new TextView(getContext()); 145 | this.addView(textView); 146 | textView.setGravity(Gravity.CENTER); 147 | textView.setText(pickerItem.getText()); 148 | initStyle(textView); 149 | } 150 | } 151 | } 152 | } 153 | 154 | /** 155 | * A method to style each view in the picker. 156 | * The already initialised LayoutParams is set to the view here and in case of a TextItem, 157 | * extra measures such as text size and color is taken care of. 158 | * @param view 159 | */ 160 | private void initStyle(View view) { 161 | LinearLayout.LayoutParams params = (LayoutParams) view.getLayoutParams(); 162 | params.width=itemWidth; 163 | params.height=itemHeight; 164 | params.setMargins(itemMargin, 0, itemMargin, 0); 165 | view.setLayoutParams(params); 166 | view.setBackgroundResource(backgroundSelector); 167 | if (view instanceof TextView) { 168 | ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); 169 | ((TextView) view).setTextColor( 170 | ContextCompat.getColorStateList(getContext(), colorSelector)); 171 | } 172 | } 173 | 174 | /** 175 | * Used to style all the children after an attribute change is made (mostly called from setters). 176 | */ 177 | private void initStyles() { 178 | for (int i = 0; i < getChildCount(); i++) { 179 | initStyle(getChildAt(i)); 180 | } 181 | } 182 | 183 | /** 184 | * An interface which should be implemented by all the Item classes. 185 | * The picker only accepts items in the form of PickerItem. 186 | */ 187 | public interface PickerItem { 188 | String getText(); 189 | 190 | @DrawableRes 191 | int getDrawable(); 192 | 193 | boolean hasDrawable(); 194 | } 195 | 196 | /** 197 | * A PickerItem which supports text. 198 | */ 199 | public static class TextItem implements PickerItem { 200 | private String text; 201 | 202 | public TextItem(String text) { 203 | this.text = text; 204 | } 205 | 206 | public String getText() { 207 | return text; 208 | } 209 | 210 | @Override 211 | public int getDrawable() { 212 | return 0; 213 | } 214 | 215 | @Override 216 | public boolean hasDrawable() { 217 | return false; 218 | } 219 | } 220 | 221 | /** 222 | * A PickerItem which supports drawables. 223 | */ 224 | public static class DrawableItem implements PickerItem { 225 | @DrawableRes 226 | private int drawable; 227 | 228 | public DrawableItem(@DrawableRes int drawable) { 229 | this.drawable = drawable; 230 | } 231 | 232 | @Override 233 | public String getText() { 234 | return null; 235 | } 236 | 237 | @DrawableRes 238 | public int getDrawable() { 239 | return drawable; 240 | } 241 | 242 | @Override 243 | public boolean hasDrawable() { 244 | return true; 245 | } 246 | } 247 | 248 | /** 249 | * Interface for the onItemSelect event. 250 | */ 251 | public interface OnSelectionChangeListener { 252 | void onItemSelect(HorizontalPicker picker, int index); 253 | } 254 | 255 | /** 256 | * Monitors the touch event. 257 | * If the action is ACTION_DOWN or ACTION_MOVE, the LinearLayout is traversed to get the hitRect of each child. 258 | * Each child hitRect is checked to see if it contains x,y touch co-ordinates. 259 | * If it does, selectChild(index) method is called. 260 | * @param view 261 | * @param motionEvent 262 | * @return 263 | */ 264 | @Override 265 | public boolean onTouch(View view, MotionEvent motionEvent) { 266 | switch (motionEvent.getAction()) { 267 | case MotionEvent.ACTION_DOWN: 268 | case MotionEvent.ACTION_MOVE: 269 | int x = (int) motionEvent.getX(); 270 | int y = (int) motionEvent.getY(); 271 | Rect hitRect = new Rect(); 272 | View v; 273 | 274 | for (int i = 0; i < getChildCount(); i++) { 275 | v = getChildAt(i); 276 | v.getHitRect(hitRect); 277 | if (hitRect.contains(x, y)) { 278 | selectChild(i); 279 | break; 280 | } 281 | } 282 | break; 283 | default: 284 | break; 285 | } 286 | 287 | return true; 288 | } 289 | 290 | /** 291 | * Selects the child at a given index and de-selects the rest. 292 | * @param index 293 | */ 294 | private void selectChild(int index) { 295 | if (selectedIndex != index) { 296 | selectedIndex = -1; 297 | for (int i = 0; i < getChildCount(); i++) { 298 | getChildAt(i).setSelected(i == index); 299 | if (i == index) { 300 | selectedIndex = index; 301 | } 302 | } 303 | 304 | if (changeListener != null) 305 | changeListener.onItemSelect(this, selectedIndex); 306 | } 307 | } 308 | 309 | /** 310 | * A method to set the picker items. 311 | * No item is selected by default. 312 | * @param items 313 | */ 314 | public void setItems(List items) { 315 | this.items = items; 316 | initViews(); 317 | selectChild(-1); 318 | } 319 | 320 | /** 321 | * A method to set the picker items. 322 | * The selectedIndex is selected by default. 323 | * @param items 324 | * @param selectedIndex 325 | */ 326 | public void setItems(List items, int selectedIndex) { 327 | setItems(items); 328 | selectChild(selectedIndex); 329 | } 330 | 331 | /** 332 | * Gets the picker items. 333 | * @return 334 | */ 335 | public List getItems() { 336 | return items; 337 | } 338 | 339 | /** 340 | * Getters and setters. 341 | */ 342 | 343 | public void setSelectedIndex(int selectedIndex) { 344 | selectChild(selectedIndex); 345 | 346 | } 347 | 348 | public int getSelectedIndex() { 349 | return selectedIndex; 350 | } 351 | 352 | public PickerItem getSelectedItem() { 353 | return items.get(selectedIndex); 354 | } 355 | 356 | @DrawableRes 357 | public int getBackgroundSelector() { 358 | return backgroundSelector; 359 | } 360 | 361 | public void setBackgroundSelector(@DrawableRes int backgroundSelector) { 362 | this.backgroundSelector = backgroundSelector; 363 | initStyles(); 364 | } 365 | 366 | @ColorRes 367 | public int getColorSelector() { 368 | return colorSelector; 369 | } 370 | 371 | public void setColorSelector(@ColorRes int colorSelector) { 372 | this.colorSelector = colorSelector; 373 | initStyles(); 374 | } 375 | 376 | public int getTextSize() { 377 | return textSize; 378 | } 379 | 380 | public void setTextSize(int textSize) { 381 | this.textSize = textSize; 382 | initStyles(); 383 | } 384 | 385 | public int getItemWidth() { 386 | return itemWidth; 387 | } 388 | 389 | public void setItemWidth(int itemWidth) { 390 | this.itemWidth = itemWidth; 391 | initStyles(); 392 | } 393 | 394 | public int getItemMargin() { 395 | return itemMargin; 396 | } 397 | 398 | public void setItemMargin(int itemMargin) { 399 | this.itemMargin = itemMargin; 400 | initStyles(); 401 | } 402 | 403 | public int getItemHeight() { 404 | return itemHeight; 405 | } 406 | 407 | public void setItemHeight(int itemHeight) { 408 | this.itemHeight = itemHeight; 409 | initStyles(); 410 | } 411 | 412 | public OnSelectionChangeListener getChangeListener() { 413 | return changeListener; 414 | } 415 | 416 | public void setChangeListener(OnSelectionChangeListener changeListener) { 417 | this.changeListener = changeListener; 418 | } 419 | 420 | } 421 | --------------------------------------------------------------------------------