├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ ├── arrays.xml │ │ │ │ └── styles.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 │ │ │ ├── drawable │ │ │ │ ├── et_login_bg.xml │ │ │ │ └── ic_launcher_background.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── layout │ │ │ │ └── activity_main.xml │ │ │ └── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── demo │ │ │ └── MainActivity.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── demo │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── demo │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── spinner_lib ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── dimens.xml │ │ │ │ └── attrs.xml │ │ │ ├── drawable-hdpi │ │ │ │ └── down_arrow.png │ │ │ ├── drawable-xhdpi │ │ │ │ └── down_arrow.png │ │ │ ├── drawable-xxhdpi │ │ │ │ └── down_arrow.png │ │ │ ├── drawable-xxxhdpi │ │ │ │ └── down_arrow.png │ │ │ ├── drawable │ │ │ │ ├── ms_selector_style_default.xml │ │ │ │ ├── ms_selector_style_pressed.xml │ │ │ │ ├── ms_arrow.xml │ │ │ │ ├── ms_popwindow_bg.xml │ │ │ │ ├── ms_menu_down.xml │ │ │ │ └── ms_selector_style.xml │ │ │ └── layout │ │ │ │ └── ms__list_item.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── lib │ │ │ └── demo │ │ │ └── spinner │ │ │ ├── MaterialSpinnerAdapter.java │ │ │ ├── MaterialSpinnerAdapterWrapper.java │ │ │ ├── Utils.java │ │ │ ├── MaterialSpinnerBaseAdapter.java │ │ │ └── MaterialSpinner.java │ ├── test │ │ └── java │ │ │ └── lib │ │ │ └── demo │ │ │ └── spinner │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── lib │ │ └── demo │ │ └── spinner │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── gif ├── demo1.gif └── demo2.gif ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── .idea ├── vcs.xml ├── modules.xml ├── runConfigurations.xml ├── gradle.xml └── misc.xml ├── README.md ├── unnamed.patch ├── gradle.properties ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /spinner_lib/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':spinner_lib' 2 | -------------------------------------------------------------------------------- /gif/demo1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/66668/MySpinner/HEAD/gif/demo1.gif -------------------------------------------------------------------------------- /gif/demo2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/66668/MySpinner/HEAD/gif/demo2.gif -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/66668/MySpinner/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | MySpinner 3 | 4 | -------------------------------------------------------------------------------- /spinner_lib/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | spinner_lib 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/66668/MySpinner/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/66668/MySpinner/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/66668/MySpinner/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/66668/MySpinner/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/66668/MySpinner/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/66668/MySpinner/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/66668/MySpinner/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/66668/MySpinner/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/66668/MySpinner/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/66668/MySpinner/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /spinner_lib/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /spinner_lib/src/main/res/drawable-hdpi/down_arrow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/66668/MySpinner/HEAD/spinner_lib/src/main/res/drawable-hdpi/down_arrow.png -------------------------------------------------------------------------------- /spinner_lib/src/main/res/drawable-xhdpi/down_arrow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/66668/MySpinner/HEAD/spinner_lib/src/main/res/drawable-xhdpi/down_arrow.png -------------------------------------------------------------------------------- /spinner_lib/src/main/res/drawable-xxhdpi/down_arrow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/66668/MySpinner/HEAD/spinner_lib/src/main/res/drawable-xxhdpi/down_arrow.png -------------------------------------------------------------------------------- /spinner_lib/src/main/res/drawable-xxxhdpi/down_arrow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/66668/MySpinner/HEAD/spinner_lib/src/main/res/drawable-xxxhdpi/down_arrow.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/et_login_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Nov 22 15:40:15 CST 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-4.1-all.zip 7 | -------------------------------------------------------------------------------- /spinner_lib/src/main/res/drawable/ms_selector_style_default.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /spinner_lib/src/main/res/drawable/ms_selector_style_pressed.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/arrays.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 身份证 5 | 护照 6 | 签证 7 | 军人证 8 | 港澳通行证 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 好用的下拉框 2 | ==== 3 | 代码示例lib已经在demo中,直接拷贝即可使用。使用样式可以根据自己的需求修改,代码中我已经做了详细注释。 4 | 5 | 这是项目中用到的效果图: 6 | 7 | 8 | 9 | 这是demo的效果图: 10 | 11 | 12 | -------------------------------------------------------------------------------- /spinner_lib/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 10dp 8 | 10dp 9 | 31dp 10 | 10dp 11 | -------------------------------------------------------------------------------- /spinner_lib/src/main/res/drawable/ms_arrow.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | -------------------------------------------------------------------------------- /spinner_lib/src/main/res/drawable/ms_popwindow_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /spinner_lib/src/main/res/drawable/ms_menu_down.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 12 | 15 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /spinner_lib/src/main/res/drawable/ms_selector_style.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/demo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.demo; 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 | } -------------------------------------------------------------------------------- /spinner_lib/src/test/java/lib/demo/spinner/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package lib.demo.spinner; 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 | } -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /spinner_lib/src/main/res/layout/ms__list_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /unnamed.patch: -------------------------------------------------------------------------------- 1 | Index: README.md 2 | IDEA additional info: 3 | Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP 4 | <+>UTF-8 5 | =================================================================== 6 | --- README.md (revision a780722674a0774fddc7085138bcfc3fc7fff08d) 7 | +++ README.md (revision ) 8 | @@ -1,4 +1,7 @@ 9 | 好用的下拉框 10 | ==== 11 | 这是项目中用到的效果图: 12 | - 13 | \ No newline at end of file 14 | + 15 | + 16 | + 17 | + 18 | \ No newline at end of file 19 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /spinner_lib/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/demo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.demo; 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 | * Instrumented 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.demo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spinner_lib/src/androidTest/java/lib/demo/spinner/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package lib.demo.spinner; 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 | * Instrumented 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("lib.demo.spinner.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spinner_lib/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 26 5 | 6 | 7 | 8 | defaultConfig { 9 | minSdkVersion 21 10 | targetSdkVersion 26 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | 25 | } 26 | 27 | dependencies { 28 | implementation fileTree(dir: 'libs', include: ['*.jar']) 29 | 30 | implementation 'com.android.support:appcompat-v7:26.1.0' 31 | testImplementation 'junit:junit:4.12' 32 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 33 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 34 | } 35 | -------------------------------------------------------------------------------- /spinner_lib/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 26 5 | defaultConfig { 6 | applicationId "com.demo" 7 | minSdkVersion 21 8 | targetSdkVersion 26 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(include: ['*.jar'], dir: 'libs') 23 | implementation 'com.android.support:appcompat-v7:26.1.0' 24 | implementation 'com.android.support.constraint:constraint-layout:1.0.2' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 28 | implementation project(':spinner_lib') 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/demo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.demo; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | 6 | import lib.demo.spinner.MaterialSpinner; 7 | 8 | public class MainActivity extends AppCompatActivity { 9 | 10 | @Override 11 | protected void onCreate(Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | setContentView(R.layout.activity_main); 14 | MaterialSpinner spinner = (MaterialSpinner) findViewById(R.id.spinner); 15 | String[] typeArrays = getResources().getStringArray(R.array.identify_types); 16 | spinner.setItems(typeArrays); 17 | spinner.setSelectedIndex(0); 18 | spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() { 19 | 20 | @Override 21 | public void onItemSelected(MaterialSpinner view, int position, long id, String item) { 22 | 23 | } 24 | }); 25 | 26 | spinner.setOnNothingSelectedListener(new MaterialSpinner.OnNothingSelectedListener() { 27 | 28 | @Override 29 | public void onNothingSelected(MaterialSpinner spinner) { 30 | spinner.getSelectedIndex(); 31 | } 32 | }); 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /spinner_lib/src/main/java/lib/demo/spinner/MaterialSpinnerAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Jared Rummler 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package lib.demo.spinner; 19 | 20 | import android.content.Context; 21 | 22 | import java.util.List; 23 | 24 | public class MaterialSpinnerAdapter extends MaterialSpinnerBaseAdapter { 25 | 26 | private final List items; 27 | 28 | public MaterialSpinnerAdapter(Context context, List items) { 29 | super(context); 30 | this.items = items; 31 | } 32 | 33 | @Override 34 | public int getCount() { 35 | return items.size() - 1; 36 | } 37 | 38 | @Override 39 | public T getItem(int position) { 40 | if (position >= getSelectedIndex()) { 41 | return items.get(position + 1); 42 | } else { 43 | return items.get(position); 44 | } 45 | } 46 | 47 | @Override 48 | public T get(int position) { 49 | return items.get(position); 50 | } 51 | 52 | @Override 53 | public List getItems() { 54 | return items; 55 | } 56 | 57 | } -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | -------------------------------------------------------------------------------- /spinner_lib/src/main/java/lib/demo/spinner/MaterialSpinnerAdapterWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Jared Rummler 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package lib.demo.spinner; 19 | 20 | import android.content.Context; 21 | import android.widget.ListAdapter; 22 | 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | 26 | final class MaterialSpinnerAdapterWrapper extends MaterialSpinnerBaseAdapter { 27 | 28 | private final ListAdapter listAdapter; 29 | 30 | public MaterialSpinnerAdapterWrapper(Context context, ListAdapter toWrap) { 31 | super(context); 32 | listAdapter = toWrap; 33 | } 34 | 35 | @Override 36 | public int getCount() { 37 | return listAdapter.getCount() - 1; 38 | } 39 | 40 | @Override 41 | public Object getItem(int position) { 42 | if (position >= getSelectedIndex()) { 43 | return listAdapter.getItem(position + 1); 44 | } else { 45 | return listAdapter.getItem(position); 46 | } 47 | } 48 | 49 | @Override 50 | public Object get(int position) { 51 | return listAdapter.getItem(position); 52 | } 53 | 54 | @Override 55 | public List getItems() { 56 | List items = new ArrayList<>(); 57 | for (int i = 0; i < getCount(); i++) { 58 | items.add(getItem(i)); 59 | } 60 | return items; 61 | } 62 | 63 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /spinner_lib/src/main/java/lib/demo/spinner/Utils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Jared Rummler 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package lib.demo.spinner; 19 | 20 | import android.content.Context; 21 | import android.graphics.Color; 22 | import android.os.Build; 23 | import android.view.View; 24 | 25 | final class Utils { 26 | 27 | /** 28 | * Darkens a color by a given factor. 29 | * 30 | * @param color the color to darken 31 | * @param factor The factor to darken the color. 32 | * @return darker version of specified color. 33 | */ 34 | static int darker(int color, float factor) { 35 | return Color.argb(Color.alpha(color), Math.max((int) (Color.red(color) * factor), 0), 36 | Math.max((int) (Color.green(color) * factor), 0), 37 | Math.max((int) (Color.blue(color) * factor), 0)); 38 | } 39 | 40 | /** 41 | * Lightens a color by a given factor. 42 | * 43 | * @param color The color to lighten 44 | * @param factor The factor to lighten the color. 0 will make the color unchanged. 1 will make the 45 | * color white. 46 | * @return lighter version of the specified color. 47 | */ 48 | static int lighter(int color, float factor) { 49 | int red = (int) ((Color.red(color) * (1 - factor) / 255 + factor) * 255); 50 | int green = (int) ((Color.green(color) * (1 - factor) / 255 + factor) * 255); 51 | int blue = (int) ((Color.blue(color) * (1 - factor) / 255 + factor) * 255); 52 | return Color.argb(Color.alpha(color), red, green, blue); 53 | } 54 | 55 | /** 56 | * Check if layout direction is RTL 57 | * 58 | * @param context the current context 59 | * @return {@code true} if the layout direction is right-to-left 60 | */ 61 | static boolean isRtl(Context context) { 62 | return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && 63 | context.getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; 64 | } 65 | 66 | 67 | } 68 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /spinner_lib/src/main/java/lib/demo/spinner/MaterialSpinnerBaseAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Jared Rummler 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package lib.demo.spinner; 19 | 20 | import android.content.Context; 21 | import android.content.res.Configuration; 22 | import android.os.Build; 23 | import android.support.annotation.ColorInt; 24 | import android.support.annotation.DrawableRes; 25 | import android.view.LayoutInflater; 26 | import android.view.View; 27 | import android.view.ViewGroup; 28 | import android.widget.BaseAdapter; 29 | import android.widget.TextView; 30 | 31 | import java.util.List; 32 | 33 | /** 34 | * list适配 35 | * 36 | * @param 37 | */ 38 | public abstract class MaterialSpinnerBaseAdapter extends BaseAdapter { 39 | 40 | private final Context context; 41 | private int selectedIndex; 42 | private int textColor; 43 | private int backgroundSelector; 44 | 45 | public MaterialSpinnerBaseAdapter(Context context) { 46 | this.context = context; 47 | } 48 | 49 | @Override 50 | public View getView(int position, View convertView, ViewGroup parent) { 51 | final TextView textView; 52 | if (convertView == null) { 53 | LayoutInflater inflater = LayoutInflater.from(context); 54 | convertView = inflater.inflate(R.layout.ms__list_item, parent, false); 55 | textView = (TextView) convertView.findViewById(R.id.tv_tinted_spinner); 56 | textView.setTextColor(textColor); 57 | if (backgroundSelector != 0) { 58 | textView.setBackgroundResource(backgroundSelector); 59 | } 60 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 61 | Configuration config = context.getResources().getConfiguration(); 62 | if (config.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) { 63 | textView.setTextDirection(View.TEXT_DIRECTION_RTL); 64 | } 65 | } 66 | convertView.setTag(new ViewHolder(textView)); 67 | } else { 68 | textView = ((ViewHolder) convertView.getTag()).textView; 69 | } 70 | textView.setText(getItemText(position)); 71 | return convertView; 72 | } 73 | 74 | public String getItemText(int position) { 75 | return getItem(position).toString(); 76 | } 77 | 78 | public int getSelectedIndex() { 79 | return selectedIndex; 80 | } 81 | 82 | public void notifyItemSelected(int index) { 83 | selectedIndex = index; 84 | } 85 | 86 | @Override 87 | public long getItemId(int position) { 88 | return position; 89 | } 90 | 91 | @Override 92 | public abstract T getItem(int position); 93 | 94 | @Override 95 | public abstract int getCount(); 96 | 97 | public abstract T get(int position); 98 | 99 | public abstract List getItems(); 100 | 101 | public MaterialSpinnerBaseAdapter setTextColor(@ColorInt int textColor) { 102 | this.textColor = textColor; 103 | return this; 104 | } 105 | 106 | public MaterialSpinnerBaseAdapter setBackgroundSelector(@DrawableRes int backgroundSelector) { 107 | this.backgroundSelector = backgroundSelector; 108 | return this; 109 | } 110 | 111 | private static class ViewHolder { 112 | 113 | private TextView textView; 114 | 115 | private ViewHolder(TextView textView) { 116 | this.textView = textView; 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 11 | 16 | 21 | 26 | 31 | 36 | 41 | 46 | 51 | 56 | 61 | 66 | 71 | 76 | 81 | 86 | 91 | 96 | 101 | 106 | 111 | 116 | 121 | 126 | 131 | 136 | 141 | 146 | 151 | 156 | 161 | 166 | 171 | 172 | -------------------------------------------------------------------------------- /spinner_lib/src/main/java/lib/demo/spinner/MaterialSpinner.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Jared Rummler 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package lib.demo.spinner; 19 | 20 | import android.animation.ObjectAnimator; 21 | import android.content.Context; 22 | import android.content.res.Resources; 23 | import android.content.res.TypedArray; 24 | import android.graphics.Color; 25 | import android.graphics.PorterDuff; 26 | import android.graphics.drawable.ColorDrawable; 27 | import android.graphics.drawable.Drawable; 28 | import android.graphics.drawable.StateListDrawable; 29 | import android.os.Build; 30 | import android.os.Bundle; 31 | import android.os.Parcelable; 32 | import android.support.annotation.ColorInt; 33 | import android.support.annotation.NonNull; 34 | import android.support.annotation.Nullable; 35 | import android.support.v4.content.ContextCompat; 36 | import android.util.AttributeSet; 37 | import android.util.Log; 38 | import android.view.Gravity; 39 | import android.view.MotionEvent; 40 | import android.view.View; 41 | import android.view.WindowManager; 42 | import android.widget.AdapterView; 43 | import android.widget.ListAdapter; 44 | import android.widget.ListView; 45 | import android.widget.PopupWindow; 46 | 47 | import java.lang.reflect.Method; 48 | import java.util.Arrays; 49 | import java.util.List; 50 | 51 | /** 52 | * 自定义 PopupWindow实现样式 53 | * 布局中直接以用,样式已经设置完成,自己需要自定义具体位置在res/drawable/ms_selector_style.xml 54 | */ 55 | public class MaterialSpinner extends android.support.v7.widget.AppCompatTextView { 56 | 57 | private OnNothingSelectedListener onNothingSelectedListener; 58 | private OnItemSelectedListener onItemSelectedListener; 59 | private MaterialSpinnerBaseAdapter adapter; //listView的适配 60 | private PopupWindow popupWindow; //使用popupWindow控件 样式 61 | private ListView listView; //布局 62 | private Drawable arrowDrawable; //箭头布局 视图 63 | private boolean hideArrow;//xml中arrow是否隐藏,默认不隐藏false 64 | private boolean nothingSelected; 65 | private int popupWindowMaxHeight;//spinner下拉框整体高度 最大高度 66 | private int popupWindowHeight;//spinner下拉框整体高度 67 | private int selectedIndex; 68 | private int backgroundColor; 69 | private int backgroundSelector; 70 | private int arrowColor; 71 | private int arrowColorDisabled; 72 | private int textColor; //字体颜色 73 | private Context context; 74 | 75 | public MaterialSpinner(Context context) { 76 | super(context); 77 | this.context = context; 78 | init(context, null); 79 | } 80 | 81 | public MaterialSpinner(Context context, AttributeSet attrs) { 82 | super(context, attrs); 83 | this.context = context; 84 | init(context, attrs); 85 | } 86 | 87 | public MaterialSpinner(Context context, AttributeSet attrs, int defStyleAttr) { 88 | super(context, attrs, defStyleAttr); 89 | this.context = context; 90 | init(context, attrs); 91 | } 92 | 93 | /** 94 | * 构建 95 | * 96 | * @param context 97 | * @param attrs 98 | */ 99 | private void init(Context context, AttributeSet attrs) { 100 | //获取attrs.xml中设置的参数 101 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaterialSpinner); 102 | 103 | //默认色设置 104 | int defaultColor = getTextColors().getDefaultColor(); 105 | 106 | boolean rtl = Utils.isRtl(context); 107 | 108 | try { 109 | //背景色 默认白色 110 | backgroundColor = typedArray.getColor(R.styleable.MaterialSpinner_ms_background_color, Color.WHITE); 111 | //背景 选中色 112 | backgroundSelector = typedArray.getResourceId(R.styleable.MaterialSpinner_ms_background_selector, 0); 113 | textColor = typedArray.getColor(R.styleable.MaterialSpinner_ms_text_color, defaultColor); 114 | arrowColor = typedArray.getColor(R.styleable.MaterialSpinner_ms_arrow_tint, textColor); 115 | hideArrow = typedArray.getBoolean(R.styleable.MaterialSpinner_ms_hide_arrow, false); 116 | popupWindowMaxHeight = typedArray.getDimensionPixelSize(R.styleable.MaterialSpinner_ms_popupwindow_maxheight, 0); 117 | popupWindowHeight = typedArray.getLayoutDimension(R.styleable.MaterialSpinner_ms_popupwindow_height, WindowManager.LayoutParams.WRAP_CONTENT); 118 | arrowColorDisabled = Utils.lighter(arrowColor, 0.8f); 119 | } finally { 120 | typedArray.recycle(); 121 | } 122 | 123 | //设置字体显示 居中 124 | Resources resources = getResources(); 125 | int left = resources.getDimensionPixelSize(R.dimen.ms_padding_left); 126 | int right = resources.getDimensionPixelSize(R.dimen.ms_padding_right); 127 | if (rtl) { 128 | right = resources.getDimensionPixelSize(R.dimen.ms_padding_left); 129 | } else { 130 | left = resources.getDimensionPixelSize(R.dimen.ms_padding_right); 131 | } 132 | 133 | setGravity(Gravity.CENTER_VERTICAL | Gravity.START); 134 | setClickable(true); 135 | setPadding(left, 0, right, 0); 136 | 137 | //设置 在界面中的显示样式,以及点击样式 138 | setBackgroundResource(R.drawable.ms_selector_style); 139 | 140 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && rtl) { 141 | setLayoutDirection(View.LAYOUT_DIRECTION_RTL); 142 | setTextDirection(View.TEXT_DIRECTION_RTL); 143 | } 144 | 145 | /**设置箭头布局 146 | * 147 | */ 148 | if (!hideArrow) { 149 | arrowDrawable = ContextCompat.getDrawable(context, R.drawable.ms_arrow).mutate(); 150 | arrowDrawable.setColorFilter(arrowColor, PorterDuff.Mode.SRC_IN); 151 | if (rtl) { 152 | setCompoundDrawablesWithIntrinsicBounds(arrowDrawable, null, null, null); 153 | } else { 154 | setCompoundDrawablesWithIntrinsicBounds(null, null, arrowDrawable, null); 155 | } 156 | } 157 | 158 | //创建布局 使用listView控件展示items 159 | listView = new ListView(context); 160 | listView.setId(getId()); 161 | listView.setDivider(null); 162 | listView.setItemsCanFocus(true); 163 | listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 164 | 165 | @Override 166 | public void onItemClick(AdapterView parent, View view, int position, long id) { 167 | if (position >= selectedIndex && position < adapter.getCount()) { 168 | position++; 169 | } 170 | selectedIndex = position; 171 | nothingSelected = false; 172 | Object item = adapter.get(position); 173 | adapter.notifyItemSelected(position); 174 | setText(item.toString()); 175 | collapse(); 176 | if (onItemSelectedListener != null) { 177 | //noinspection unchecked 178 | onItemSelectedListener.onItemSelected(MaterialSpinner.this, position, id, item); 179 | } 180 | } 181 | }); 182 | 183 | //使用PopupWindow控件承载数据 184 | popupWindow = new PopupWindow(context); 185 | popupWindow.setContentView(listView);// 186 | popupWindow.setOutsideTouchable(true); 187 | popupWindow.setFocusable(true); 188 | 189 | //设置背景色 190 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 191 | popupWindow.setElevation(16);//设置阴影 192 | popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.ms_popwindow_bg));// R.drawable.ms__drawable 193 | } else { 194 | popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.ms_popwindow_bg)); 195 | } 196 | 197 | //设置背景 198 | if (backgroundColor != Color.WHITE) { // default color is white 199 | setBackgroundColor(backgroundColor); 200 | } else if (backgroundSelector != 0) { 201 | //改变最底层颜色 202 | setBackgroundResource(backgroundSelector); 203 | } 204 | //数据显示颜色 205 | if (textColor != defaultColor) { 206 | setTextColor(textColor); 207 | } 208 | 209 | popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { 210 | 211 | @Override 212 | public void onDismiss() { 213 | if (nothingSelected && onNothingSelectedListener != null) { 214 | onNothingSelectedListener.onNothingSelected(MaterialSpinner.this); 215 | } 216 | if (!hideArrow) { 217 | animateArrow(false); 218 | } 219 | } 220 | }); 221 | } 222 | 223 | @Override 224 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 225 | 226 | popupWindow.setWidth(MeasureSpec.getSize(widthMeasureSpec)); 227 | popupWindow.setHeight(calculatePopupWindowHeight()); 228 | 229 | if (adapter != null) { 230 | CharSequence currentText = getText(); 231 | String longestItem = currentText.toString(); 232 | for (int i = 0; i < adapter.getCount(); i++) { 233 | String itemText = adapter.getItemText(i); 234 | if (itemText.length() > longestItem.length()) { 235 | longestItem = itemText; 236 | } 237 | } 238 | setText(longestItem); 239 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 240 | setText(currentText); 241 | } else { 242 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 243 | } 244 | } 245 | 246 | /** 247 | * 处理分发,该处处理简单,只对ACTION_UP做简单处理 248 | * 249 | * @param event 250 | * @return 251 | */ 252 | @Override 253 | public boolean onTouchEvent(@NonNull MotionEvent event) { 254 | if (event.getAction() == MotionEvent.ACTION_UP) { 255 | if (isEnabled() && isClickable()) { 256 | if (!popupWindow.isShowing()) { 257 | expand(); 258 | } else { 259 | collapse(); 260 | } 261 | } 262 | } 263 | return super.onTouchEvent(event); 264 | } 265 | 266 | /** 267 | * 重写 268 | * 269 | * @param color 270 | */ 271 | @Override 272 | public void setBackgroundColor(int color) { 273 | backgroundColor = color; 274 | Drawable background = getBackground(); 275 | if (background instanceof StateListDrawable) { // pre-L 276 | try { 277 | Method getStateDrawable = StateListDrawable.class.getDeclaredMethod("getStateDrawable", int.class); 278 | if (!getStateDrawable.isAccessible()) 279 | getStateDrawable.setAccessible(true); 280 | int[] colors = {Utils.darker(color, 0.85f), color}; 281 | for (int i = 0; i < colors.length; i++) { 282 | ColorDrawable drawable = (ColorDrawable) getStateDrawable.invoke(background, i); 283 | drawable.setColor(colors[i]); 284 | } 285 | } catch (Exception e) { 286 | Log.e("MaterialSpinner", "Error setting background color", e); 287 | } 288 | } else if (background != null) { // 21+ (RippleDrawable) 289 | background.setColorFilter(color, PorterDuff.Mode.SRC_IN); 290 | } 291 | popupWindow.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN); 292 | } 293 | 294 | /** 295 | * 重写 296 | * 297 | * @param color 298 | */ 299 | @Override 300 | public void setTextColor(int color) { 301 | textColor = color; 302 | super.setTextColor(color); 303 | } 304 | 305 | /** 306 | * 保存状态 307 | * 308 | * @return 309 | */ 310 | @Override 311 | public Parcelable onSaveInstanceState() { 312 | Bundle bundle = new Bundle(); 313 | bundle.putParcelable("state", super.onSaveInstanceState()); 314 | bundle.putInt("selected_index", selectedIndex); 315 | if (popupWindow != null) { 316 | bundle.putBoolean("is_popup_showing", popupWindow.isShowing()); 317 | collapse(); 318 | } else { 319 | bundle.putBoolean("is_popup_showing", false); 320 | } 321 | return bundle; 322 | } 323 | 324 | /** 325 | * 获取状态 326 | * 327 | * @param savedState 328 | */ 329 | @Override 330 | public void onRestoreInstanceState(Parcelable savedState) { 331 | if (savedState instanceof Bundle) { 332 | Bundle bundle = (Bundle) savedState; 333 | selectedIndex = bundle.getInt("selected_index"); 334 | if (adapter != null) { 335 | setText(adapter.get(selectedIndex).toString()); 336 | adapter.notifyItemSelected(selectedIndex); 337 | } 338 | if (bundle.getBoolean("is_popup_showing")) { 339 | if (popupWindow != null) { 340 | // Post the show request into the looper to avoid bad token exception 341 | post(new Runnable() { 342 | 343 | @Override 344 | public void run() { 345 | expand(); 346 | } 347 | }); 348 | } 349 | } 350 | savedState = bundle.getParcelable("state"); 351 | } 352 | super.onRestoreInstanceState(savedState); 353 | } 354 | 355 | @Override 356 | public void setEnabled(boolean enabled) { 357 | super.setEnabled(enabled); 358 | if (arrowDrawable != null) { 359 | arrowDrawable.setColorFilter(enabled ? arrowColor : arrowColorDisabled, PorterDuff.Mode.SRC_IN); 360 | } 361 | } 362 | 363 | /** 364 | * @return the selected item position 365 | */ 366 | public int getSelectedIndex() { 367 | return selectedIndex; 368 | } 369 | 370 | /** 371 | * Set the default spinner item using its index 372 | * 初始界面 常用 373 | * 374 | * @param position the item's position 375 | */ 376 | public void setSelectedIndex(int position) { 377 | if (adapter != null) { 378 | if (position >= 0 && position <= adapter.getCount()) { 379 | adapter.notifyItemSelected(position); 380 | selectedIndex = position; 381 | setText(adapter.get(position).toString()); 382 | } else { 383 | throw new IllegalArgumentException("Position must be lower than adapter count!"); 384 | } 385 | } 386 | } 387 | 388 | 389 | /** 390 | * Set the dropdown items 391 | * 392 | * @param items A list of items 393 | * @param The item type 394 | */ 395 | public void setItems(@NonNull T... items) { 396 | setItems(Arrays.asList(items)); 397 | } 398 | 399 | /** 400 | * Set the dropdown items 401 | * 402 | * @param items A list of items 403 | * @param The item type 404 | */ 405 | public void setItems(@NonNull List items) { 406 | adapter = new MaterialSpinnerAdapter<>(getContext(), items).setBackgroundSelector(backgroundSelector).setTextColor(textColor); 407 | setAdapterInternal(adapter); 408 | } 409 | 410 | /** 411 | * Get the list of items in the adapter 412 | * 413 | * @param The item type 414 | * @return A list of items or {@code null} if no items are set. 415 | */ 416 | public List getItems() { 417 | if (adapter == null) { 418 | return null; 419 | } 420 | //noinspection unchecked 421 | return adapter.getItems(); 422 | } 423 | 424 | /** 425 | * Set a custom adapter for the dropdown items 426 | * 427 | * @param adapter The list adapter 428 | */ 429 | public void setAdapter(@NonNull ListAdapter adapter) { 430 | this.adapter = new MaterialSpinnerAdapterWrapper(getContext(), adapter).setBackgroundSelector(backgroundSelector) 431 | .setTextColor(textColor); 432 | setAdapterInternal(this.adapter); 433 | } 434 | 435 | /** 436 | * Set the custom adapter for the dropdown items 437 | * 438 | * @param adapter The adapter 439 | * @param The type 440 | */ 441 | public void setAdapter(MaterialSpinnerAdapter adapter) { 442 | this.adapter = adapter; 443 | this.adapter.setTextColor(textColor); 444 | this.adapter.setBackgroundSelector(backgroundSelector); 445 | setAdapterInternal(adapter); 446 | } 447 | 448 | /** 449 | * 数据绑定+显示 450 | * 451 | * @param adapter 452 | */ 453 | private void setAdapterInternal(@NonNull MaterialSpinnerBaseAdapter adapter) { 454 | listView.setAdapter(adapter); 455 | if (selectedIndex >= adapter.getCount()) { 456 | selectedIndex = 0; 457 | } 458 | if (adapter.getCount() > 0) { 459 | setText(adapter.get(selectedIndex).toString()); 460 | } else { 461 | setText(""); 462 | } 463 | } 464 | 465 | /** 466 | * 展开 467 | */ 468 | public void expand() { 469 | if (!hideArrow) { 470 | animateArrow(true); 471 | } 472 | nothingSelected = true; 473 | popupWindow.showAsDropDown(this); 474 | } 475 | 476 | /** 477 | * 收起 478 | */ 479 | public void collapse() { 480 | if (!hideArrow) { 481 | animateArrow(false); 482 | } 483 | popupWindow.dismiss(); 484 | } 485 | 486 | /** 487 | * Set the tint color for the dropdown arrow 488 | * 489 | * @param color the color value 490 | */ 491 | public void setArrowColor(@ColorInt int color) { 492 | arrowColor = color; 493 | arrowColorDisabled = Utils.lighter(arrowColor, 0.8f); 494 | if (arrowDrawable != null) { 495 | arrowDrawable.setColorFilter(arrowColor, PorterDuff.Mode.SRC_IN); 496 | } 497 | } 498 | 499 | private void animateArrow(boolean shouldRotateUp) { 500 | int start = shouldRotateUp ? 0 : 10000; 501 | int end = shouldRotateUp ? 10000 : 0; 502 | ObjectAnimator animator = ObjectAnimator.ofInt(arrowDrawable, "level", start, end); 503 | animator.start(); 504 | } 505 | 506 | /** 507 | * 计算popupWindow控件 弹窗的高度 508 | * 509 | * @return 510 | */ 511 | private int calculatePopupWindowHeight() { 512 | if (adapter == null) { 513 | return WindowManager.LayoutParams.WRAP_CONTENT; 514 | } 515 | //计算出listView的总高度 516 | float listViewHeight = adapter.getCount() * getResources().getDimension(R.dimen.ms_item_height); 517 | 518 | //如果xml布局中设置了最高高度,且listViewHeight高度满足,优先使用最高高度 519 | if (popupWindowMaxHeight > 0 && listViewHeight > popupWindowMaxHeight) { 520 | 521 | return popupWindowMaxHeight; 522 | 523 | } else if (popupWindowHeight != WindowManager.LayoutParams.MATCH_PARENT 524 | && popupWindowHeight != WindowManager.LayoutParams.WRAP_CONTENT 525 | && popupWindowHeight <= listViewHeight) { 526 | return popupWindowHeight; 527 | } 528 | return WindowManager.LayoutParams.WRAP_CONTENT; 529 | } 530 | 531 | /** 532 | * Get the {@link PopupWindow}. 533 | * 534 | * @return The {@link PopupWindow} that is displayed when the view has been clicked. 535 | */ 536 | public PopupWindow getPopupWindow() { 537 | return popupWindow; 538 | } 539 | 540 | /** 541 | * Get the {@link ListView} that is used in the dropdown menu 542 | * 543 | * @return the ListView shown in the PopupWindow. 544 | */ 545 | public ListView getListView() { 546 | return listView; 547 | } 548 | 549 | 550 | /** 551 | * Interface definition for a callback to be invoked when an item in this view has been selected. 552 | * 553 | * @param Adapter item type 554 | */ 555 | public interface OnItemSelectedListener { 556 | 557 | /** 558 | *

Callback method to be invoked when an item in this view has been selected. This callback is invoked only when 559 | * the newly selected position is different from the previously selected position or if there was no selected 560 | * item.

561 | * 562 | * @param view The {@link MaterialSpinner} view 563 | * @param position The position of the view in the adapter 564 | * @param id The row id of the item that is selected 565 | * @param item The selected item 566 | */ 567 | void onItemSelected(MaterialSpinner view, int position, long id, T item); 568 | } 569 | 570 | /** 571 | * Register a callback to be invoked when an item in the dropdown is selected. 572 | * 573 | * @param onItemSelectedListener The callback that will run 574 | */ 575 | public void setOnItemSelectedListener(@Nullable OnItemSelectedListener onItemSelectedListener) { 576 | this.onItemSelectedListener = onItemSelectedListener; 577 | } 578 | 579 | /** 580 | * Interface definition for a callback to be invoked when the dropdown is dismissed and no item was selected. 581 | */ 582 | public interface OnNothingSelectedListener { 583 | 584 | /** 585 | * Callback method to be invoked when the {@link PopupWindow} is dismissed and no item was selected. 586 | * 587 | * @param spinner the {@link MaterialSpinner} 588 | */ 589 | void onNothingSelected(MaterialSpinner spinner); 590 | } 591 | 592 | /** 593 | * Register a callback to be invoked when the {@link PopupWindow} is shown but the user didn't select an item. 594 | * 595 | * @param onNothingSelectedListener the callback that will run 596 | */ 597 | public void setOnNothingSelectedListener(@Nullable OnNothingSelectedListener onNothingSelectedListener) { 598 | this.onNothingSelectedListener = onNothingSelectedListener; 599 | } 600 | } 601 | --------------------------------------------------------------------------------