diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser
new file mode 100644
index 0000000..15fe9d7
Binary files /dev/null and b/.idea/caches/build_file_checksums.ser differ
diff --git a/.idea/checkstyle-idea.xml b/.idea/checkstyle-idea.xml
deleted file mode 100644
index 183ec08..0000000
--- a/.idea/checkstyle-idea.xml
+++ /dev/null
@@ -1,15 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml
new file mode 100644
index 0000000..30aa626
--- /dev/null
+++ b/.idea/codeStyles/Project.xml
@@ -0,0 +1,29 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/compiler.xml b/.idea/compiler.xml
deleted file mode 100644
index 96cc43e..0000000
--- a/.idea/compiler.xml
+++ /dev/null
@@ -1,22 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml
deleted file mode 100644
index e7bedf3..0000000
--- a/.idea/copyright/profiles_settings.xml
+++ /dev/null
@@ -1,3 +0,0 @@
-
-
-
\ No newline at end of file
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
deleted file mode 100644
index 97626ba..0000000
--- a/.idea/encodings.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 0a0c222..99202cc 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -1,18 +1,16 @@
-
-
-
@@ -27,29 +25,10 @@
-
+
-
-
-
-
-
-
-
- 1.8
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
index f702f35..dfb3ec7 100644
--- a/.idea/modules.xml
+++ b/.idea/modules.xml
@@ -2,7 +2,7 @@
-
+
diff --git a/.idea/qaplug_profiles.xml b/.idea/qaplug_profiles.xml
deleted file mode 100644
index e24d63d..0000000
--- a/.idea/qaplug_profiles.xml
+++ /dev/null
@@ -1,289 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/app/build.gradle b/app/build.gradle
index 35b2f51..7acf9c8 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -2,7 +2,6 @@ apply plugin: 'com.android.application'
android {
compileSdkVersion 25
- buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
applicationId "com.lib.kingja.switchbutton"
@@ -18,15 +17,23 @@ android {
}
}
+ testOptions{
+ unitTests{
+ includeAndroidResources = true
+ }
+ }
+
lintOptions {
abortOnError false
}
+ buildToolsVersion '27.0.3'
}
dependencies {
- compile fileTree(include: ['*.jar'], dir: 'libs')
- testCompile 'junit:junit:4.12'
- compile project(':libk-switchbutton')
+ implementation fileTree(include: ['*.jar'], dir: 'libs')
+ testImplementation 'junit:junit:4.12'
+ implementation project(':libk-switchbutton')
// compile 'lib.kingja.switchbutton:switchbutton:1.1.6'
- compile "com.android.support:appcompat-v7:$rootProject.ext.supportVersion"
+ implementation "com.android.support:appcompat-v7:$rootProject.ext.supportVersion"
+ testImplementation "org.robolectric:robolectric:3.8"
}
diff --git a/app/src/androidTest/java/com/kingja/switchbutton/ApplicationTest.java b/app/src/androidTest/java/com/kingja/switchbutton/ApplicationTest.java
deleted file mode 100644
index c998a7e..0000000
--- a/app/src/androidTest/java/com/kingja/switchbutton/ApplicationTest.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package com.kingja.switchbutton;
-
-import android.app.Application;
-import android.test.ApplicationTestCase;
-
-/**
- * Testing Fundamentals
- */
-public class ApplicationTest extends ApplicationTestCase {
- public ApplicationTest() {
- super(Application.class);
- }
-}
\ No newline at end of file
diff --git a/app/src/main/java/com/kingja/switchbutton/MainActivity.java b/app/src/main/java/com/kingja/switchbutton/MainActivity.java
index 849b3a6..0a9dfac 100644
--- a/app/src/main/java/com/kingja/switchbutton/MainActivity.java
+++ b/app/src/main/java/com/kingja/switchbutton/MainActivity.java
@@ -19,6 +19,10 @@
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
+import android.view.View;
+import android.widget.Button;
+import android.widget.CompoundButton;
+import android.widget.Switch;
import android.widget.Toast;
import com.lib.kingja.switchbutton.R;
@@ -36,25 +40,82 @@
*/
public class MainActivity extends AppCompatActivity {
- private String[] tabTexts1 = {"才子", "帅哥", "大湿", "猛将兄"};
+ private String[] tabTexts1 = {"test1", "test2", "test3", "test4"};
private String[] tabTexts4 = {"已经", "在家", "等你"};
+ private SwitchMultiButton smb;
+ private boolean showToast = false;
+
+ private SwitchMultiButton.OnSwitchListener onSwitchListener = new SwitchMultiButton.OnSwitchListener() {
+ @Override
+ public void onSwitch(int position, String tabText) {
+ //Toast.makeText(MainActivity.this, tabText, Toast.LENGTH_SHORT).show();
+ }
+ };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
- ((SwitchMultiButton) findViewById(R.id.switchmultibutton1)).setText(tabTexts1).setOnSwitchListener(onSwitchListener);
+ smb = ((SwitchMultiButton) findViewById(R.id.switchmultibutton1));
+ smb.setText(tabTexts1).setOnSwitchListener(onSwitchListener);
+
+ Integer in[] = new Integer[]{0, 2};
+
+ for (Integer i : in) {
+ smb.setSelectedTab(i);
+ }
+
((SwitchMultiButton) findViewById(R.id.switchmultibutton2)).setText("点个Star", "狠心拒绝").setOnSwitchListener(onSwitchListener);
((SwitchMultiButton) findViewById(switchmultibutton3)).setOnSwitchListener(onSwitchListener).setSelectedTab(1);
((SwitchMultiButton) findViewById(R.id.switchmultibutton4)).setText(tabTexts4).setOnSwitchListener(onSwitchListener);
- }
- private SwitchMultiButton.OnSwitchListener onSwitchListener = new SwitchMultiButton.OnSwitchListener() {
- @Override
- public void onSwitch(int position, String tabText) {
- Toast.makeText(MainActivity.this, tabText, Toast.LENGTH_SHORT).show();
- }
- };
+ Button unselect = (Button) findViewById(R.id.button);
+ Button select = (Button) findViewById(R.id.button2);
+ Switch sw = (Switch) findViewById(R.id.switch1);
+
+ unselect.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ smb.clearSelected();
+ }
+ });
+
+ select.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ Runnable r = new Runnable() {
+ @Override
+ public void run() {
+ for (int i = 0; i < tabTexts1.length; i++) {
+ final int ii[] = new int[1];
+ ii[0] = i;
+ smb.setSelectedTab(ii[0]);
+ }
+ }
+ };
+ new Thread(r).run();
+ }
+ });
+
+ sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+ @Override
+ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+ showToast = isChecked;
+ }
+ });
+
+ smb.setOnSwitchListener(new SwitchMultiButton.OnSwitchListener() {
+ @Override
+ public void onSwitch(int position, String tabText) {
+ if (showToast)
+ Toast.makeText(getBaseContext(), tabText + " pressed", Toast.LENGTH_SHORT).show();
+ }
+ });
+ }
+
+ public SwitchMultiButton getSwitchMultiButton() {
+ return smb;
+ }
}
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index cfcca24..78dbbd4 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -1,11 +1,11 @@
+ xmlns:app="http://schemas.android.com/apk/res-auto"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:gravity="center_horizontal"
+ android:orientation="vertical"
+ android:padding="16dp">
-
+ app:textSize="14sp" />
+ app:textSize="14sp" />
+ app:textSize="14sp"
+ app:typeface="tielanti.ttf" />
+ app:textSize="14sp" />
+ app:typeface="DeVinneTxtBT.ttf" />
+
+
+
+
+
+
+
+
+
+
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 1ac558f..8d4a34e 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -1,3 +1,6 @@
SwitchButton
+ Toasts
+ Select all
+ Unselect all
diff --git a/app/src/test/java/com/kingja/switchbutton/ApplicationTest.java b/app/src/test/java/com/kingja/switchbutton/ApplicationTest.java
new file mode 100644
index 0000000..de1abdb
--- /dev/null
+++ b/app/src/test/java/com/kingja/switchbutton/ApplicationTest.java
@@ -0,0 +1,100 @@
+package com.kingja.switchbutton;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+
+import lib.kingja.switchbutton.SwitchMultiButton;
+
+import static junit.framework.Assert.assertFalse;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Testing Fundamentals
+ */
+@RunWith(RobolectricTestRunner.class)
+public class ApplicationTest {
+
+ private MainActivity a;
+ private SwitchMultiButton smb;
+
+ @Before
+ public void setUp() {
+ a = Robolectric.setupActivity(MainActivity.class);
+ smb = a.getSwitchMultiButton();
+ }
+
+ @Test
+ public void testGetSelectedButtons() {
+ Integer in[] = new Integer[]{0, 2};
+ StringBuilder sb = new StringBuilder();
+ sb.append("in: ");
+
+ for (Integer i : in) {
+ smb.setSelectedTab(i);
+ sb.append(i).append(" ");
+ }
+
+ sb.append("out: ");
+
+ Integer out[] = smb.getSelectedTabs();
+
+ for (Integer i : out) {
+ sb.append(i).append(" ");
+ }
+
+ assertArrayEquals(sb.toString(), in, out);
+ }
+
+ @Test
+ public void testSelectAndUnselectButtons() {
+ Integer in[] = new Integer[]{0, 1, 2, 3};
+
+ for (Integer i : in) {
+ smb.setSelectedTab(i);
+ }
+
+ Integer out[] = smb.getSelectedTabs();
+
+ assertArrayEquals(in, out);
+
+ smb.clearSelected();
+
+ assertArrayEquals(smb.getSelectedTabs(), new Integer[]{});
+
+ for (Integer i : in) {
+ if (i % 2 == 1)
+ smb.setSelectedTab(i);
+ }
+
+ assertArrayEquals(smb.getSelectedTabs(), new Integer[]{1, 3});
+ }
+
+ @Test
+ public void testGetButtonStatus() {
+ smb.clearSelected();
+
+ Integer in[] = new Integer[]{0, 3};
+ for (Integer i : in) {
+ smb.setSelectedTab(i);
+ }
+ assertTrue(smb.getState(0));
+ assertTrue(smb.getState(3));
+ assertFalse(smb.getState(1));
+ assertFalse(smb.getState(2));
+
+ smb.clearSelected();
+
+ assertFalse(smb.getState(0));
+ assertFalse(smb.getState(3));
+
+ smb.setSelectedTab(1);
+ smb.setSelectedTab(2);
+
+ assertTrue(smb.getState(1));
+ assertTrue(smb.getState(2));
+ }
+}
\ No newline at end of file
diff --git a/build.gradle b/build.gradle
index eb1fe92..53b376d 100644
--- a/build.gradle
+++ b/build.gradle
@@ -3,15 +3,17 @@ apply from:'config.gradle'
buildscript {
repositories {
jcenter()
+ google()
}
dependencies {
- classpath 'com.android.tools.build:gradle:2.3.3'
+ classpath 'com.android.tools.build:gradle:3.1.3'
}
}
allprojects {
repositories {
jcenter()
+ google()
}
}
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index aedaf6f..a8ee906 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
-#Sun Jul 09 18:58:12 CST 2017
+#Wed Jul 11 17:52:15 COT 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
diff --git a/libk-switchbutton/build.gradle b/libk-switchbutton/build.gradle
index 043dd79..dc203d8 100644
--- a/libk-switchbutton/build.gradle
+++ b/libk-switchbutton/build.gradle
@@ -1,7 +1,6 @@
apply plugin: 'com.android.library'
android {
compileSdkVersion rootProject.ext.compileSdkVersion
- buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
@@ -15,10 +14,22 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
+ buildToolsVersion '27.0.3'
}
dependencies {
- compile fileTree(dir: 'libs', include: ['*.jar'])
- testCompile 'junit:junit:4.12'
- compile "com.android.support:appcompat-v7:$rootProject.ext.supportVersion"
+ implementation fileTree(dir: 'libs', include: ['*.jar'])
+
+ implementation "com.android.support:appcompat-v7:25.3.1"
+ // Required for instrumented tests
+ testImplementation 'junit:junit:4.12'
+ testImplementation 'org.mockito:mockito-core:2.4.0'
+ testImplementation 'org.powermock:powermock-core:1.7.0RC2'
+ testImplementation 'org.powermock:powermock-module-junit4:1.7.0RC2'
+ testImplementation 'org.powermock:powermock-api-mockito2:1.7.0RC2'
+ testImplementation "org.robolectric:robolectric:3.8"
+
+ androidTestImplementation 'com.android.support:support-annotations:27.1.1'
+ androidTestImplementation 'org.testng:testng:6.9.6'
+ androidTestImplementation 'com.android.support.test:runner:1.0.2'
}
diff --git a/libk-switchbutton/src/androidTest/java/lib/kingja/switchbutton/ApplicationTest.java b/libk-switchbutton/src/androidTest/java/lib/kingja/switchbutton/ApplicationTest.java
deleted file mode 100644
index e0dfa0b..0000000
--- a/libk-switchbutton/src/androidTest/java/lib/kingja/switchbutton/ApplicationTest.java
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Copyright (C) 2016 KingJA
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-package lib.kingja.switchbutton;
-
-import android.app.Application;
-import android.test.ApplicationTestCase;
-
-/**
- * Testing Fundamentals
- */
-public class ApplicationTest extends ApplicationTestCase {
- public ApplicationTest() {
- super(Application.class);
- }
-}
\ No newline at end of file
diff --git a/libk-switchbutton/src/main/java/lib/kingja/switchbutton/SwitchMultiButton.java b/libk-switchbutton/src/main/java/lib/kingja/switchbutton/SwitchMultiButton.java
index d939c37..021ad66 100644
--- a/libk-switchbutton/src/main/java/lib/kingja/switchbutton/SwitchMultiButton.java
+++ b/libk-switchbutton/src/main/java/lib/kingja/switchbutton/SwitchMultiButton.java
@@ -33,6 +33,10 @@
import android.view.MotionEvent;
import android.view.View;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
/**
* Description A smart switchable button,support multiple tabs.
* Create Time 2016/7/27 14:57
@@ -42,15 +46,16 @@
public class SwitchMultiButton extends View {
private static final String TAG = "SwitchMultiButton";
- /*default value*/
- private String[] mTabTexts = {"L", "R"};
- private int mTabNum = mTabTexts.length;
private static final float STROKE_RADIUS = 0;
private static final float STROKE_WIDTH = 2;
private static final float TEXT_SIZE = 14;
private static final int SELECTED_COLOR = 0xffeb7b00;
private static final int SELECTED_TAB = 0;
+ private static final boolean ALLOW_MULTI_TAB = false;
private static final String FONTS_DIR = "fonts/";
+ /*default value*/
+ private String[] mTabTexts = {"L", "R"};
+ private int mTabNum = mTabTexts.length;
/*other*/
private Paint mStrokePaint;
private Paint mFillPaint;
@@ -63,13 +68,13 @@ public class SwitchMultiButton extends View {
private float mStrokeWidth;
private int mSelectedColor;
private float mTextSize;
- private int mSelectedTab;
+ private Set mSelectedTabs;
private String mTypeface;
private float perWidth;
private float mTextHeightOffset;
private Paint.FontMetrics mFontMetrics;
private Typeface typeface;
-
+ private boolean allowMultiTab;
public SwitchMultiButton(Context context) {
this(context, null);
@@ -92,18 +97,24 @@ public SwitchMultiButton(Context context, AttributeSet attrs, int defStyleAttr)
* @param attrs
*/
private void initAttrs(Context context, AttributeSet attrs) {
+ mSelectedTabs = new HashSet<>();
+
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SwitchMultiButton);
mStrokeRadius = typedArray.getDimension(R.styleable.SwitchMultiButton_strokeRadius, STROKE_RADIUS);
mStrokeWidth = typedArray.getDimension(R.styleable.SwitchMultiButton_strokeWidth, STROKE_WIDTH);
mTextSize = typedArray.getDimension(R.styleable.SwitchMultiButton_textSize, TEXT_SIZE);
mSelectedColor = typedArray.getColor(R.styleable.SwitchMultiButton_selectedColor, SELECTED_COLOR);
- mSelectedTab = typedArray.getInteger(R.styleable.SwitchMultiButton_selectedTab, SELECTED_TAB);
+ mSelectedTabs.add(typedArray.getInteger(R.styleable.SwitchMultiButton_selectedTab, SELECTED_TAB));
+ allowMultiTab = typedArray.getBoolean(R.styleable.SwitchMultiButton_allowMultiTab, ALLOW_MULTI_TAB);
mTypeface = typedArray.getString(R.styleable.SwitchMultiButton_typeface);
+
int mSwitchTabsResId = typedArray.getResourceId(R.styleable.SwitchMultiButton_switchTabs, 0);
+
if (mSwitchTabsResId != 0) {
- mTabTexts = getResources().getStringArray(mSwitchTabsResId);
+ mTabTexts = context.getResources().getStringArray(mSwitchTabsResId);
mTabNum = mTabTexts.length;
}
+
if (!TextUtils.isEmpty(mTypeface)) {
typeface = Typeface.createFromAsset(context.getAssets(), FONTS_DIR + mTypeface);
}
@@ -220,7 +231,8 @@ protected void onDraw(Canvas canvas) {
for (int i = 0; i < mTabNum; i++) {
String tabText = mTabTexts[i];
float tabTextWidth = mSelectedTextPaint.measureText(tabText);
- if (i == mSelectedTab) {
+
+ if (mSelectedTabs.contains(i)) {
//draw selected tab
if (i == 0) {
drawLeftPath(canvas, left, top, bottom);
@@ -229,16 +241,16 @@ protected void onDraw(Canvas canvas) {
drawRightPath(canvas, top, right, bottom);
} else {
- canvas.drawRect(new RectF(perWidth * i, top, perWidth * (i + 1), bottom), mFillPaint);
+ canvas.drawRect(new RectF(perWidth * i, top, perWidth * (i + 1), bottom),
+ mFillPaint);
}
// draw selected text
- canvas.drawText(tabText, 0.5f * perWidth * (2 * i + 1) - 0.5f * tabTextWidth, mHeight * 0.5f +
- mTextHeightOffset, mSelectedTextPaint);
-
+ canvas.drawText(tabText, 0.5f * perWidth * (2 * i + 1) - 0.5f * tabTextWidth,
+ mHeight * 0.5f + mTextHeightOffset, mSelectedTextPaint);
} else {
//draw unselected text
- canvas.drawText(tabText, 0.5f * perWidth * (2 * i + 1) - 0.5f * tabTextWidth, mHeight * 0.5f +
- mTextHeightOffset, mUnselectedTextPaint);
+ canvas.drawText(tabText, 0.5f * perWidth * (2 * i + 1) - 0.5f * tabTextWidth,
+ mHeight * 0.5f + mTextHeightOffset, mUnselectedTextPaint);
}
}
}
@@ -257,9 +269,11 @@ private void drawLeftPath(Canvas canvas, float left, float top, float bottom) {
leftPath.lineTo(perWidth, top);
leftPath.lineTo(perWidth, bottom);
leftPath.lineTo(left + mStrokeRadius, bottom);
- leftPath.arcTo(new RectF(left, bottom - 2 * mStrokeRadius, left + 2 * mStrokeRadius, bottom), 90, 90);
+ leftPath.arcTo(new RectF(left, bottom - 2 * mStrokeRadius, left + 2 * mStrokeRadius, bottom),
+ 90, 90);
leftPath.lineTo(left, top + mStrokeRadius);
- leftPath.arcTo(new RectF(left, top, left + 2 * mStrokeRadius, top + 2 * mStrokeRadius), 180, 90);
+ leftPath.arcTo(new RectF(left, top, left + 2 * mStrokeRadius, top + 2 * mStrokeRadius),
+ 180, 90);
canvas.drawPath(leftPath, mFillPaint);
}
@@ -277,9 +291,11 @@ private void drawRightPath(Canvas canvas, float top, float right, float bottom)
rightPath.lineTo(right - perWidth, top);
rightPath.lineTo(right - perWidth, bottom);
rightPath.lineTo(right - mStrokeRadius, bottom);
- rightPath.arcTo(new RectF(right - 2 * mStrokeRadius, bottom - 2 * mStrokeRadius, right, bottom), 90, -90);
+ rightPath.arcTo(new RectF(right - 2 * mStrokeRadius, bottom - 2 * mStrokeRadius, right, bottom),
+ 90, -90);
rightPath.lineTo(right, top + mStrokeRadius);
- rightPath.arcTo(new RectF(right - 2 * mStrokeRadius, top, right, top + 2 * mStrokeRadius), 0, -90);
+ rightPath.arcTo(new RectF(right - 2 * mStrokeRadius, top, right, top + 2 * mStrokeRadius),
+ 0, -90);
canvas.drawPath(rightPath, mFillPaint);
}
@@ -322,13 +338,12 @@ public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
for (int i = 0; i < mTabNum; i++) {
if (x > perWidth * i && x < perWidth * (i + 1)) {
- if (mSelectedTab == i) {
- return true;
- }
- mSelectedTab = i;
- if (onSwitchListener != null) {
- onSwitchListener.onSwitch(i, mTabTexts[i]);
+ if (mSelectedTabs.contains(i)) {
+ removeTab(i);
+ } else {
+ addTab(i);
}
+ //mSelectedTabs.clear();
}
}
invalidate();
@@ -336,49 +351,78 @@ public boolean onTouchEvent(MotionEvent event) {
return true;
}
- /*=========================================Interface=========================================*/
+ private void removeTab(int i) {
+ mSelectedTabs.remove(i);
- /**
- * called when swtiched
- */
- public interface OnSwitchListener {
- void onSwitch(int position, String tabText);
+ if (onSwitchListener != null) {
+ onSwitchListener.onSwitch(i, mTabTexts[i]);
+ }
}
+ /*=========================================Interface=========================================*/
+
public SwitchMultiButton setOnSwitchListener(@NonNull OnSwitchListener onSwitchListener) {
this.onSwitchListener = onSwitchListener;
return this;
}
- /*=========================================Set and Get=========================================*/
-
/**
* get position of selected tab
*/
- public int getSelectedTab() {
- return mSelectedTab;
+ public Integer[] getSelectedTabs() {
+ return mSelectedTabs.toArray(new Integer[mSelectedTabs.size()]);
}
+ /*=========================================Set and Get=========================================*/
+
/**
- * set selected tab
+ * Set a tab to selected
*
- * @param mSelectedTab
- * @return
+ * @param mSelectedTab integer with the position of the tab
*/
- public SwitchMultiButton setSelectedTab(int mSelectedTab) {
- this.mSelectedTab = mSelectedTab;
+ public void setSelectedTab(int mSelectedTab) {
+ addTab(mSelectedTab);
invalidate();
+ }
+
+ private void addTab(int tab) {
+ if (!allowMultiTab)
+ mSelectedTabs.clear();
+
+ this.mSelectedTabs.add(tab);
+
if (onSwitchListener != null) {
- onSwitchListener.onSwitch(mSelectedTab, mTabTexts[mSelectedTab]);
+ onSwitchListener.onSwitch(tab, mTabTexts[tab]);
}
- return this;
}
- public void clearSelection() {
- this.mSelectedTab = -1;
+ /**
+ * Unselect a tab
+ *
+ * @param mSelectedTab integer with the position of the tab
+ */
+ public void unselectTab(int mSelectedTab) {
+ removeTab(mSelectedTab);
invalidate();
}
+ public void clearSelected() {
+ HashSet cp = new HashSet(mSelectedTabs);
+
+ this.mSelectedTabs.clear();
+ invalidate();
+
+ Iterator it = cp.iterator();
+
+ while (it.hasNext()) {
+ if (onSwitchListener != null) {
+ Integer i = it.next();
+ removeTab(i);
+ it.remove();
+ }
+ }
+ }
+
/**
* set data for the switchbutton
*
@@ -395,7 +439,16 @@ public SwitchMultiButton setText(String... tagTexts) {
throw new IllegalArgumentException("the size of tagTexts should greater then 1");
}
}
- /*======================================save and restore======================================*/
+
+ /**
+ * Return true if the button is checked, or false otherwise
+ *
+ * @param tabPosition a int value
+ * @return state a boolean value with the button state
+ */
+ public boolean getState(int tabPosition) {
+ return mSelectedTabs.contains(tabPosition);
+ }
@Override
protected Parcelable onSaveInstanceState() {
@@ -405,9 +458,11 @@ protected Parcelable onSaveInstanceState() {
bundle.putFloat("StrokeWidth", mStrokeWidth);
bundle.putFloat("TextSize", mTextSize);
bundle.putInt("SelectedColor", mSelectedColor);
- bundle.putInt("SelectedTab", mSelectedTab);
+ bundle.putSerializable("SelectedTab", (HashSet) mSelectedTabs);
+ bundle.putBoolean("AllowMultiTab", allowMultiTab);
return bundle;
}
+ /*======================================save and restore======================================*/
@Override
protected void onRestoreInstanceState(Parcelable state) {
@@ -417,10 +472,18 @@ protected void onRestoreInstanceState(Parcelable state) {
mStrokeWidth = bundle.getFloat("StrokeWidth");
mTextSize = bundle.getFloat("TextSize");
mSelectedColor = bundle.getInt("SelectedColor");
- mSelectedTab = bundle.getInt("SelectedTab");
+ mSelectedTabs = (HashSet) bundle.getSerializable("SelectedTab");
+ allowMultiTab = bundle.getBoolean("AllowMultiTab");
super.onRestoreInstanceState(bundle.getParcelable("View"));
} else {
super.onRestoreInstanceState(state);
}
}
+
+ /**
+ * called when a tab is switched
+ */
+ public interface OnSwitchListener {
+ void onSwitch(int position, String tabText);
+ }
}
diff --git a/libk-switchbutton/src/main/res/values/attr.xml b/libk-switchbutton/src/main/res/values/attr.xml
index 28f76ce..79a6466 100644
--- a/libk-switchbutton/src/main/res/values/attr.xml
+++ b/libk-switchbutton/src/main/res/values/attr.xml
@@ -8,5 +8,6 @@
+
\ No newline at end of file
diff --git a/libk-switchbutton/src/test/java/lib/kingja/switchbutton/SwitchMultiButtonTest.java b/libk-switchbutton/src/test/java/lib/kingja/switchbutton/SwitchMultiButtonTest.java
new file mode 100644
index 0000000..cf52efc
--- /dev/null
+++ b/libk-switchbutton/src/test/java/lib/kingja/switchbutton/SwitchMultiButtonTest.java
@@ -0,0 +1,17 @@
+package lib.kingja.switchbutton;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+
+/**
+ * Testing Fundamentals
+ */
+@RunWith(RobolectricTestRunner.class)
+public class SwitchMultiButtonTest {
+ @Test
+ public void getSelectedButtons() {
+
+ }
+}
\ No newline at end of file