├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── qiangxi │ │ └── switchviewproject │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── qiangxi │ │ │ └── switchviewproject │ │ │ └── MainActivity.java │ └── res │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── qiangxi │ └── switchviewproject │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── image ├── image.png ├── 效果2.gif ├── 效果3.png └── 默认.gif ├── settings.gradle └── switchview ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src ├── androidTest └── java │ └── com │ └── qiangxi │ └── switchview │ └── ExampleInstrumentedTest.java ├── main ├── AndroidManifest.xml ├── java │ └── com │ │ └── qiangxi │ │ └── switchview │ │ ├── SwitchView.java │ │ └── callback │ │ └── OnItemSelectedListener.java └── res │ ├── drawable │ └── bg_selected_drawable.xml │ └── values │ ├── attrs.xml │ └── strings.xml └── test └── java └── com └── qiangxi └── switchview └── ExampleUnitTest.java /.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/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 1.8 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SwitchViewProject 2 | ## 对应的文章 3 | [关于SwitchView简单解读的文章](http://blog.csdn.net/qiang_xi/article/details/74942386) 4 | ### 效果图: 5 | ##### 默认效果 6 | ![默认效果](https://github.com/qiangxi/SwitchViewProject/blob/master/image/%E9%BB%98%E8%AE%A4.gif?raw=true) 7 | ##### 自定义文本及字体颜色、大小 8 | ![效果1](https://github.com/qiangxi/SwitchViewProject/blob/master/image/%E6%95%88%E6%9E%9C3.png?raw=true) 9 | ##### 自定义插值器,锁定位置(锁定位置为2),滑动时不展示文本 10 | ![效果2](https://github.com/qiangxi/SwitchViewProject/blob/master/image/%E6%95%88%E6%9E%9C2.gif?raw=true) 11 | 12 | ### 更新日志 13 | #### 2017-07-11 14 | 发布第一个稳定版本V1.0.0。 15 |   16 | ### 集成方式 17 | #### gradle: 18 | ```grooxy 19 | compile 'com.qiangxi.switchview:switchview:1.0.0' 20 | ``` 21 | #### maven: 22 | ```maven 23 | 24 | com.qiangxi.switchview 25 | switchview 26 | 1.0.0 27 | pom 28 | 29 | ``` 30 | ### API说明 31 | API | API调用时机 | 对应的xml属性|作用 32 | ---|---|---|--- 33 | setOnItemSelectedListener(OnItemSelectedListener listener) | view初始化后的任意时刻|无|设置item选中监听,当item被选中时触发回调 34 | setScrollEnable(boolean scrollEnable)|view初始化后的任意时刻|app:scrollEnable="boolean"|设置是否禁用滑动手势【true:不禁用;false:禁用;默认为true】 35 | smoothScrollTo(int position)|需要动态滑动时调用|无|平滑移动到指定位置【注意与setDefaultSelectedPosition(int position)方法区分开来。】 36 | setShowTextWhenScrolling(boolean isShowText)|view初始化后的任意时刻|app:showTextWhenScrolling="boolean"|滑块在滑动时是否在滑块上显示文本【true:显示;false:不显示;默认为true】 37 | setInterpolator(TimeInterpolator interpolator)|view初始化后的任意时刻|无|设置滑动插值器 38 | setLockPosition(int position)|view初始化后的任意时刻|app:lockedPosition="interger"|指定锁定位置【位置被锁定后,该位置不可点击,且滑块滑动到该位置松开后,会自动滑动到原位置;只能指定一个位置为锁定位置】 39 | unlockPosition()|view初始化后的任意时刻|无|解锁被锁定的位置【解锁后,所有位置都自由了】 40 | getLockPosition()|view初始化后的任意时刻|无|获取被锁定的位置【未设置锁定位置时返回-1】 41 | getTotalItemCount()|view初始化后的任意时刻|无|获取item总数量 42 | setNormalTextColor(int normalTextColor)|view初始化后的任意时刻|app:normalTextColor="color"|设置默认文本颜色【未设置则使用默认】 43 | setNormalTextSize(float normalTextSize)|view初始化后的任意时刻|app:normalTextSize="dimension"|设置默认文本字体大小【未设置则使用默认】 44 | setSelectedTextColor(int selectedTextColor)|view初始化后的任意时刻|app:selectedTextColor="color"|设置选中文本颜色【未设置则使用默认】 45 | setSelectedTextSize(float selectedTextSize)|view初始化后的任意时刻|app:selectedTextSize="dimension"|设置选中文本字体大小【未设置则使用默认】 46 | setSelectedDrawableResId(@DrawableRes int selectedDrawableResId)|view初始化后的任意时刻|app:selectedDrawableResId="integer"|设置选中的item的背景drawable【未设置则使用默认】 47 | setSelectedBgMarginArray(int[] selectedBgMarginArray) |view初始化时调用|无|设置滑块距离四周的margin值【整型数组,位置对应关系:[left,top,right,bottom],数组长度必须为4】 48 | setDefaultSelectedPosition(int position)|view初始化时调用|app:defaultSelectedPosition="integer"|设置滑块初始位置【未设置则使用默认值,即0位置】 49 | setTextArray(String[] textArray)|view初始化时调用|app:textArray="reference"|设置文本内容【使用xml属性时,按照如下方式使用:app:textArray="@array/customTextArray"】 50 | getSlideView()|view初始化后的任意时刻|无|获取滑块view【目前是TextView,将来可能会继承TextView自定义一个SlideView】 51 | setEnable(boolean enable)|view初始化后的任意时刻|无|是否禁用所有手势【true:不禁用;false:禁用;禁用之后,不在响应任何手势,包括点击和滑动】 52 | isEnable()|view初始化后的任意时刻|无|是否已经禁用所有手势 53 |   54 | ### 基本使用方式 55 | ```java 56 | public class MainActivity extends AppCompatActivity { 57 | private String[] textArray = {"文本一", "文本二", "文本三", "文本四", "文本五"}; 58 | private SwitchView mSwitchView; 59 | 60 | private int[] marginArray = {20, 20, 20, 20}; 61 | private boolean isRequestSuccess; 62 | 63 | @Override 64 | protected void onCreate(Bundle savedInstanceState) { 65 | super.onCreate(savedInstanceState); 66 | setContentView(R.layout.activity_main); 67 | mSwitchView = (SwitchView) findViewById(switchView); 68 | // mSwitchView.setTextArray(textArray); 69 | // mSwitchView.setNormalTextColor(Color.GREEN); 70 | // mSwitchView.setNormalTextSize(15); 71 | // mSwitchView.setSelectedDrawableResId(R.mipmap.ic_launcher); 72 | // mSwitchView.setSelectedTextColor(Color.BLUE); 73 | // mSwitchView.setSelectedTextSize(18); 74 | 75 | // mSwitchView.setLockPosition(2); 76 | // mSwitchView.setSelectedBgMarginArray(marginArray); 77 | // mSwitchView.setDefaultSelectedPosition(0); 78 | // mSwitchView.setInterpolator(new OvershootInterpolator()); 79 | // mSwitchView.setShowTextWhenScrolling(false); 80 | mSwitchView.setOnItemSelectedListener(new OnItemSelectedListener() { 81 | @Override 82 | public void onItemSelected(int currentSelectedPosition, final int lastSelectedPosition) { 83 | //模拟网络请求 84 | new Handler().postDelayed(new TimerTask() { 85 | @Override 86 | public void run() { 87 | if (isRequestSuccess) { 88 | //若请求成功,do nothing 89 | } else { 90 | //若请求失败,调用如下方法移动到上一个位置 91 | mSwitchView.smoothScrollTo(lastSelectedPosition); 92 | } 93 | } 94 | }, 2000); 95 | // Toast.makeText(MainActivity.this, "currentSelectedPosition=" + currentSelectedPosition + ",lastSelectedPosition=" + lastSelectedPosition, Toast.LENGTH_SHORT).show(); 96 | } 97 | }); 98 | } 99 | 100 | public void click(View view) { 101 | 102 | // mSwitchView.setScrollEnable(false); 103 | // mSwitchView.setNormalTextColor(Color.GREEN); 104 | // mSwitchView.setNormalTextSize(25); 105 | // mSwitchView.setSelectedDrawableResId(R.mipmap.ic_launcher); 106 | // mSwitchView.setSelectedTextColor(Color.BLUE); 107 | // mSwitchView.setSelectedTextSize(50); 108 | 109 | 110 | // mSwitchView.unlockPosition();//有效 111 | 112 | } 113 | } 114 | ``` 115 | ### 最后 116 | 如果该项目对你有帮助,还请不吝赏赐star或fork。也欢迎提出issue或pr,一起维护这个项目。  117 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.0" 6 | defaultConfig { 7 | applicationId "com.qiangxi.switchviewproject" 8 | minSdkVersion 16 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | dataBinding { 21 | enabled = true 22 | } 23 | } 24 | 25 | dependencies { 26 | compile fileTree(include: ['*.jar'], dir: 'libs') 27 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 28 | exclude group: 'com.android.support', module: 'support-annotations' 29 | }) 30 | compile 'com.android.support:appcompat-v7:25.0.0' 31 | testCompile 'junit:junit:4.12' 32 | compile project(':switchview') 33 | } 34 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Android\AndroidSDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/qiangxi/switchviewproject/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.qiangxi.switchviewproject; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.qiangxi.switchviewproject", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/qiangxi/switchviewproject/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.qiangxi.switchviewproject; 2 | 3 | import android.os.Bundle; 4 | import android.os.Handler; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | 8 | import com.qiangxi.switchview.SwitchView; 9 | import com.qiangxi.switchview.callback.OnItemSelectedListener; 10 | 11 | import java.util.TimerTask; 12 | 13 | import static com.qiangxi.switchviewproject.R.id.switchView; 14 | 15 | public class MainActivity extends AppCompatActivity { 16 | private String[] textArray = {"文本一", "文本二", "文本三", "文本四", "文本五"}; 17 | private SwitchView mSwitchView; 18 | 19 | private int[] marginArray = {20, 20, 20, 20}; 20 | private boolean isRequestSuccess; 21 | 22 | @Override 23 | protected void onCreate(Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | setContentView(R.layout.activity_main); 26 | mSwitchView = (SwitchView) findViewById(switchView); 27 | // mSwitchView.setTextArray(textArray); 28 | // mSwitchView.setNormalTextColor(Color.GREEN); 29 | // mSwitchView.setNormalTextSize(15); 30 | // mSwitchView.setSelectedDrawableResId(R.mipmap.ic_launcher); 31 | // mSwitchView.setSelectedTextColor(Color.BLUE); 32 | // mSwitchView.setSelectedTextSize(18); 33 | 34 | // mSwitchView.setLockPosition(2); 35 | // mSwitchView.setSelectedBgMarginArray(marginArray); 36 | // mSwitchView.setDefaultSelectedPosition(0); 37 | // mSwitchView.setInterpolator(new OvershootInterpolator()); 38 | // mSwitchView.setShowTextWhenScrolling(false); 39 | mSwitchView.setOnItemSelectedListener(new OnItemSelectedListener() { 40 | @Override 41 | public void onItemSelected(int currentSelectedPosition, final int lastSelectedPosition) { 42 | //模拟网络请求 43 | new Handler().postDelayed(new TimerTask() { 44 | @Override 45 | public void run() { 46 | if (isRequestSuccess) { 47 | //若请求成功,do nothing 48 | } else { 49 | //若请求失败,调用如下方法移动到上一个位置 50 | mSwitchView.smoothScrollTo(lastSelectedPosition); 51 | } 52 | } 53 | }, 2000); 54 | // Toast.makeText(MainActivity.this, "currentSelectedPosition=" + currentSelectedPosition + ",lastSelectedPosition=" + lastSelectedPosition, Toast.LENGTH_SHORT).show(); 55 | } 56 | }); 57 | } 58 | 59 | public void click(View view) { 60 | 61 | // mSwitchView.setScrollEnable(false); 62 | // mSwitchView.setNormalTextColor(Color.GREEN); 63 | // mSwitchView.setNormalTextSize(25); 64 | // mSwitchView.setSelectedDrawableResId(R.mipmap.ic_launcher); 65 | // mSwitchView.setSelectedTextColor(Color.BLUE); 66 | // mSwitchView.setSelectedTextSize(50); 67 | 68 | 69 | // mSwitchView.unlockPosition();//有效 70 | 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 15 | 16 |