├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── styles.xml │ │ │ │ └── colors.xml │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── test.jpg │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_search.png │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ ├── ic_search.png │ │ │ │ ├── ic_search_bg.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── drawable │ │ │ │ ├── sort_text_bg.xml │ │ │ │ ├── search_bg_stroke_shape.xml │ │ │ │ └── ic_launcher_background.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ └── layout │ │ │ │ ├── itemview_sort.xml │ │ │ │ └── activity_main.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── lzj │ │ │ └── sidebarviewdemo │ │ │ ├── bean │ │ │ └── SortBean.java │ │ │ ├── adapter │ │ │ └── SortAdapter.java │ │ │ ├── utils │ │ │ ├── SortComparator.java │ │ │ └── PinYinStringHelper.java │ │ │ └── MainActivity.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── lzj │ │ │ └── sidebarviewdemo │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── lzj │ │ └── sidebarviewdemo │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── sidebar ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ └── attrs.xml │ │ │ ├── drawable │ │ │ │ └── sort_text_view_hint_bg.xml │ │ │ └── layout │ │ │ │ └── view_sidebar_layout.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── lzj │ │ │ └── sidebar │ │ │ ├── SideBarLayout.java │ │ │ └── SideBarSortView.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── lzj │ │ │ └── sidebar │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── lzj │ │ └── sidebar │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── imgfolder └── gif.gif ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── README.md ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sidebar/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':sidebar' 2 | -------------------------------------------------------------------------------- /imgfolder/gif.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzjin/SideBarView/HEAD/imgfolder/gif.gif -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 字母索引 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzjin/SideBarView/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /sidebar/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | sidebar 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzjin/SideBarView/HEAD/app/src/main/res/mipmap-xxhdpi/test.jpg -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzjin/SideBarView/HEAD/app/src/main/res/mipmap-mdpi/ic_search.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzjin/SideBarView/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzjin/SideBarView/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzjin/SideBarView/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzjin/SideBarView/HEAD/app/src/main/res/mipmap-xhdpi/ic_search.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_search_bg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzjin/SideBarView/HEAD/app/src/main/res/mipmap-xhdpi/ic_search_bg.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzjin/SideBarView/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzjin/SideBarView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sidebar/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzjin/SideBarView/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/lzjin/SideBarView/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/lzjin/SideBarView/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/lzjin/SideBarView/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/lzjin/SideBarView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sidebar/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008AB8 4 | #FF765A 5 | #FFFFFF 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/sort_text_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | /.idea 3 | .gradle 4 | /local.properties 5 | /.idea/caches 6 | /.idea/libraries 7 | /.idea/modules.xml 8 | /.idea/workspace.xml 9 | /.idea/navEditor.xml 10 | /.idea/assetWizardSettings.xml 11 | .DS_Store 12 | /build 13 | /captures 14 | .externalNativeBuild 15 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Mar 30 16:25:32 CST 2023 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-6.5-all.zip 7 | -------------------------------------------------------------------------------- /sidebar/src/main/res/drawable/sort_text_view_hint_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/search_bg_stroke_shape.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /sidebar/src/test/java/com/lzj/sidebar/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.lzj.sidebar; 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() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/test/java/com/lzj/sidebarviewdemo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.lzj.sidebarviewdemo; 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() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | #FFA07A 8 | 9 | #FF8C00 10 | 11 | #FF7F50 12 | 13 | #FF69B4 14 | #8B0000 15 | 16 | #2B2B2B 17 | #999999 18 | #3D3D3D 19 | 20 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /sidebar/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 | -------------------------------------------------------------------------------- /sidebar/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /sidebar/src/androidTest/java/com/lzj/sidebar/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.lzj.sidebar; 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() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.lzj.sidebar.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/lzj/sidebarviewdemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.lzj.sidebarviewdemo; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.InstrumentationRegistry; 6 | import androidx.test.runner.AndroidJUnit4; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getTargetContext(); 24 | 25 | assertEquals("com.lzj.sidebarviewdemo", appContext.getPackageName()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app's APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Automatically convert third-party libraries to use AndroidX 19 | android.enableJetifier=true 20 | 21 | -------------------------------------------------------------------------------- /sidebar/src/main/res/layout/view_sidebar_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 21 | 22 | 29 | 30 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 17 | 18 | 21 | 22 | 25 | 26 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 29 5 | buildToolsVersion '29.0.2' 6 | defaultConfig { 7 | applicationId "com.lzj.sidebarviewdemo" 8 | minSdkVersion 19 9 | targetSdkVersion 28 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | compileOptions { 21 | sourceCompatibility = 1.8 22 | targetCompatibility = 1.8 23 | } 24 | } 25 | 26 | dependencies { 27 | implementation fileTree(include: ['*.jar'], dir: 'libs') 28 | implementation 'androidx.appcompat:appcompat:1.1.0' 29 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 30 | testImplementation 'junit:junit:4.12' 31 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 32 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 33 | implementation 'com.jakewharton:butterknife:10.2.1' 34 | annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1' 35 | implementation 'androidx.recyclerview:recyclerview:1.1.0' 36 | // 基础依赖包,必须要依赖 37 | implementation 'com.gyf.immersionbar:immersionbar:3.0.0' 38 | //汉字转拼音 39 | implementation 'com.belerweb:pinyin4j:2.5.1' 40 | implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.1' 41 | implementation project(':sidebar') 42 | 43 | } 44 | -------------------------------------------------------------------------------- /sidebar/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'maven-publish' 3 | 4 | afterEvaluate { 5 | publishing { 6 | publications { 7 | release(MavenPublication) { 8 | from components.release // 表示发布 release(jitpack 都不会使用到) 9 | groupId = 'com.lzj.sidebar' // 这个是依赖库的组 id 10 | artifactId = 'sidebar' // 依赖库的名称(jitpack 都不会使用到) 11 | version ="1.0.1" //当前版本依赖库版本号,这个jitpack不会使用到,只是我们开发者自己查看 12 | } 13 | } 14 | } 15 | } 16 | 17 | android { 18 | compileSdkVersion 29 19 | buildToolsVersion '29.0.2' 20 | defaultConfig { 21 | minSdkVersion 19 22 | targetSdkVersion 28 23 | versionCode 1 24 | versionName "1.0" 25 | 26 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 27 | 28 | } 29 | 30 | buildTypes { 31 | release { 32 | minifyEnabled false 33 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 34 | } 35 | } 36 | compileOptions { 37 | sourceCompatibility = 1.8 38 | targetCompatibility = 1.8 39 | // sourceCompatibility JavaVersion.VERSION_11 40 | // targetCompatibility JavaVersion.VERSION_11 41 | } 42 | 43 | 44 | 45 | } 46 | 47 | dependencies { 48 | implementation fileTree(dir: 'libs', include: ['*.jar']) 49 | 50 | implementation 'androidx.appcompat:appcompat:1.1.0' 51 | testImplementation 'junit:junit:4.12' 52 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 53 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/java/com/lzj/sidebarviewdemo/bean/SortBean.java: -------------------------------------------------------------------------------- 1 | package com.lzj.sidebarviewdemo.bean; 2 | 3 | import com.lzj.sidebarviewdemo.utils.PinYinStringHelper; 4 | 5 | public class SortBean { 6 | private String imgUrl; 7 | private String name; 8 | private String pinyin; 9 | private String jianpin; 10 | private String word;//首字母 11 | private String telephone; 12 | private String address; 13 | 14 | public SortBean(String name,String address) { 15 | this.address=address; 16 | this.name = name; 17 | this.pinyin=PinYinStringHelper.getPingYin(name);//全拼 18 | this.word = PinYinStringHelper.getAlpha(name);//大写首字母或特殊字符 19 | this.jianpin=PinYinStringHelper.getPinYinHeadChar(name);//简拼 20 | } 21 | 22 | public String getJianpin() { 23 | return jianpin; 24 | } 25 | 26 | public void setJianpin(String jianpin) { 27 | this.jianpin = jianpin; 28 | } 29 | 30 | public String getImgUrl() { 31 | return imgUrl; 32 | } 33 | 34 | public void setImgUrl(String imgUrl) { 35 | this.imgUrl = imgUrl; 36 | } 37 | 38 | public String getName() { 39 | return name; 40 | } 41 | 42 | public void setName(String name) { 43 | this.name = name; 44 | } 45 | 46 | public String getPinyin() { 47 | return pinyin; 48 | } 49 | 50 | public void setPinyin(String pinyin) { 51 | this.pinyin = pinyin; 52 | } 53 | 54 | public String getWord() { 55 | return word; 56 | } 57 | 58 | public void setWord(String word) { 59 | this.word = word; 60 | } 61 | 62 | public String getTelephone() { 63 | return telephone; 64 | } 65 | 66 | public void setTelephone(String telephone) { 67 | this.telephone = telephone; 68 | } 69 | 70 | public String getAddress() { 71 | return address; 72 | } 73 | 74 | public void setAddress(String address) { 75 | this.address = address; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SideBarView 2 | Android 字母索引View,类似电话联系人分类 3 | 4 | #### 博客讲解地址,欢迎前往查看 5 | [博客讲解地址](https://blog.csdn.net/lin857/article/details/105193760) 6 | 7 | ### 欢迎大家Star,老铁给鼓励呗 8 | 9 | ### 效果图如下: 10 | 11 | 12 | ### 主要功能 13 | * 支持侧边栏字母大小设置 14 | * 支持侧边栏字母选中、未选中颜色设置 15 | * 支持屏幕中间高亮TextView的字体大小、颜色、背景设置 16 | 17 | ### API方法介绍 18 | * onSideBarScrollUpdateItem("A") <== 侧边栏字母滑动 --> item 19 | * OnItemScrollUpdateText("B") <== item滑动 --> 侧边栏字母 20 | 21 | ### Jitpack 22 | 23 | --- 24 | Step 1. Add it in your root build.gradle at the end of repositories: 25 | ``` 26 | allprojects { 27 | repositories { 28 | ... 29 | maven { url 'https://jitpack.io' } 30 | } 31 | } 32 | ``` 33 | #### Gradle: 34 | Step 2. Add the dependency 35 | ``` 36 | dependencies { 37 | //androidX 版本使用下面的依赖 38 | implementation 'com.github.lzjin:SideBarView:1.0.1' 39 | } 40 | ``` 41 | 42 | #### 在布局文件中添加 SideBarLayout 43 | ``` 44 | 55 | ``` 56 | #### 侧边字母滑动回调,设置滚动控件item位置 57 | ``` 58 | sideBarLayout.setSideBarLayout(new SideBarLayout.OnSideBarLayoutListener() { 59 |             @Override 60 |             public void onSideBarScrollUpdateItem(String word) { 61 |                 //根据自己业务实现 62 |                 for (int i = 0; i < mList.size(); i++) { 63 |                     if (mList.get(i).getWord().equals(word)) { 64 |                         recyclerView.smoothScrollToPosition(i); 65 |                         break; 66 |                     } 67 |                 } 68 |             } 69 |         }); 70 | ``` 71 | #### 滚动控件item滑动设置侧边字母位置 72 | ``` 73 | sideBarLayout.OnItemScrollUpdateText(mList.get(firstItemPosition).getWord()); 74 | ``` 75 | ### 老铁都看这了,给个Star再走呗 76 | 77 | #### 1.0 基本版使用 78 | 79 | #### 1.0.1 修复bug 80 | * 重复调用问题 81 | * 优化示例的搜索 82 | -------------------------------------------------------------------------------- /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 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 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 Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /app/src/main/res/layout/itemview_sort.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 19 | 20 | 27 | 28 | 36 | 37 | 38 | 39 | 46 | 47 | 48 | 53 | 54 | 55 | 63 | 64 | 65 | 66 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /app/src/main/java/com/lzj/sidebarviewdemo/adapter/SortAdapter.java: -------------------------------------------------------------------------------- 1 | package com.lzj.sidebarviewdemo.adapter; 2 | 3 | import android.view.View; 4 | import com.chad.library.adapter.base.BaseQuickAdapter; 5 | import com.chad.library.adapter.base.viewholder.BaseViewHolder; 6 | import com.lzj.sidebarviewdemo.R; 7 | import com.lzj.sidebarviewdemo.bean.SortBean; 8 | import java.util.List; 9 | 10 | /** 11 | * 参考示例 12 | */ 13 | public class SortAdapter extends BaseQuickAdapter { 14 | private List mData; 15 | 16 | public SortAdapter(int layoutResId, List data) { 17 | super(layoutResId, data); 18 | mData=data; 19 | } 20 | 21 | @Override 22 | protected void convert(BaseViewHolder viewHolder, SortBean item) { 23 | //TODO 参考代码,请根据自身业务实现 24 | 25 | //第一个字母显示 26 | if (viewHolder.getLayoutPosition() == 0) { 27 | (viewHolder.getView(R.id.tv_key)).setVisibility(View.VISIBLE); 28 | } else { 29 | //然后判断当前姓名的首字母和上一个首字母是否相同,如果相同字母导航条就隐藏,否则就显示 30 | // if(mData.get(viewHolder.getLayoutPosition()).getWordAscii()==mData.get(viewHolder.getLayoutPosition()-1).getWordAscii()){ 31 | // (viewHolder.getView(R.id.tv_key)).setVisibility(View.GONE); 32 | // }else { 33 | // (viewHolder.getView(R.id.tv_key)).setVisibility(View.VISIBLE); 34 | // } 35 | 36 | //首字母和上一个首字母是否相同,如果相同字母导航条就影藏,否则就显示 37 | int section = getSectionForPosition(viewHolder.getLayoutPosition()); 38 | if (viewHolder.getLayoutPosition() == getPositionForSection(section)) { 39 | (viewHolder.getView(R.id.tv_key)).setVisibility(View.VISIBLE); 40 | } else { 41 | (viewHolder.getView(R.id.tv_key)).setVisibility(View.GONE); 42 | } 43 | } 44 | viewHolder.setText(R.id.tv_key, item.getWord()); 45 | viewHolder.setText(R.id.tv_name, item.getName()); 46 | viewHolder.setText(R.id.tv_address, item.getAddress()); 47 | } 48 | 49 | /** 50 | * 根据View的当前位置获取分类的首字母的Char ascii值 51 | */ 52 | public int getSectionForPosition(int position) { 53 | return getData().get(position).getWord().charAt(0); 54 | } 55 | 56 | /** 57 | * 获取第一次出现该首字母的List所在的位置 58 | */ 59 | public int getPositionForSection(int section) { 60 | for (int i = 0; i < getData().size(); i++) { 61 | String sortStr = getData().get(i).getWord(); 62 | char firstChar = sortStr.toUpperCase().charAt(0); 63 | if (firstChar == section) { 64 | return i; 65 | } 66 | } 67 | return -1; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 15 | 16 | 20 | 21 | 30 | 31 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 59 | 60 | 68 | 69 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /app/src/main/java/com/lzj/sidebarviewdemo/utils/SortComparator.java: -------------------------------------------------------------------------------- 1 | package com.lzj.sidebarviewdemo.utils; 2 | 3 | 4 | import com.lzj.sidebarviewdemo.bean.SortBean; 5 | 6 | import java.util.Comparator; 7 | import java.util.regex.Pattern; 8 | 9 | /** 10 | * Created by lzj on 2018/6/11. 11 | * 根据某字段排序 12 | */ 13 | 14 | public class SortComparator implements Comparator { 15 | private int compareCallNum = 0; 16 | 17 | @Override 18 | public int compare(Object o1, Object o2) { 19 | compareCallNum = 0; 20 | return compareString(((SortBean) o1).getName(), ((SortBean) o2).getName()); 21 | } 22 | 23 | private int compareString(String o1, String o2) { 24 | compareCallNum++; 25 | //若存在长度为0的情况: 26 | if ((o1.length() == 0) && (o2.length() == 0)) { 27 | return 0; 28 | } else if (o1.length() == 0) { 29 | return -1; 30 | } else if (o2.length() == 0) { 31 | return 1; 32 | } 33 | 34 | String firstStrA = o1.substring(0, 1); 35 | String firstStrB = o2.substring(0, 1); 36 | 37 | int typeA = getFirstCharType(o1); 38 | int typeB = getFirstCharType(o2); 39 | 40 | if (typeA > typeB) { 41 | return -1;//返回负值,则往前排 42 | } else if (typeA < typeB) { 43 | return 1; 44 | } 45 | 46 | //类型相同,需要进行进一步的比较 47 | int compareResult; 48 | 49 | if (typeA < 9 && typeB < 9) { 50 | compareResult = firstStrA.compareTo(firstStrB); 51 | if (compareResult != 0) { 52 | //若不同,立即出来比较结果 53 | return compareResult; 54 | } else { 55 | //若相同,则递归调用 56 | return compareString(o1.substring(1), o2.substring(1)); 57 | } 58 | } 59 | 60 | //是字母或汉字 61 | 62 | //若是首字母,先用第一个字母或拼音进行比较 63 | //否则,先判断字符类型 64 | String firstPinyinA = PinYinStringHelper.getFirstPingYin(o1).substring(0, 1); 65 | String firstPinyinB = PinYinStringHelper.getFirstPingYin(o2).substring(0, 1); 66 | if (compareCallNum == 1) { 67 | compareResult = firstPinyinA.compareTo(firstPinyinB); 68 | if (compareResult != 0) { 69 | return compareResult; 70 | } 71 | } 72 | //若首字的第一个字母相同,或不是首字,判断原字符是汉字还是字母,汉字排在前面 73 | typeA = getFirstCharType2(o1); 74 | typeB = getFirstCharType2(o2); 75 | if (typeA > typeB) { 76 | return -1; 77 | } else if (typeA < typeB) { 78 | return 1; 79 | } 80 | 81 | //不是首字母,在字符类型之后判断,第一个字母或拼音进行比较 82 | if (compareCallNum != 1) { 83 | compareResult = firstPinyinA.compareTo(firstPinyinB); 84 | if (compareResult != 0) { 85 | return compareResult; 86 | } 87 | } 88 | 89 | if (isLetter(o1) && isLetter(o2)) { 90 | //若是同一个字母,还要比较下大小写 91 | compareResult = firstStrA.compareTo(firstStrB); 92 | if (compareResult != 0) { 93 | return compareResult; 94 | } 95 | } 96 | 97 | if (isHanzi(o1) && isHanzi(o2)) { 98 | //使用姓的拼音进行比较 99 | // compareResult = firstPinyinA.compareTo(firstPinyinB); 100 | compareResult = PinYinStringHelper.getFirstPingYin(o1) 101 | .compareTo(PinYinStringHelper.getFirstPingYin(o2)); 102 | if (compareResult != 0) { 103 | return compareResult; 104 | } 105 | 106 | //若姓的拼音相同,比较汉字是否相同 107 | compareResult = firstStrA.compareTo(firstStrB); 108 | if (compareResult != 0) { 109 | return compareResult; 110 | } 111 | } 112 | //若相同,则进行下一个字符的比较(递归调用) 113 | return compareString(o1.substring(1), o2.substring(1)); 114 | } 115 | 116 | private int getFirstCharType2(String str) { 117 | if (isHanzi(str)) { 118 | return 10; 119 | } else if (isLetter(str)) { 120 | return 9; 121 | } else if (isNumber(str)) { 122 | return 8; 123 | } else { 124 | return 0; 125 | } 126 | } 127 | 128 | private int getFirstCharType(String str) { 129 | if (isHanzi(str)) { 130 | return 10; 131 | } else if (isLetter(str)) { 132 | return 10; 133 | } else if (isNumber(str)) { 134 | return 8; 135 | } else { 136 | return 0; 137 | } 138 | } 139 | 140 | private boolean isLetter(String str) { 141 | char c = str.charAt(0); 142 | // 正则表达式,判断首字母是否是英文字母 143 | Pattern pattern = Pattern.compile("^[A-Za-z]+$"); 144 | return pattern.matcher(c + "").matches(); 145 | } 146 | 147 | private boolean isNumber(String str) { 148 | char c = str.charAt(0); 149 | // 正则表达式,判断首字母是否是英文字母 150 | Pattern pattern = Pattern.compile("^[1-9]+$"); 151 | return pattern.matcher(c + "").matches(); 152 | } 153 | 154 | private boolean isHanzi(String str) { 155 | char c = str.charAt(0); 156 | // 正则表达式,判断首字母是否是英文字母 157 | Pattern pattern = Pattern.compile("[\\u4E00-\\u9FA5]+"); 158 | return pattern.matcher(c + "").matches(); 159 | } 160 | } 161 | -------------------------------------------------------------------------------- /sidebar/src/main/java/com/lzj/sidebar/SideBarLayout.java: -------------------------------------------------------------------------------- 1 | package com.lzj.sidebar; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Color; 6 | import android.graphics.drawable.Drawable; 7 | import android.util.AttributeSet; 8 | import android.util.Log; 9 | import android.util.TypedValue; 10 | import android.view.LayoutInflater; 11 | import android.view.View; 12 | import android.widget.RelativeLayout; 13 | import android.widget.TextView; 14 | 15 | import java.util.List; 16 | 17 | /** 18 | * Created by lzj on 2019/12/31 19 | * Describe :字母排序组合布局 20 | */ 21 | public class SideBarLayout extends RelativeLayout implements SideBarSortView.OnIndexChangedListener { 22 | private View mLayout; 23 | private Context mContext; 24 | private TextView mTvTips; 25 | private SideBarSortView mSortView; 26 | private int selectTextColor; 27 | private int unselectTextColor; 28 | private float selectTextSize; 29 | private float unselectTextSize; 30 | 31 | private int wordTextColor; 32 | private float wordTextSize; 33 | private Drawable wordBackground; 34 | 35 | public SideBarLayout(Context context) { 36 | super(context); 37 | } 38 | 39 | public SideBarLayout(Context context, AttributeSet attrs, int defStyleAttr) { 40 | super(context, attrs, defStyleAttr); 41 | init(context, attrs); 42 | initView(); 43 | 44 | } 45 | 46 | public SideBarLayout(Context context, AttributeSet attrs) { 47 | super(context, attrs); 48 | init(context, attrs); 49 | initView(); 50 | } 51 | 52 | private void init(Context context, AttributeSet attrs) { 53 | mContext = context; 54 | //获取自定义属性 55 | if (attrs != null) { 56 | TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.SideBarView); 57 | unselectTextColor = ta.getColor(R.styleable.SideBarView_sidebarUnSelectTextColor, Color.parseColor("#1ABDE6")); 58 | selectTextColor = ta.getColor(R.styleable.SideBarView_sidebarSelectTextColor, Color.parseColor("#2E56D7")); 59 | selectTextSize = ta.getDimension(R.styleable.SideBarView_sidebarSelectTextSize, dip2px(mContext, 12)); 60 | unselectTextSize = ta.getDimension(R.styleable.SideBarView_sidebarUnSelectTextSize, dip2px(mContext, 10)); 61 | 62 | wordTextSize = ta.getDimension(R.styleable.SideBarView_sidebarWordTextSize, px2sp(mContext, 45)); 63 | wordTextColor = ta.getColor(R.styleable.SideBarView_sidebarWordTextColor, Color.parseColor("#FFFFFF")); 64 | wordBackground = ta.getDrawable(R.styleable.SideBarView_sidebarWordBackground); 65 | if (wordBackground == null) { 66 | wordBackground = context.getResources().getDrawable(R.drawable.sort_text_view_hint_bg); 67 | } 68 | ta.recycle(); 69 | } 70 | } 71 | 72 | private void initView() { 73 | //引入布局 74 | mLayout = LayoutInflater.from(mContext).inflate(R.layout.view_sidebar_layout, null, true); 75 | mTvTips = (TextView) mLayout.findViewById(R.id.tvTips); 76 | mSortView = (SideBarSortView) mLayout.findViewById(R.id.sortView); 77 | mSortView.setIndexChangedListener(this); 78 | 79 | 80 | mSortView.setmTextColor(unselectTextColor); 81 | mSortView.setmTextSize(unselectTextSize); 82 | 83 | mSortView.setmTextColorChoose(selectTextColor); 84 | mSortView.setmTextSizeChoose(selectTextSize); 85 | mSortView.invalidate(); 86 | 87 | mTvTips.setTextColor(wordTextColor); 88 | mTvTips.setTextSize(px2sp(mContext, wordTextSize)); 89 | mTvTips.setBackground(wordBackground); 90 | this.addView(mLayout); //将子布局添加到父容器,才显示控件 91 | } 92 | 93 | 94 | /** 95 | * 监听回调:由侧边栏滑动更新Item 96 | */ 97 | 98 | private OnSideBarLayoutListener mListener; 99 | 100 | public static interface OnSideBarLayoutListener { 101 | void onSideBarScrollUpdateItem(String word); 102 | } 103 | 104 | public void setSideBarLayout(OnSideBarLayoutListener listener) { 105 | this.mListener = listener; 106 | } 107 | 108 | /** 109 | * 侧边栏滑动 更新Item 110 | * @param word 字母 111 | */ 112 | @Override 113 | public void onSideBarScrollUpdateItem(String word) { 114 | mTvTips.setVisibility(View.VISIBLE); 115 | mTvTips.setText(word); 116 | if (mListener != null) { 117 | mListener.onSideBarScrollUpdateItem(word); 118 | } 119 | } 120 | 121 | /** 122 | * 侧边栏滑动结束 隐藏提示 123 | */ 124 | @Override 125 | public void onSideBarScrollEndHideText() { 126 | mTvTips.setVisibility(View.GONE); 127 | } 128 | 129 | /** 130 | * Item滚动更新 侧边栏 131 | * @param word 132 | */ 133 | public void onItemScrollUpdateSideBarText(String word) { 134 | if (mListener != null) { 135 | mSortView.onUpdateSideBarText(word); 136 | } 137 | } 138 | 139 | public static int dip2px(Context context, float dpValue) { 140 | final float scale = context.getResources().getDisplayMetrics().density; 141 | return (int) (dpValue * scale + 0.5f); 142 | } 143 | 144 | public static int px2sp(Context context, float pxValue) { 145 | final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; 146 | return (int) (pxValue / fontScale + 0.5f); 147 | } 148 | 149 | } 150 | -------------------------------------------------------------------------------- /sidebar/src/main/java/com/lzj/sidebar/SideBarSortView.java: -------------------------------------------------------------------------------- 1 | package com.lzj.sidebar; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.graphics.Color; 6 | import android.graphics.Paint; 7 | import android.graphics.Typeface; 8 | import android.util.AttributeSet; 9 | import android.util.Log; 10 | import android.view.MotionEvent; 11 | import android.view.View; 12 | import android.view.ViewParent; 13 | 14 | import androidx.annotation.Nullable; 15 | 16 | /** 17 | * Created by lzj on 2019/12/31 18 | * Describe :字母排序索引 19 | */ 20 | public class SideBarSortView extends View { 21 | private Canvas mCanvas; 22 | private int mSelectIndex = 0; 23 | private float mTextSize; 24 | private int mTextColor; 25 | private float mTextSizeChoose; 26 | private int mTextColorChoose; 27 | //标记 避免重复调用 28 | private boolean isDown = false; 29 | 30 | public void setmTextSize(float mTextSize) { 31 | this.mTextSize = mTextSize; 32 | } 33 | 34 | public void setmTextColor(int mTextColor) { 35 | this.mTextColor = mTextColor; 36 | } 37 | 38 | public void setmTextSizeChoose(float mTextSizeChoose) { 39 | this.mTextSizeChoose = mTextSizeChoose; 40 | } 41 | 42 | public void setmTextColorChoose(int mTextColorChoose) { 43 | this.mTextColorChoose = mTextColorChoose; 44 | } 45 | 46 | public static String[] mList = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", 47 | "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"}; 48 | 49 | public Paint paint = new Paint(); 50 | 51 | public SideBarSortView(Context context) { 52 | super(context); 53 | } 54 | 55 | public SideBarSortView(Context context, @Nullable AttributeSet attrs) { 56 | super(context, attrs); 57 | } 58 | 59 | @Override 60 | protected void onDraw(Canvas canvas) { 61 | super.onDraw(canvas); 62 | this.mCanvas = canvas; 63 | paintText(); 64 | } 65 | 66 | private void paintText() { 67 | //计算每一个字母的高度,总告诉除以字母集合的高度就可以 68 | int height = (getHeight()) / mList.length; 69 | for (int i = 0; i < mList.length; i++) { 70 | if (i == mSelectIndex) { 71 | paint.setColor(mTextColorChoose); 72 | paint.setTextSize(mTextSizeChoose); 73 | } else { 74 | paint.setColor(mTextColor); 75 | paint.setTextSize(mTextSize); 76 | } 77 | paint.setAntiAlias(true);//设置抗锯齿 78 | paint.setTypeface(Typeface.DEFAULT_BOLD); 79 | //计算每一个字母x轴 80 | float paintX = getWidth() / 2F - paint.measureText(mList[i]) / 2; 81 | //计算每一个字母Y轴 82 | int paintY = height * i + height; 83 | //绘画出来这个TextView 84 | mCanvas.drawText(mList[i], paintX, paintY, paint); 85 | //画完一个以后重置画笔 86 | paint.reset(); 87 | } 88 | } 89 | 90 | @Override 91 | public boolean onTouchEvent(MotionEvent event) { 92 | ViewParent parent; 93 | switch (event.getAction()) { 94 | case MotionEvent.ACTION_DOWN: 95 | case MotionEvent.ACTION_MOVE: 96 | int index = (int) (event.getY() / getHeight() * mList.length); 97 | if (index >= 0 && index < mList.length && mSelectIndex != index) { 98 | if (mClickListener != null) { 99 | mClickListener.onSideBarScrollUpdateItem(mList[index]); 100 | } 101 | mSelectIndex = index; 102 | invalidate(); 103 | //改变标记状态 104 | isDown = true; 105 | } 106 | parent = getParent(); 107 | if (parent != null) { 108 | parent.requestDisallowInterceptTouchEvent(true);// 请求父级拦截,解决水平滑动冲突 109 | } 110 | break; 111 | case MotionEvent.ACTION_UP: 112 | case MotionEvent.ACTION_CANCEL: 113 | if (mClickListener != null) { 114 | mClickListener.onSideBarScrollEndHideText(); 115 | } 116 | //改变标记状态 117 | isDown = false; 118 | 119 | parent = getParent(); 120 | if (parent != null) { 121 | parent.requestDisallowInterceptTouchEvent(false);// 请求父级放行,解决水平滑动冲突 122 | } 123 | break; 124 | } 125 | return true; 126 | } 127 | 128 | private OnIndexChangedListener mClickListener; 129 | 130 | public static interface OnIndexChangedListener { 131 | //滚动位置 132 | void onSideBarScrollUpdateItem(String word); 133 | 134 | //隐藏提示文本 135 | void onSideBarScrollEndHideText(); 136 | } 137 | 138 | public void setIndexChangedListener(OnIndexChangedListener listener) { 139 | this.mClickListener = listener; 140 | } 141 | 142 | /** 143 | * Item滚动 更新侧边栏字母 144 | * 145 | * @param word 字母 146 | */ 147 | public void onUpdateSideBarText(String word) { 148 | //手指没触摸才调用 149 | if (!isDown) { 150 | for (int i = 0; i < mList.length; i++) { 151 | if (mList[i].equals(word) && mSelectIndex != i) { 152 | mSelectIndex = i; 153 | invalidate(); 154 | } 155 | } 156 | } 157 | } 158 | 159 | } 160 | 161 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/java/com/lzj/sidebarviewdemo/utils/PinYinStringHelper.java: -------------------------------------------------------------------------------- 1 | package com.lzj.sidebarviewdemo.utils; 2 | 3 | import net.sourceforge.pinyin4j.PinyinHelper; 4 | import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; 5 | import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; 6 | import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; 7 | import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; 8 | import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; 9 | 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | import java.util.regex.Pattern; 13 | 14 | 15 | public class PinYinStringHelper { 16 | //多音字替换为我们希望的同样发音的单音字,可以继续增加 17 | public static Map specialHanzi = new HashMap(); 18 | static { 19 | specialHanzi.put("重", "虫");//重->虫 20 | specialHanzi.put("贾", "甲");//贾->甲 21 | specialHanzi.put("瞿", "渠");//瞿->渠 22 | specialHanzi.put("单", "擅");//单->擅 23 | specialHanzi.put("沈", "审");//沈->审 24 | specialHanzi.put("解", "谢");//解->谢 25 | specialHanzi.put("俞", "于");//俞->于 26 | specialHanzi.put("曾", "增");//曾->增 27 | } 28 | /** 29 | * 得到 全拼 30 | */ 31 | public static String getPingYin(String src) { 32 | char[] t1 = null; 33 | 34 | if (src.trim().length() < 1) { 35 | return null; 36 | } 37 | //遇到多音字,先转换为单音字(根据指定的表),再取拼音 38 | String firstChar = src.trim().substring(0, 1); 39 | if (specialHanzi.containsKey(firstChar)) { 40 | src = src.replace(firstChar, specialHanzi.get(firstChar)); 41 | } 42 | 43 | t1 = src.toCharArray(); 44 | String[] t2 = new String[t1.length]; 45 | HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat(); 46 | t3.setCaseType(HanyuPinyinCaseType.LOWERCASE); 47 | t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 48 | t3.setVCharType(HanyuPinyinVCharType.WITH_V); 49 | String t4 = ""; 50 | int t0 = t1.length; 51 | try { 52 | for (int i = 0; i < t0; i++) { 53 | // 判断是否为汉字字符 54 | if (Character.toString(t1[i]).matches( 55 | "[\\u4E00-\\u9FA5]+")) { 56 | t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3); 57 | t4 += t2[0]; 58 | } else { 59 | t4 += Character.toString(t1[i]); 60 | } 61 | } 62 | return t4.toUpperCase(); 63 | } catch (BadHanyuPinyinOutputFormatCombination e1) { 64 | e1.printStackTrace(); 65 | } 66 | return t4.toUpperCase(); 67 | } 68 | 69 | public static String getFirstPingYin(String src) { 70 | char[] t1 = null; 71 | if (src.length() < 1) { 72 | return null; 73 | } 74 | 75 | if (!isHanzi(src)) { 76 | return src.substring(0, 1).toUpperCase(); 77 | } 78 | 79 | //遇到多音字,先转换为单音字(根据指定的表),再取拼音 80 | String firstChar = src.substring(0, 1); 81 | if (specialHanzi.containsKey(firstChar)) { 82 | src = specialHanzi.get(firstChar); 83 | // LogUtil.logWithMethod(new Exception(),"firstChar="+firstChar+" src="+src); 84 | } else { 85 | src = firstChar; 86 | } 87 | 88 | t1 = src.toCharArray(); 89 | String[] t2 = new String[t1.length]; 90 | HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat(); 91 | t3.setCaseType(HanyuPinyinCaseType.LOWERCASE); 92 | t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 93 | t3.setVCharType(HanyuPinyinVCharType.WITH_V); 94 | String t4 = ""; 95 | int t0 = t1.length; 96 | try { 97 | for (int i = 0; i < t0; i++) { 98 | // 判断是否为汉字字符 99 | if (Character.toString(t1[i]).matches( 100 | "[\\u4E00-\\u9FA5]+")) { 101 | t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3); 102 | t4 += t2[0]; 103 | } else { 104 | t4 += Character.toString(t1[i]); 105 | } 106 | } 107 | return t4.toUpperCase(); 108 | } catch (BadHanyuPinyinOutputFormatCombination e1) { 109 | e1.printStackTrace(); 110 | } 111 | return t4.toUpperCase(); 112 | } 113 | 114 | /** 115 | * 得到中文首字母缩写 116 | * 简拼 117 | */ 118 | public static String getPinYinHeadChar(String str) { 119 | if (str.trim().length() < 1) { 120 | return null; 121 | } 122 | StringBuilder convert = new StringBuilder(); 123 | for (int j = 0; j < str.length(); j++) { 124 | char word = str.charAt(j); 125 | String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word); 126 | if (pinyinArray != null) { 127 | convert.append(pinyinArray[0].charAt(0)); 128 | } else { 129 | convert.append(word); 130 | } 131 | } 132 | return convert.toString().toUpperCase(); 133 | } 134 | 135 | /** 136 | * 获得汉语拼音首字母 137 | */ 138 | public static String getAlpha(String str) { 139 | if (str == null) { 140 | return "#"; 141 | } 142 | if (str.trim().length() == 0) { 143 | return "#"; 144 | } 145 | if (!isHanzi(str) && !isLetter(str)) { 146 | return "#"; 147 | } 148 | char c = str.trim().substring(0, 1).charAt(0); 149 | // 正则表达式,判断首字母是否是英文字母 150 | Pattern pattern = Pattern.compile("^[A-Za-z]+$"); 151 | if (pattern.matcher(c + "").matches()) { 152 | return (c + "").toUpperCase(); 153 | } else { 154 | //汉字转拼音,获取拼音首字母 155 | String headChar = PinYinStringHelper.getHeadChar(str); 156 | if (headChar != null && headChar.length() > 0) { 157 | return headChar.substring(0, 1); 158 | } else { 159 | return "#"; 160 | } 161 | } 162 | } 163 | 164 | /** 165 | * 得到首字母 166 | */ 167 | public static String getHeadChar(String str) { 168 | if (str.trim().length() < 1) { 169 | return null; 170 | } 171 | //遇到多音字,先转换为单音字(根据指定的表),再取拼音 172 | String firstChar = str.trim().substring(0, 1); 173 | if (specialHanzi.containsKey(firstChar)) { 174 | str = specialHanzi.get(firstChar); 175 | } 176 | 177 | String convert = ""; 178 | char word = str.charAt(0); 179 | String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word); 180 | if (pinyinArray != null) { 181 | convert += pinyinArray[0].charAt(0); 182 | } else { 183 | convert += word; 184 | } 185 | return convert.toUpperCase(); 186 | } 187 | 188 | public static boolean isHanzi(String str) { 189 | char c = str.charAt(0); 190 | // 正则表达式,判断首字母是否是英文字母 191 | Pattern pattern = Pattern.compile("[\\u4E00-\\u9FA5]+"); 192 | if (pattern.matcher(c + "").matches()) { 193 | return true; 194 | } 195 | return false; 196 | } 197 | 198 | public static boolean isLetter(String str) { 199 | char c = str.charAt(0); 200 | // 正则表达式,判断首字母是否是英文字母 201 | Pattern pattern = Pattern.compile("^[A-Za-z]+$"); 202 | if (pattern.matcher(c + "").matches()) { 203 | return true; 204 | } 205 | return false; 206 | } 207 | } 208 | -------------------------------------------------------------------------------- /app/src/main/java/com/lzj/sidebarviewdemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.lzj.sidebarviewdemo; 2 | 3 | import android.os.Bundle; 4 | import android.text.Editable; 5 | import android.text.TextUtils; 6 | import android.text.TextWatcher; 7 | import android.util.Log; 8 | import android.widget.EditText; 9 | import android.widget.ImageView; 10 | 11 | import com.gyf.immersionbar.ImmersionBar; 12 | import com.lzj.sidebar.SideBarLayout; 13 | import com.lzj.sidebarviewdemo.adapter.SortAdapter; 14 | import com.lzj.sidebarviewdemo.bean.SortBean; 15 | import com.lzj.sidebarviewdemo.utils.SortComparator; 16 | 17 | import java.util.ArrayList; 18 | import java.util.Collections; 19 | import java.util.List; 20 | import java.util.regex.Matcher; 21 | import java.util.regex.Pattern; 22 | 23 | import androidx.annotation.NonNull; 24 | import androidx.appcompat.app.AppCompatActivity; 25 | import androidx.recyclerview.widget.LinearLayoutManager; 26 | import androidx.recyclerview.widget.RecyclerView; 27 | import butterknife.BindView; 28 | import butterknife.ButterKnife; 29 | 30 | /** 31 | * 参考示例 32 | */ 33 | public class MainActivity extends AppCompatActivity implements TextWatcher { 34 | @BindView(R.id.iv_back) 35 | ImageView ivBack; 36 | @BindView(R.id.edt_search) 37 | EditText edtSearch; 38 | @BindView(R.id.recyclerView) 39 | RecyclerView recyclerView; 40 | @BindView(R.id.sidebar) 41 | SideBarLayout sidebarView; 42 | SortAdapter mSortAdaper; 43 | List mList; 44 | private int mScrollState = -1; 45 | @Override 46 | protected void onCreate(Bundle savedInstanceState) { 47 | super.onCreate(savedInstanceState); 48 | setContentView(R.layout.activity_main); 49 | ButterKnife.bind(this); 50 | ImmersionBar.with(this).transparentStatusBar().fitsSystemWindows(false).statusBarDarkFont(false).init(); 51 | 52 | edtSearch.addTextChangedListener(this); 53 | mScrollState = -1; 54 | initData(); 55 | connectData(); 56 | } 57 | private void initData() { 58 | mList = new ArrayList<>(); 59 | createTestData(); 60 | //进行排序 61 | Collections.sort(mList, new SortComparator()); 62 | mSortAdaper = new SortAdapter(R.layout.itemview_sort, mList); 63 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); //设置LayoutManager为LinearLayoutManager 64 | recyclerView.setAdapter(mSortAdaper); 65 | recyclerView.setNestedScrollingEnabled(false);//解决滑动不流畅 66 | 67 | } 68 | private void connectData() { 69 | //侧边栏滑动 --> item 70 | sidebarView.setSideBarLayout(new SideBarLayout.OnSideBarLayoutListener() { 71 | @Override 72 | public void onSideBarScrollUpdateItem(String word) { 73 | //循环判断点击的拼音导航栏和集合中姓名的首字母,如果相同recyclerView就跳转指定位置 74 | for (int i = 0; i < mList.size(); i++) { 75 | if (mList.get(i).getWord().equals(word)) { 76 | recyclerView.smoothScrollToPosition(i); 77 | break; 78 | } 79 | } 80 | } 81 | }); 82 | //item滑动 --> 侧边栏 83 | recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { 84 | @Override 85 | public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int scrollState) { 86 | super.onScrollStateChanged(recyclerView, scrollState); 87 | mScrollState = scrollState; 88 | } 89 | 90 | @Override 91 | public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { 92 | super.onScrolled(recyclerView, dx, dy); 93 | if (mScrollState != -1) { 94 | //第一个可见的位置 95 | RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); 96 | //判断是当前layoutManager是否为LinearLayoutManager 97 | // 只有LinearLayoutManager才有查找第一个和最后一个可见view位置的方法 98 | int firstItemPosition=0; 99 | if (layoutManager instanceof LinearLayoutManager) { 100 | LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager; 101 | //获取第一个可见view的位置 102 | firstItemPosition = linearManager.findFirstVisibleItemPosition(); 103 | } 104 | 105 | sidebarView.onItemScrollUpdateSideBarText(mList.get(firstItemPosition).getWord()); 106 | if (mScrollState == RecyclerView.SCROLL_STATE_IDLE) { 107 | mScrollState = -1; 108 | } 109 | } 110 | } 111 | }); 112 | 113 | 114 | } 115 | 116 | @Override 117 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { 118 | 119 | } 120 | 121 | @Override 122 | public void onTextChanged(CharSequence s, int start, int before, int count) { 123 | 124 | } 125 | 126 | @Override 127 | public void afterTextChanged(Editable s) { 128 | if (mList == null || mList.size() <= 0) { 129 | return; 130 | } 131 | String keyWord = s.toString().trim(); 132 | Log.i("test","------------key="+keyWord); 133 | if (!TextUtils.isEmpty(keyWord)) { 134 | List searchList=matcherSearch(keyWord, mList); 135 | if (searchList.size() > 0) { 136 | sidebarView.onItemScrollUpdateSideBarText(searchList.get(0).getWord()); 137 | } 138 | mSortAdaper.setNewData(searchList); 139 | } else { 140 | sidebarView.onItemScrollUpdateSideBarText(mList.get(0).getWord()); 141 | mSortAdaper.setNewData(mList); 142 | } 143 | mSortAdaper.notifyDataSetChanged(); 144 | } 145 | 146 | /** 147 | * 匹配输入数据 148 | * 149 | * @param keyword 150 | * @param list 151 | * @return 152 | */ 153 | public List matcherSearch(String keyword, List list) { 154 | List results = new ArrayList<>(); 155 | String patten = Pattern.quote(keyword); 156 | Pattern pattern = Pattern.compile(patten, Pattern.CASE_INSENSITIVE); 157 | for (int i = 0; i < list.size(); i++) { 158 | //根据首字母 159 | Matcher matcherWord = pattern.matcher((list.get(i)).getWord()); 160 | //根据拼音 161 | Matcher matcherPin = pattern.matcher((list.get(i)).getPinyin()); 162 | //根据简拼 163 | Matcher matcherJianPin = pattern.matcher((list.get(i)).getJianpin()); 164 | //根据名字 165 | Matcher matcherName = pattern.matcher((list.get(i)).getName()); 166 | if (matcherWord.find() || matcherPin.find() || matcherName.find() || matcherJianPin.find()) { 167 | results.add(list.get(i)); 168 | } 169 | } 170 | 171 | return results; 172 | } 173 | 174 | 175 | /** 176 | * 创建测试数据 177 | */ 178 | private void createTestData(){ 179 | //A 180 | mList.add(new SortBean("阿三"," 成都敬江渠")); 181 | mList.add(new SortBean("阿刘"," 成都敬江渠")); 182 | mList.add(new SortBean("阿九"," 成都敬江渠")); 183 | //B 184 | mList.add(new SortBean("宝宝"," 成都敬江渠")); 185 | mList.add(new SortBean("包打听"," 成都敬江渠")); 186 | mList.add(new SortBean("豹子费"," 成都敬江渠")); 187 | //C 188 | mList.add(new SortBean("陈菲菲"," 成都敬江渠")); 189 | mList.add(new SortBean("陈大菲"," 成都敬江渠")); 190 | mList.add(new SortBean("车臣"," 成都敬江渠")); 191 | mList.add(new SortBean("程师妹"," 成都敬江渠")); 192 | //D 193 | mList.add(new SortBean("戴氏龙"," 成都敬江渠")); 194 | mList.add(new SortBean("德飞侠"," 成都敬江渠")); 195 | mList.add(new SortBean("刁峰"," 成都敬江渠")); 196 | //E 197 | mList.add(new SortBean("饿了么"," 成都敬江渠")); 198 | mList.add(new SortBean("恶人谷"," 成都敬江渠")); 199 | mList.add(new SortBean("额度"," 成都敬江渠")); 200 | //F 201 | mList.add(new SortBean("冯雨晴"," 成都敬江渠")); 202 | mList.add(new SortBean("飞儿"," 成都敬江渠")); 203 | // G 204 | mList.add(new SortBean("郭沫"," 成都敬江渠")); 205 | mList.add(new SortBean("果实李"," 成都敬江渠")); 206 | // H 207 | mList.add(new SortBean("海神"," 成都敬江渠")); 208 | mList.add(new SortBean("韩信"," 成都敬江渠")); 209 | mList.add(new SortBean("汉朝"," 成都敬江渠")); 210 | // I 211 | mList.add(new SortBean("I Love You"," 成都敬江渠")); 212 | // J 213 | mList.add(new SortBean("精忠报国"," 成都敬江渠")); 214 | mList.add(new SortBean("积极"," 成都敬江渠")); 215 | // K 216 | mList.add(new SortBean("康有为"," 成都敬江渠")); 217 | mList.add(new SortBean("康师傅"," 成都敬江渠")); 218 | // L 219 | mList.add(new SortBean("李白"," 成都敬江渠")); 220 | mList.add(new SortBean("李太白"," 成都敬江渠")); 221 | mList.add(new SortBean("李世民"," 成都敬江渠")); 222 | mList.add(new SortBean("林落"," 成都敬江渠")); 223 | // M 224 | mList.add(new SortBean("米老鼠"," 成都敬江渠")); 225 | mList.add(new SortBean("明日"," 成都敬江渠")); 226 | // N 227 | mList.add(new SortBean("你好啊"," 成都敬江渠")); 228 | // O 229 | mList.add(new SortBean("哦"," 成都敬江渠")); 230 | // P 231 | mList.add(new SortBean("骗你的"," 成都敬江渠")); 232 | // Q 233 | mList.add(new SortBean("情不自禁"," 成都敬江渠")); 234 | // R 235 | mList.add(new SortBean("日子不错"," 成都敬江渠")); 236 | // S 237 | mList.add(new SortBean("胜龙"," 成都敬江渠")); 238 | mList.add(new SortBean("神经病"," 成都敬江渠")); 239 | // T 240 | mList.add(new SortBean("提示"," 成都敬江渠")); 241 | mList.add(new SortBean("腾讯"," 成都敬江渠")); 242 | // U 243 | mList.add(new SortBean("U YB"," 成都敬江渠")); 244 | // V 245 | mList.add(new SortBean("V字头"," 成都敬江渠")); 246 | // W 247 | mList.add(new SortBean("王老三"," 成都敬江渠")); 248 | mList.add(new SortBean("王东的"," 成都敬江渠")); 249 | // X 250 | mList.add(new SortBean("新人"," 成都敬江渠")); 251 | mList.add(new SortBean("洗漱"," 成都敬江渠")); 252 | // Y 253 | mList.add(new SortBean("阳光"," 成都敬江渠")); 254 | mList.add(new SortBean("杨家枪"," 成都敬江渠")); 255 | // Z 256 | mList.add(new SortBean("张三"," 成都敬江渠")); 257 | mList.add(new SortBean("张龙"," 成都敬江渠")); 258 | mList.add(new SortBean("张笑龙"," 成都敬江渠")); 259 | // # 260 | } 261 | } 262 | --------------------------------------------------------------------------------