diff --git a/README.md b/README.md index 68e6e00a761645feb21c73f7db8b620ab37e1b98..7a6e205436baef2990e0f935b3feeed27b4f0304 100644 --- a/README.md +++ b/README.md @@ -10,22 +10,24 @@ ####Params -|Key | Type | Default| Description | -| --- | ---- | ------ | ----------- | -|pickerConfirmBtnText |string |确认 | | -|pickerCancelBtnText |string |取消 | | -|pickerTitleText |string |请选择 | | -|pickerConfirmBtnColor |array |[1, 186, 245, 1] | | -|pickerCancelBtnColor |array |[1, 186, 245, 1] | | -|pickerTitleColor |array |[20, 20, 20, 1]) | | -|pickerToolBarBg |array |[232, 232, 232, 1] | | -|pickerBg |array |[196, 199, 206, 1] | | -|wheelFlex |array |[2, 1, 1] |set width of the three wheels to 2:1:1 | -|pickerData |array | | | -|selectedValue |array | | | -|onPickerConfirm |function| | | -|onPickerCancel |function| | | -|onPickerSelect |function| | | +|Key | Description | Type | Default| +| --- | ----------- | ---- | ------ | +|pickerConfirmBtnText | |string |confirm | +|pickerCancelBtnText | |string |cancel | +|pickerTitleText | |string |pls select | +|pickerConfirmBtnColor | |array |[1, 186, 245, 1] | +|pickerCancelBtnColor | |array |[1, 186, 245, 1] | +|pickerTitleColor | |array |[20, 20, 20, 1]) | +|pickerToolBarBg | |array |[232, 232, 232, 1] | +|pickerBg | |array |[196, 199, 206, 1] | +|pickerToolBarFontSize | |number |16 | +|pickerFontSize | |number |16 | +|pickerFontColor | |array |[31, 31, 31, 1] | +|pickerData | |array | | +|selectedValue | |string | | +|onPickerConfirm | |function| | +|onPickerCancel | |function| | +|onPickerSelect | |function| | ####Methods @@ -93,7 +95,7 @@ ```javascript pickerData = [1,2,3,4]; - selectedValue = [3]; + selectedValue = 3; ``` - two or more wheel: diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index 956edd051b1f20d1e3ed65fe957d1e2b54d9dbf5..005069de69bec4c504a75c0df69df0b35ae91eae 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -1,3 +1,3 @@ - \ No newline at end of file + diff --git a/android/src/main/java/com/beefe/picker/PickerViewModule.java b/android/src/main/java/com/beefe/picker/PickerViewModule.java index cb8cd16b47b57729fb823edea9aedd884683c806..31843885e2ce2e9e5a69285d8cb1b3d5c9722808 100644 --- a/android/src/main/java/com/beefe/picker/PickerViewModule.java +++ b/android/src/main/java/com/beefe/picker/PickerViewModule.java @@ -1,13 +1,12 @@ package com.beefe.picker; import android.app.Activity; -import android.app.Application; import android.app.Dialog; import android.graphics.Color; import android.graphics.PixelFormat; -import android.os.Bundle; import android.support.annotation.Nullable; import android.text.TextUtils; +import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.Window; @@ -18,8 +17,10 @@ import android.widget.TextView; import com.beefe.picker.view.OnSelectedListener; import com.beefe.picker.view.PickerViewAlone; import com.beefe.picker.view.PickerViewLinkage; +import com.beefe.picker.view.ReturnData; import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Callback; +import com.facebook.react.bridge.LifecycleEventListener; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; @@ -32,8 +33,10 @@ import com.facebook.react.modules.core.DeviceEventManagerModule; import java.util.ArrayList; +import static android.graphics.Color.argb; + /** - * Author: heng + * Author: heng *

* Created by heng on 16/9/5. *

@@ -53,9 +56,20 @@ import java.util.ArrayList; * Edited by heng on 2016/11/17 * 1. Used Dialog replace WindowManger * 2. Restore method show() isPickerShow() + *

+ * Edited by heng on 2016/12/23 + * 1. Changed returnData type + * 2. Added pickerToolBarFontSize + * + * Edited by heng on 2016/12/26 + * 1. Fixed returnData bug + * 2. Added pickerFontColor + * 3. Added pickerFontSize + * 4. Used LifecycleEventListener replace Application.ActivityLifecycleCallbacks + * 5. Fixed other bug */ -public class PickerViewModule extends ReactContextBaseJavaModule implements Application.ActivityLifecycleCallbacks { +public class PickerViewModule extends ReactContextBaseJavaModule implements LifecycleEventListener { private static final String REACT_CLASS = "BEEPickerManager"; @@ -68,17 +82,21 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Appl private static final String PICKER_BG_COLOR = "pickerBg"; - private static final String TEXT_BAR_COLOR = "pickerToolBarBg"; - private static final String TEXT_BAR_HEIGHT = "pickerToolBarHeight"; + private static final String PICKER_TOOL_BAR_BG = "pickerToolBarBg"; + private static final String PICKER_TOOL_BAR_HEIGHT = "pickerToolBarHeight"; + private static final String PICKER_TOOL_BAR_TEXT_SIZE = "pickerToolBarFontSize"; - private static final String CONFIRM_TEXT = "pickerConfirmBtnText"; - private static final String CONFIRM_TEXT_COLOR = "pickerConfirmBtnColor"; + private static final String PICKER_CONFIRM_BTN_TEXT = "pickerConfirmBtnText"; + private static final String PICKER_CONFIRM_BTN_COLOR = "pickerConfirmBtnColor"; - private static final String CANCEL_TEXT = "pickerCancelBtnText"; - private static final String CANCEL_TEXT_COLOR = "pickerCancelBtnColor"; + private static final String PICKER_CANCEL_BTN_TEXT = "pickerCancelBtnText"; + private static final String PICKER_CANCEL_BTN_COLOR = "pickerCancelBtnColor"; - private static final String TITLE_TEXT = "pickerTitleText"; - private static final String TITLE_TEXT_COLOR = "pickerTitleColor"; + private static final String PICKER_TITLE_TEXT = "pickerTitleText"; + private static final String PICKER_TITLE_TEXT_COLOR = "pickerTitleColor"; + + private static final String PICKER_TEXT_COLOR = "pickerFontColor"; + private static final String PICKER_TEXT_SIZE = "pickerFontSize"; private static final String PICKER_EVENT_NAME = "pickerEvent"; private static final String EVENT_KEY_CONFIRM = "confirm"; @@ -97,12 +115,13 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Appl private double[] weights; - private ArrayList returnData; + private ArrayList returnData; private int curStatus; public PickerViewModule(ReactApplicationContext reactContext) { super(reactContext); + reactContext.addLifecycleEventListener(this); } @Override @@ -124,11 +143,11 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Appl final PickerViewAlone pickerViewAlone = (PickerViewAlone) view.findViewById(R.id.pickerViewAlone); int barViewHeight; - if (options.hasKey(TEXT_BAR_HEIGHT)) { + if (options.hasKey(PICKER_TOOL_BAR_HEIGHT)) { try { - barViewHeight = options.getInt(TEXT_BAR_HEIGHT); + barViewHeight = options.getInt(PICKER_TOOL_BAR_HEIGHT); } catch (Exception e) { - barViewHeight = (int) options.getDouble(TEXT_BAR_HEIGHT); + barViewHeight = (int) options.getDouble(PICKER_TOOL_BAR_HEIGHT); } } else { barViewHeight = (int) (activity.getResources().getDisplayMetrics().density * 40); @@ -138,22 +157,28 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Appl barViewHeight); barLayout.setLayoutParams(params); - if (options.hasKey(TEXT_BAR_COLOR)) { - ReadableArray array = options.getArray(TEXT_BAR_COLOR); + if (options.hasKey(PICKER_TOOL_BAR_BG)) { + ReadableArray array = options.getArray(PICKER_TOOL_BAR_BG); int[] colors = getColor(array); - barLayout.setBackgroundColor(Color.argb(colors[3], colors[0], colors[1], colors[2])); + barLayout.setBackgroundColor(argb(colors[3], colors[0], colors[1], colors[2])); } + if (options.hasKey(PICKER_TOOL_BAR_TEXT_SIZE)) { + int toolBarTextSize = options.getInt(PICKER_TOOL_BAR_TEXT_SIZE); + cancelTV.setTextSize(toolBarTextSize); + titleTV.setTextSize(toolBarTextSize); + confirmTV.setTextSize(toolBarTextSize); + } - if (options.hasKey(CONFIRM_TEXT)) { - confirmText = options.getString(CONFIRM_TEXT); + if (options.hasKey(PICKER_CONFIRM_BTN_TEXT)) { + confirmText = options.getString(PICKER_CONFIRM_BTN_TEXT); } confirmTV.setText(!TextUtils.isEmpty(confirmText) ? confirmText : ""); - if (options.hasKey(CONFIRM_TEXT_COLOR)) { - ReadableArray array = options.getArray(CONFIRM_TEXT_COLOR); + if (options.hasKey(PICKER_CONFIRM_BTN_COLOR)) { + ReadableArray array = options.getArray(PICKER_CONFIRM_BTN_COLOR); int[] colors = getColor(array); - confirmTV.setTextColor(Color.argb(colors[3], colors[0], colors[1], colors[2])); + confirmTV.setTextColor(argb(colors[3], colors[0], colors[1], colors[2])); } confirmTV.setOnClickListener(new View.OnClickListener() { @Override @@ -172,24 +197,24 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Appl }); - if (options.hasKey(TITLE_TEXT)) { - titleText = options.getString(TITLE_TEXT); + if (options.hasKey(PICKER_TITLE_TEXT)) { + titleText = options.getString(PICKER_TITLE_TEXT); } titleTV.setText(!TextUtils.isEmpty(titleText) ? titleText : ""); - if (options.hasKey(TITLE_TEXT_COLOR)) { - ReadableArray array = options.getArray(TITLE_TEXT_COLOR); + if (options.hasKey(PICKER_TITLE_TEXT_COLOR)) { + ReadableArray array = options.getArray(PICKER_TITLE_TEXT_COLOR); int[] colors = getColor(array); - titleTV.setTextColor(Color.argb(colors[3], colors[0], colors[1], colors[2])); + titleTV.setTextColor(argb(colors[3], colors[0], colors[1], colors[2])); } - if (options.hasKey(CANCEL_TEXT)) { - cancelText = options.getString(CANCEL_TEXT); + if (options.hasKey(PICKER_CANCEL_BTN_TEXT)) { + cancelText = options.getString(PICKER_CANCEL_BTN_TEXT); } cancelTV.setText(!TextUtils.isEmpty(cancelText) ? cancelText : ""); - if (options.hasKey(CANCEL_TEXT_COLOR)) { - ReadableArray array = options.getArray(CANCEL_TEXT_COLOR); + if (options.hasKey(PICKER_CANCEL_BTN_COLOR)) { + ReadableArray array = options.getArray(PICKER_CANCEL_BTN_COLOR); int[] colors = getColor(array); - cancelTV.setTextColor(Color.argb(colors[3], colors[0], colors[1], colors[2])); + cancelTV.setTextColor(argb(colors[3], colors[0], colors[1], colors[2])); } cancelTV.setOnClickListener(new View.OnClickListener() { @Override @@ -262,6 +287,22 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Appl } } + int pickerTextColor = 0xff000000; + if (options.hasKey(PICKER_TEXT_COLOR)) { + ReadableArray array = options.getArray(PICKER_TEXT_COLOR); + int[] colors = getColor(array); + pickerTextColor = Color.argb(colors[3], colors[0], colors[1], colors[2]); + } + + int pickerTextSize = 16; + if (options.hasKey(PICKER_TEXT_SIZE)) { + try { + pickerTextSize = options.getInt(PICKER_TEXT_SIZE); + } catch (Exception e) { + pickerTextSize = (int) options.getDouble(PICKER_TEXT_SIZE); + } + } + ReadableArray pickerData = options.getArray(PICKER_DATA); int pickerViewHeight; @@ -273,11 +314,13 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Appl pickerViewAlone.setVisibility(View.GONE); pickerViewLinkage.setPickerData(pickerData, weights); + pickerViewLinkage.setTextColor(pickerTextColor); + pickerViewLinkage.setTextSize(pickerTextSize); pickerViewLinkage.setIsLoop(isLoop); pickerViewLinkage.setOnSelectListener(new OnSelectedListener() { @Override - public void onSelected(ArrayList selectedList) { + public void onSelected(ArrayList selectedList) { returnData = selectedList; commonEvent(EVENT_KEY_SELECTED); } @@ -291,11 +334,13 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Appl pickerViewLinkage.setVisibility(View.GONE); pickerViewAlone.setPickerData(pickerData, weights); + pickerViewAlone.setTextColor(pickerTextColor); + pickerViewAlone.setTextSize(pickerTextSize); pickerViewAlone.setIsLoop(isLoop); pickerViewAlone.setOnSelectedListener(new OnSelectedListener() { @Override - public void onSelected(ArrayList selectedList) { + public void onSelected(ArrayList selectedList) { returnData = selectedList; commonEvent(EVENT_KEY_SELECTED); } @@ -309,9 +354,10 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Appl if (options.hasKey(PICKER_BG_COLOR)) { ReadableArray array = options.getArray(PICKER_BG_COLOR); int[] colors = getColor(array); - pickerLayout.setBackgroundColor(Color.argb(colors[3], colors[0], colors[1], colors[2])); + pickerLayout.setBackgroundColor(argb(colors[3], colors[0], colors[1], colors[2])); } + Log.d("PickerView", "pickerViewHeight = " + pickerViewHeight); int height = barViewHeight + pickerViewHeight; if (dialog == null) { dialog = new Dialog(activity, R.style.Dialog_Full_Screen); @@ -331,7 +377,6 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Appl dialog.dismiss(); dialog.setContentView(view); } - dialog.show(); } } @@ -357,6 +402,8 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Appl @ReactMethod public void isPickerShow(Callback callback) { + if (callback == null) + return; if (dialog == null) { callback.invoke(ERROR_NOT_INIT); } else { @@ -385,11 +432,15 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Appl private void commonEvent(String eventKey) { WritableMap map = Arguments.createMap(); - WritableArray array = Arguments.createArray(); - for (String item : returnData) { - array.pushString(item); + map.putString("type", eventKey); + WritableArray indexes = Arguments.createArray(); + WritableArray values = Arguments.createArray(); + for (ReturnData data : returnData) { + indexes.pushInt(data.getIndex()); + values.pushString(data.getItem()); } - map.putArray(eventKey, array); + map.putArray("selectedValue", values); + map.putArray("selectedIndex", indexes); sendEvent(getReactApplicationContext(), PICKER_EVENT_NAME, map); } @@ -402,36 +453,18 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Appl } @Override - public void onActivityCreated(Activity activity, Bundle savedInstanceState) { + public void onHostResume() { } @Override - public void onActivityStarted(Activity activity) { - - } - - @Override - public void onActivityResumed(Activity activity) { - - } - - @Override - public void onActivityPaused(Activity activity) { - - } - - @Override - public void onActivityStopped(Activity activity) { + public void onHostPause() { hide(); + dialog = null; } @Override - public void onActivitySaveInstanceState(Activity activity, Bundle outState) { + public void onHostDestroy() { } - - @Override - public void onActivityDestroyed(Activity activity) { - } -} +} \ No newline at end of file diff --git a/android/src/main/java/com/beefe/picker/view/LoopView.java b/android/src/main/java/com/beefe/picker/view/LoopView.java index 60c36a0cc20e62dbcc3c3a8c63428430982e8b55..6540f5a3333540d4fc0dee0cbe92452fb765569b 100644 --- a/android/src/main/java/com/beefe/picker/view/LoopView.java +++ b/android/src/main/java/com/beefe/picker/view/LoopView.java @@ -19,9 +19,13 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; /** - * Edited by shexiaoheng on 2016/10/20 + * Edited by heng on 2016/10/20 * 1. Added method getY * 2. Changed line color 0xffc5c5c5 -> 0xffb8bbc2 + * + * Edited by heng on 2016/12/26 + * 1. Added setTextColor + * 2. Added setTextSize */ public class LoopView extends View { @@ -50,10 +54,6 @@ public class LoopView extends View { private int textSize; int maxTextHeight; - private int colorGray; - private int colorBlack; - private int colorLightGray; - // 条目间距倍数 float lineSpacingMultiplier; boolean isLoop; @@ -109,37 +109,30 @@ public class LoopView extends View { lineSpacingMultiplier = 2.0F; isLoop = true; itemsVisible = 9; - textSize = 0; - colorGray = 0xffafafaf; - colorBlack = 0xff313131; - colorLightGray = 0xffb8bbc2; - // colorLightGray = 0xffc5c5c5; + textSize = (int) (context.getResources().getDisplayMetrics().density * 16); totalScrollY = 0; initPosition = -1; - initPaints(); - - setTextSize(20F); } private void initPaints() { paintOuterText = new Paint(); - paintOuterText.setColor(colorGray); + paintOuterText.setColor(0xffafafaf); paintOuterText.setAntiAlias(true); paintOuterText.setTypeface(Typeface.MONOSPACE); paintOuterText.setTextSize(textSize); paintCenterText = new Paint(); - paintCenterText.setColor(colorBlack); + paintCenterText.setColor(0xff000000); paintCenterText.setAntiAlias(true); paintCenterText.setTextScaleX(scaleX); paintCenterText.setTypeface(Typeface.MONOSPACE); paintCenterText.setTextSize(textSize); paintIndicator = new Paint(); - paintIndicator.setColor(colorLightGray); + paintIndicator.setColor(0xffb8bbc2); paintIndicator.setAntiAlias(true); if (android.os.Build.VERSION.SDK_INT >= 11) { @@ -204,15 +197,22 @@ public class LoopView extends View { } } + public void setTextColor(int color){ + paintCenterText.setColor(color); + invalidate(); + } + public final void setNotLoop() { isLoop = false; } public final void setTextSize(float size) { if (size > 0.0F) { - textSize = (int) (context.getResources().getDisplayMetrics().density * size); + this.textSize = (int) (context.getResources().getDisplayMetrics().density * size); paintOuterText.setTextSize(textSize); paintCenterText.setTextSize(textSize); + remeasure(); + invalidate(); } } @@ -242,6 +242,7 @@ public class LoopView extends View { this.initPosition = initPosition; } } + selectedIndex = initPosition; totalScrollY = 0; cancelFuture(); invalidate(); @@ -450,4 +451,4 @@ public class LoopView extends View { invalidate(); return true; } -} +} \ No newline at end of file diff --git a/android/src/main/java/com/beefe/picker/view/OnSelectedListener.java b/android/src/main/java/com/beefe/picker/view/OnSelectedListener.java index 7122b26f17e52449b3a94f4c17a7049f92b8fb01..72d1519a4ab8c8b5c27cdf2e52e4aabca16dc21a 100644 --- a/android/src/main/java/com/beefe/picker/view/OnSelectedListener.java +++ b/android/src/main/java/com/beefe/picker/view/OnSelectedListener.java @@ -8,6 +8,6 @@ import java.util.ArrayList; public interface OnSelectedListener { - void onSelected(ArrayList selectedList); + void onSelected(ArrayList selectedList); } diff --git a/android/src/main/java/com/beefe/picker/view/PickerViewAlone.java b/android/src/main/java/com/beefe/picker/view/PickerViewAlone.java index b28b627e4f556eb02115208f84a25e8a59356bb3..74ed494c38aa514dea17fd1f0a47d375619d2233 100644 --- a/android/src/main/java/com/beefe/picker/view/PickerViewAlone.java +++ b/android/src/main/java/com/beefe/picker/view/PickerViewAlone.java @@ -13,10 +13,14 @@ import java.util.ArrayList; import java.util.Arrays; /** - * Created by heng on 16/9/6. + * Created by heng on 16/9/6. *

* Edited by heng on 16/10/09: * 修复滚动后返回值错误的bug + * + * Edited by heng on 2016/12/26 + * 1. Fixed returnData bug + * 2. Added LoopView TextColor and TextSize support */ public class PickerViewAlone extends LinearLayout { @@ -25,7 +29,7 @@ public class PickerViewAlone extends LinearLayout { private OnSelectedListener onSelectedListener; - private ArrayList curSelectedList; + private ArrayList curSelectedList; public PickerViewAlone(Context context) { super(context); @@ -46,23 +50,23 @@ public class PickerViewAlone extends LinearLayout { this.onSelectedListener = listener; } - public void setPickerData(ReadableArray array, double[] weights) { + public void setPickerData(ReadableArray array, double[] weights) { curSelectedList = new ArrayList<>(); switch (array.getType(0).name()) { case "Array": - setMultipleData(array, curSelectedList, weights); + setMultipleData(array, weights); break; default: - setAloneData(array, curSelectedList); + setAloneData(array); break; } } - public ArrayList getSelectedData(){ + public ArrayList getSelectedData() { return this.curSelectedList; } - private void setAloneData(ReadableArray array, final ArrayList curSelectedList) { + private void setAloneData(ReadableArray array) { ArrayList values = arrayToList(array); final LoopView loopView = new LoopView(getContext()); LayoutParams params = new LayoutParams( @@ -71,16 +75,22 @@ public class PickerViewAlone extends LinearLayout { loopView.setLayoutParams(params); loopView.setItems(values); loopView.setSelectedPosition(0); + ReturnData returnData = new ReturnData(); + returnData.setItem(values.get(0)); + returnData.setIndex(loopView.getSelectedIndex()); if (curSelectedList.size() > 0) { - curSelectedList.set(0, values.get(0)); + curSelectedList.set(0, returnData); } else { - curSelectedList.add(0, values.get(0)); + curSelectedList.add(0, returnData); } loopView.setListener(new OnItemSelectedListener() { @Override public void onItemSelected(String item, int index) { if (onSelectedListener != null) { - curSelectedList.set(0, item); + ReturnData returnData1 = new ReturnData(); + returnData1.setItem(item); + returnData1.setIndex(index); + curSelectedList.set(0, returnData1); onSelectedListener.onSelected(curSelectedList); } } @@ -88,8 +98,9 @@ public class PickerViewAlone extends LinearLayout { pickerViewAloneLayout.addView(loopView); } - private void setMultipleData(ReadableArray array, final ArrayList curSelectedList, double[] weights) { + private void setMultipleData(ReadableArray array, double[] weights) { final String[] selectedItems = new String[array.size()]; + final int[] selectedIndexes = new int[array.size()]; for (int i = 0; i < array.size(); i++) { switch (array.getType(i).name()) { case "Array": @@ -110,10 +121,14 @@ public class PickerViewAlone extends LinearLayout { loopView.setItems(values); loopView.setTag(i); loopView.setSelectedPosition(0); + + ReturnData returnData = new ReturnData(); + returnData.setItem(values.get(0)); + returnData.setIndex(loopView.getSelectedIndex()); if (curSelectedList.size() > i) { - curSelectedList.set(i, values.get(0)); + curSelectedList.set(i, returnData); } else { - curSelectedList.add(i, values.get(0)); + curSelectedList.add(i, returnData); } selectedItems[i] = values.get(0); loopView.setListener(new OnItemSelectedListener() { @@ -125,12 +140,16 @@ public class PickerViewAlone extends LinearLayout { if (view instanceof LoopView) { LoopView loop = (LoopView) view; selectedItems[k] = loop.getSelectedItem(); + selectedIndexes[k] = loop.getSelectedIndex(); } } if (onSelectedListener != null) { for (int i = 0; i < selectedItems.length; i++) { - curSelectedList.set(i, selectedItems[i]); + ReturnData returnData1 = new ReturnData(); + returnData1.setItem(selectedItems[i]); + returnData1.setIndex(selectedIndexes[i]); + curSelectedList.set(i, returnData1); } onSelectedListener.onSelected(curSelectedList); } @@ -155,19 +174,44 @@ public class PickerViewAlone extends LinearLayout { } } - private void setSelect(int size, String[] values, ArrayList curSelectedList) { + private void setSelect(int size, String[] values, ArrayList curSelectedList) { for (int i = 0; i < size; i++) { View view = pickerViewAloneLayout.getChildAt(i); if (view instanceof LoopView) { LoopView loop = (LoopView) view; if (loop.hasItem(values[i])) { loop.setSelectedItem(values[i]); - curSelectedList.set(i, values[i]); + ReturnData returnData = new ReturnData(); + returnData.setItem(values[i]); + returnData.setIndex(loop.getSelectedIndex()); + curSelectedList.set(i, returnData); } } } } + public void setTextColor(int color){ + int viewCount = pickerViewAloneLayout.getChildCount(); + for (int i = 0; i < viewCount; i++) { + View view = pickerViewAloneLayout.getChildAt(i); + if (view instanceof LoopView) { + LoopView loopView = (LoopView) view; + loopView.setTextColor(color); + } + } + } + + public void setTextSize(float size){ + int viewCount = pickerViewAloneLayout.getChildCount(); + for (int i = 0; i < viewCount; i++) { + View view = pickerViewAloneLayout.getChildAt(i); + if (view instanceof LoopView) { + LoopView loopView = (LoopView) view; + loopView.setTextSize(size); + } + } + } + public void setIsLoop(boolean isLoop) { if (!isLoop) { int viewCount = pickerViewAloneLayout.getChildCount(); @@ -215,4 +259,4 @@ public class PickerViewAlone extends LinearLayout { return values; } -} +} \ No newline at end of file diff --git a/android/src/main/java/com/beefe/picker/view/PickerViewLinkage.java b/android/src/main/java/com/beefe/picker/view/PickerViewLinkage.java index b0390aaaa3885aa5201310f302736e048d1093ee..88a01aba5aecde264909c52606632d78f4ecf7a0 100644 --- a/android/src/main/java/com/beefe/picker/view/PickerViewLinkage.java +++ b/android/src/main/java/com/beefe/picker/view/PickerViewLinkage.java @@ -15,7 +15,11 @@ import java.util.ArrayList; import java.util.Arrays; /** - * Created by heng on 16/9/1. + * Created by heng on 2016/09/01 + * + * Edited by heng on 2016/12/26 + * 1. Fixed returnData bug + * 2. Added LoopView TextColor and TextSize support */ public class PickerViewLinkage extends LinearLayout { @@ -77,7 +81,11 @@ public class PickerViewLinkage extends LinearLayout { private int selectOneIndex; private int selectTwoIndex; - private ArrayList curSelectedList; + private ArrayList curSelectedList; + + private ReturnData returnData; + private ReturnData returnData1; + private ReturnData returnData2; private void checkItems(LoopView loopView, ArrayList list) { if (list != null && list.size() > 0) { @@ -137,6 +145,9 @@ public class PickerViewLinkage extends LinearLayout { */ public void setPickerData(ReadableArray array, double[] weights) { curSelectedList = new ArrayList<>(); + returnData = new ReturnData(); + returnData1 = new ReturnData(); + returnData2 = new ReturnData(); oneList.clear(); for (int i = 0; i < array.size(); i++) { ReadableMap map = array.getMap(i); @@ -148,10 +159,13 @@ public class PickerViewLinkage extends LinearLayout { } } checkItems(loopViewOne, oneList); + + returnData.setItem(oneList.get(0)); + returnData.setIndex(loopViewOne.getSelectedIndex()); if (curSelectedList.size() > 0) { - curSelectedList.set(0, oneList.get(0)); + curSelectedList.set(0, returnData); } else { - curSelectedList.add(0, oneList.get(0)); + curSelectedList.add(0, returnData); } ReadableArray childArray = data.get(0).getArray(oneList.get(0)); @@ -162,10 +176,12 @@ public class PickerViewLinkage extends LinearLayout { twoList.clear(); getTwoListData(); checkItems(loopViewTwo, twoList); + returnData1.setItem(twoList.get(0)); + returnData1.setIndex(loopViewTwo.getSelectedIndex()); if (curSelectedList.size() > 1) { - curSelectedList.set(1, twoList.get(0)); + curSelectedList.set(1, returnData1); } else { - curSelectedList.add(1, twoList.get(0)); + curSelectedList.add(1, returnData1); } ReadableMap childMap = data.get(0).getArray(oneList.get(0)).getMap(0); @@ -174,17 +190,22 @@ public class PickerViewLinkage extends LinearLayout { threeList.clear(); threeList = arrayToList(sunArray); checkItems(loopViewThree, threeList); + returnData2.setItem(threeList.get(0)); + returnData2.setIndex(loopViewThree.getSelectedIndex()); if (curSelectedList.size() > 2) { - curSelectedList.set(2, threeList.get(0)); + curSelectedList.set(2, returnData2); } else { - curSelectedList.add(2, threeList.get(0)); + curSelectedList.add(2, returnData2); } loopViewOne.setListener(new OnItemSelectedListener() { @Override public void onItemSelected(String item, int index) { selectOneIndex = index; - curSelectedList.set(0, item); + returnData = new ReturnData(); + returnData.setIndex(index); + returnData.setItem(item); + curSelectedList.set(0, returnData); twoList.clear(); ReadableArray arr = data.get(index).getArray(item); for (int i = 0; i < arr.size(); i++) { @@ -195,7 +216,10 @@ public class PickerViewLinkage extends LinearLayout { } } checkItems(loopViewTwo, twoList); - curSelectedList.set(1, twoList.get(0)); + returnData1 = new ReturnData(); + returnData1.setItem(twoList.get(0)); + returnData1.setIndex(loopViewTwo.getSelectedIndex()); + curSelectedList.set(1, returnData1); ReadableArray ar = data.get(index).getArray(item); @@ -205,7 +229,10 @@ public class PickerViewLinkage extends LinearLayout { threeList.clear(); threeList = arrayToList(sunArray); checkItems(loopViewThree, threeList); - curSelectedList.set(2, threeList.get(0)); + returnData2 = new ReturnData(); + returnData2.setItem(threeList.get(0)); + returnData2.setIndex(loopViewThree.getSelectedIndex()); + curSelectedList.set(2, returnData2); if (onSelectedListener != null) { onSelectedListener.onSelected(curSelectedList); @@ -226,9 +253,20 @@ public class PickerViewLinkage extends LinearLayout { threeList = arrayToList(sunArray); checkItems(loopViewThree, threeList); - curSelectedList.set(0, oneList.get(selectOneIndex)); - curSelectedList.set(1, item); - curSelectedList.set(2, threeList.get(0)); + returnData = new ReturnData(); + returnData.setItem(oneList.get(selectOneIndex)); + returnData.setIndex(loopViewOne.getSelectedIndex()); + curSelectedList.set(0, returnData); + + returnData1 = new ReturnData(); + returnData1.setItem(item); + returnData1.setIndex(index); + curSelectedList.set(1, returnData1); + + returnData2 = new ReturnData(); + returnData2.setItem(threeList.get(0)); + returnData2.setIndex(loopViewThree.getSelectedIndex()); + curSelectedList.set(2, returnData2); if (onSelectedListener != null) { onSelectedListener.onSelected(curSelectedList); } @@ -238,9 +276,20 @@ public class PickerViewLinkage extends LinearLayout { loopViewThree.setListener(new OnItemSelectedListener() { @Override public void onItemSelected(String item, int index) { - curSelectedList.set(0, oneList.get(selectOneIndex)); - curSelectedList.set(1, twoList.get(selectTwoIndex)); - curSelectedList.set(2, item); + returnData = new ReturnData(); + returnData.setItem(oneList.get(selectOneIndex)); + returnData.setIndex(loopViewOne.getSelectedIndex()); + curSelectedList.set(0, returnData); + + returnData1 = new ReturnData(); + returnData1.setItem(twoList.get(selectTwoIndex)); + returnData1.setIndex(loopViewTwo.getSelectedIndex()); + curSelectedList.set(1, returnData1); + + returnData2 = new ReturnData(); + returnData2.setItem(item); + returnData2.setIndex(index); + curSelectedList.set(2, returnData2); if (onSelectedListener != null) { onSelectedListener.onSelected(curSelectedList); } @@ -256,8 +305,14 @@ public class PickerViewLinkage extends LinearLayout { twoList.clear(); twoList = arrayToList(arr); checkItems(loopViewTwo, twoList); - curSelectedList.set(0, item); - curSelectedList.set(1, twoList.get(0)); + returnData = new ReturnData(); + returnData.setItem(item); + returnData.setIndex(index); + curSelectedList.set(0, returnData); + returnData1 = new ReturnData(); + returnData1.setItem(twoList.get(0)); + returnData1.setIndex(loopViewTwo.getSelectedIndex()); + curSelectedList.set(1, returnData1); if (onSelectedListener != null) { onSelectedListener.onSelected(curSelectedList); } @@ -267,16 +322,26 @@ public class PickerViewLinkage extends LinearLayout { twoList.clear(); twoList = arrayToList(childArray); checkItems(loopViewTwo, twoList); + returnData1 = new ReturnData(); + returnData1.setItem(twoList.get(0)); + returnData1.setIndex(loopViewTwo.getSelectedIndex()); if (curSelectedList.size() > 1) { - curSelectedList.set(1, twoList.get(0)); + curSelectedList.set(1, returnData1); } else { - curSelectedList.add(1, twoList.get(0)); + curSelectedList.add(1, returnData1); } loopViewTwo.setListener(new OnItemSelectedListener() { @Override public void onItemSelected(String item, int index) { - curSelectedList.set(0, oneList.get(selectOneIndex)); - curSelectedList.set(1, item); + returnData = new ReturnData(); + returnData.setItem(oneList.get(selectOneIndex)); + returnData.setIndex(loopViewOne.getSelectedIndex()); + curSelectedList.set(0, returnData); + + returnData1 = new ReturnData(); + returnData1.setIndex(index); + returnData1.setItem(item); + curSelectedList.set(1, returnData1); if (onSelectedListener != null) { onSelectedListener.onSelected(curSelectedList); } @@ -330,13 +395,19 @@ public class PickerViewLinkage extends LinearLayout { getTwoListData(); loopViewTwo.setItems(twoList); loopViewTwo.setSelectedPosition(0); - curSelectedList.set(1, loopViewTwo.getIndexItem(0)); + returnData1 = new ReturnData(); + returnData1.setItem(loopViewTwo.getIndexItem(0)); + returnData1.setIndex(loopViewTwo.getSelectedIndex()); + curSelectedList.set(1, returnData1); threeList.clear(); getThreeListData(); loopViewThree.setItems(threeList); loopViewThree.setSelectedPosition(0); - curSelectedList.set(2, loopViewThree.getIndexItem(0)); + returnData2 = new ReturnData(); + returnData2.setItem(loopViewThree.getIndexItem(0)); + returnData2.setIndex(loopViewThree.getSelectedIndex()); + curSelectedList.set(2, returnData2); break; case 2: @@ -344,7 +415,10 @@ public class PickerViewLinkage extends LinearLayout { getAllTwoListData(); loopViewTwo.setItems(twoList); loopViewTwo.setSelectedPosition(0); - curSelectedList.set(1, loopViewTwo.getIndexItem(0)); + returnData1 = new ReturnData(); + returnData1.setItem(loopViewTwo.getIndexItem(0)); + returnData1.setIndex(loopViewTwo.getSelectedIndex()); + curSelectedList.set(1, returnData1); break; } break; @@ -362,7 +436,10 @@ public class PickerViewLinkage extends LinearLayout { getThreeListData(); loopViewThree.setItems(threeList); loopViewThree.setSelectedPosition(0); - curSelectedList.set(2, loopViewThree.getIndexItem(0)); + returnData2 = new ReturnData(); + returnData2.setItem(loopViewThree.getIndexItem(0)); + returnData2.setIndex(loopViewThree.getSelectedIndex()); + curSelectedList.set(2, returnData2); break; } break; @@ -372,7 +449,7 @@ public class PickerViewLinkage extends LinearLayout { } } - private void selectValues(String[] values, final ArrayList curSelectedList) { + private void selectValues(String[] values, final ArrayList curSelectedList) { switch (values.length) { case 3: selectOneLoop(values, curSelectedList); @@ -400,47 +477,53 @@ public class PickerViewLinkage extends LinearLayout { /** * 设置第一个滚轮选中的值 */ - private void selectOneLoop(String[] values, final ArrayList curSelectedList) { + private void selectOneLoop(String[] values, final ArrayList curSelectedList) { if (loopViewOne.hasItem(values[0])) { selectOneIndex = loopViewOne.getItemPosition(values[0]); - loopViewOne.setSelectedPosition(selectOneIndex); - curSelectedList.set(0, loopViewOne.getIndexItem(selectOneIndex)); } else { selectOneIndex = 0; - loopViewOne.setSelectedPosition(0); - curSelectedList.set(0, loopViewOne.getIndexItem(0)); } + loopViewOne.setSelectedPosition(selectOneIndex); + + returnData = new ReturnData(); + returnData.setItem(loopViewOne.getIndexItem(selectOneIndex)); + returnData.setIndex(loopViewOne.getSelectedIndex()); + curSelectedList.set(0, returnData); } /** * 设置第二个滚轮选中的值 */ - private void selectTwoLoop(String[] values, final ArrayList curSelectedList) { + private void selectTwoLoop(String[] values, final ArrayList curSelectedList) { loopViewTwo.setItems(twoList); if (loopViewTwo.hasItem(values[1])) { selectTwoIndex = loopViewTwo.getItemPosition(values[1]); - loopViewTwo.setSelectedPosition(selectTwoIndex); - curSelectedList.set(1, loopViewTwo.getIndexItem(selectTwoIndex)); } else { selectTwoIndex = 0; - loopViewTwo.setSelectedPosition(0); - curSelectedList.set(1, loopViewTwo.getIndexItem(0)); } + returnData1 = new ReturnData(); + loopViewTwo.setSelectedPosition(selectTwoIndex); + returnData1.setItem(loopViewTwo.getIndexItem(selectTwoIndex)); + returnData1.setIndex(loopViewTwo.getSelectedIndex()); + curSelectedList.set(1, returnData1); } /** * 设置第三个滚轮选中的值 */ - private void selectThreeLoop(String[] values, final ArrayList curSelectedList) { + private void selectThreeLoop(String[] values, final ArrayList curSelectedList) { loopViewThree.setItems(threeList); + int selectThreeIndex; if (loopViewThree.hasItem(values[2])) { - int selectThreeIndex = loopViewThree.getItemPosition(values[2]); - loopViewThree.setSelectedPosition(selectThreeIndex); - curSelectedList.set(2, loopViewThree.getIndexItem(selectThreeIndex)); + selectThreeIndex = loopViewThree.getItemPosition(values[2]); } else { - loopViewThree.setSelectedPosition(0); - curSelectedList.set(2, loopViewThree.getIndexItem(0)); + selectThreeIndex = 0; } + returnData2 = new ReturnData(); + loopViewThree.setSelectedPosition(selectThreeIndex); + returnData2.setItem(loopViewThree.getIndexItem(selectThreeIndex)); + returnData2.setIndex(loopViewThree.getSelectedIndex()); + curSelectedList.set(2, returnData2); } /** @@ -477,6 +560,33 @@ public class PickerViewLinkage extends LinearLayout { threeList = arrayToList(sunArray); } + public void setTextSize(float size){ + switch (curRow) { + case 2: + loopViewOne.setTextSize(size); + loopViewTwo.setTextSize(size); + break; + case 3: + loopViewOne.setTextSize(size); + loopViewTwo.setTextSize(size); + loopViewThree.setTextSize(size); + break; + } + } + + public void setTextColor(int color){ + switch (curRow) { + case 2: + loopViewOne.setTextColor(color); + loopViewTwo.setTextColor(color); + break; + case 3: + loopViewOne.setTextColor(color); + loopViewTwo.setTextColor(color); + loopViewThree.setTextColor(color); + break; + } + } public void setIsLoop(boolean isLoop) { if (!isLoop) { @@ -491,7 +601,6 @@ public class PickerViewLinkage extends LinearLayout { loopViewThree.setNotLoop(); break; } - } } @@ -499,12 +608,11 @@ public class PickerViewLinkage extends LinearLayout { return loopViewOne.getViewHeight(); } - public ArrayList getSelectedData(){ + public ArrayList getSelectedData() { return this.curSelectedList; } public void setOnSelectListener(OnSelectedListener listener) { this.onSelectedListener = listener; } - -} +} \ No newline at end of file diff --git a/android/src/main/java/com/beefe/picker/view/ReturnData.java b/android/src/main/java/com/beefe/picker/view/ReturnData.java new file mode 100644 index 0000000000000000000000000000000000000000..9f8d60142399d291b15a4195f3bed9b9b29f77c2 --- /dev/null +++ b/android/src/main/java/com/beefe/picker/view/ReturnData.java @@ -0,0 +1,27 @@ +package com.beefe.picker.view; + +/** + * Created by shexiaoheng on 2016/12/23. + */ + +public class ReturnData { + + private String item; + private int index; + + public String getItem() { + return item; + } + + public void setItem(String item) { + this.item = item; + } + + public int getIndex() { + return index; + } + + public void setIndex(int index) { + this.index = index; + } +} diff --git a/android/src/main/res/values/styles.xml b/android/src/main/res/values/styles.xml index 1e2b2e419ec00d5bfef658301faf8e23ba041fd9..65df192b2369ce03023b239204910626c6835931 100644 --- a/android/src/main/res/values/styles.xml +++ b/android/src/main/res/values/styles.xml @@ -1,6 +1,6 @@ -