Skip to content

Commit

Permalink
优化3d数据选择控件,适配华为手机
Browse files Browse the repository at this point in the history
  • Loading branch information
yijiebuyi committed Apr 19, 2018
1 parent e142043 commit 1a7b715
Show file tree
Hide file tree
Showing 7 changed files with 1,188 additions and 856 deletions.
26 changes: 25 additions & 1 deletion app/src/main/java/com/wp/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void onBirthPicked(int year, int month, int day) {
@Override
public void onClick(View view) {
DataPicker.pickFutureDate(MainActivity.this, new Date(System.currentTimeMillis()),
DateWheelPicker.TYPE_ALL, View.VISIBLE, new DataPicker.OnDatePickListener() {
DateWheelPicker.TYPE_ALL, View.VISIBLE, 100, new DataPicker.OnDatePickListener() {
@Override
public void onDatePicked(int year, int month, int day, int hour, int minute, int second) {
Toast.makeText(MainActivity.this, year + "-" + (month + 1) + "-" + day + " " + hour + ":" + minute, Toast.LENGTH_SHORT).show();
Expand Down Expand Up @@ -89,6 +89,30 @@ private List<String> getTextList() {
data.add("阿朱");
data.add("王菇凉");

data.add("杨过2");
data.add("张无忌2");
data.add("郭靖2");
data.add("乔峰2");
data.add("令狐冲2");
data.add("赵敏2");
data.add("东方不败2");
data.add("小龙女2");
data.add("黄蓉2");
data.add("阿朱2");
data.add("王菇凉2");

data.add("杨过3");
data.add("张无忌3");
data.add("郭靖3");
data.add("乔峰3");
data.add("令狐冲3");
data.add("赵敏3");
data.add("东方不败3");
data.add("小龙女3");
data.add("黄蓉3");
data.add("阿朱3");
data.add("王菇凉3");

return data;
}
}
22 changes: 16 additions & 6 deletions library/src/main/java/com/wheelpicker/DataPicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.graphics.drawable.ColorDrawable;
import android.view.View;


import com.wheelpicker.core.AbstractWheelPicker;
import com.wheelpicker.core.OnWheelPickedListener;
import com.wheelpicker.widget.TextWheelPicker;
Expand Down Expand Up @@ -84,6 +85,7 @@ public void onDatePicked(int year, int month, int day, int hour, int minute, int
mDay = dd;
}
picker.setCurrentDate(mYear, mMonth, mDay);
picker.notifyDataSetChanged();

int padding = context.getResources().getDimensionPixelOffset(R.dimen.px20);
picker.setPadding(0, padding, 0, padding);
Expand Down Expand Up @@ -181,6 +183,7 @@ public void onDatePicked(int year, int month, int day, int hour, int minute, int

picker.setCurrentTime(mHour, mMinute, mSecond);
picker.setCurrentDate(mYear, mMonth, mDay);
picker.notifyDataSetChanged();

int padding = context.getResources().getDimensionPixelOffset(R.dimen.px20);
picker.setPadding(0, padding, 0, padding);
Expand All @@ -199,14 +202,15 @@ public void onClick(View v) {
}

/**
* 选择未来时间,默认当前时间到100年后
* 选择未来时间,默认100年后
*
* @param context
* @param currentDate
* @param year 往后推多少年
* @param pickListener
*/
public static void pickFutureDate(Context context, Date currentDate, int whichWheelPick,
int visibility, final OnDatePickListener pickListener) {
public static void pickFutureDate(Context context, Date currentDate, int whichWheelPick, int visibility,
int year, final OnDatePickListener pickListener) {
BottomSheet bottomSheet = new BottomSheet(context);
final DateWheelPicker picker = new DateWheelPicker(context);
picker.setWheelPickerVisibility(whichWheelPick, visibility);
Expand All @@ -232,10 +236,16 @@ public void onDatePicked(int year, int month, int day, int hour, int minute, int
mYear = year;
mMonth = month;
mDay = day;
mHour = hour;
mMinute = minute;
mSecond = second;
}
});

picker.setDateRange(dy, dy + 100);
if (year <= 0) {
year = 100;
}
picker.setDateRange(dy, dy + year);
//after set onDatePickerLister
if (currentDate != null) {
calendar.setTime(currentDate);
Expand All @@ -253,9 +263,9 @@ public void onDatePicked(int year, int month, int day, int hour, int minute, int
mMinute = mm;
mSecond = ss;
}

picker.setCurrentTime(mHour, mMinute, mSecond);
picker.setCurrentDate(mYear, mMonth, mDay);
picker.notifyDataSetChanged();

int padding = context.getResources().getDimensionPixelOffset(R.dimen.px20);
picker.setPadding(0, padding, 0, padding);
Expand Down Expand Up @@ -286,7 +296,7 @@ public static void pickFutureDate(Context context, Date currentDate, int days,
BottomSheet bottomSheet = new BottomSheet(context);
FutureTimePicker picker = new FutureTimePicker(context);

java.util.Calendar calendar = java.util.Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());

picker.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
Expand Down
99 changes: 75 additions & 24 deletions library/src/main/java/com/wheelpicker/DateWheelPicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.view.Gravity;
import android.widget.LinearLayout;


import com.wheelpicker.core.AbstractWheelPicker;
import com.wheelpicker.core.OnWheelPickedListener;
import com.wheelpicker.widget.TextWheelPicker;
Expand Down Expand Up @@ -40,7 +39,7 @@ public class DateWheelPicker extends LinearLayout implements OnWheelPickedListen
public final static int TYPE_ALL = TYPE_YEAR | TYPE_MONTH | TYPE_DAY |
TYPE_HOUR | TYPE_MINUTE | TYPE_SECOND;
//年月日
public final static int TYPE_YY_MM_DD = TYPE_HOUR | TYPE_SECOND | TYPE_MINUTE;
public final static int TYPE_YY_MM_DD = TYPE_YEAR | TYPE_MONTH | TYPE_DAY;
//时分秒
public final static int TYPE_HH_MM_SS = TYPE_HOUR | TYPE_SECOND | TYPE_MINUTE;

Expand Down Expand Up @@ -279,6 +278,7 @@ public void setCurrentDate(int year, int month, int day) {
mSelectedMonth = month;
mSelectedDay = day;

//更新月份和天数
if (mCurrYear == mSelectedYear) {
if (mMode == MODE_BIRTHDAY) {
updateMaxMonths(mCurrMonth);
Expand All @@ -298,15 +298,7 @@ public void setCurrentDate(int year, int month, int day) {
int monthIndex = Math.max(0, mMonths.indexOf((month + 1) + mMonthStr));
int dayIndex = Math.max(0, mDays.indexOf(day + mDayStr));

setDateItemIndex(yearIndex, monthIndex, dayIndex);

mMonthPickerAdapter.setData(mMonths);
mDayPickerAdapter.setData(mDays);

if (mOnDatePickListener != null) {
mOnDatePickListener.onDatePicked(mSelectedYear, mSelectedMonth, mSelectedDay,
mSelectedHour, mSelectedMinute, mSelectedSecond);
}
setDateItemIndexWithoutReLayout(yearIndex, monthIndex, dayIndex);
}

public void setCurrentTime(int hour, int minute, int second) {
Expand All @@ -320,10 +312,9 @@ public void setCurrentTime(int hour, int minute, int second) {

if (mMode == MODE_PENDING) {
updateMinHour(mCurrHour);
//updateMinMinute(mCurrMinute);
//updateMinSecond(mCurrSecond);
updateMinMinute(mCurrMinute);
//TODO
updateMaxMinute(mCurrMinute);
//updateMinSecond(mCurrSecond);
updateMaxSecond(mCurrSecond);
} else {
updateMaxHour(mCurrHour);
Expand All @@ -333,7 +324,12 @@ public void setCurrentTime(int hour, int minute, int second) {
int hourIndex = Math.max(0, mHours.indexOf(hour + mHourStr));
int minuteIndex = Math.max(0, mMinutes.indexOf(minute + mMinuteStr));
int secondIndex = Math.max(0, mSeconds.indexOf(second + mSecondStr));
setTimeItemIndex(hourIndex, minuteIndex, secondIndex);
setTimeItemIndexWithoutReLayout(hourIndex, minuteIndex, secondIndex);
}

public void notifyDataSetChanged() {
//年月日市时分秒是联动的,所以只需要通知年的数据变化
mYearPickerAdapter.notifyDataSetChanged();
}

private void setDateItemIndex(int yearIndex, int monthIndex, int dayIndex) {
Expand All @@ -348,6 +344,18 @@ private void setTimeItemIndex(int hourIndex, int minuteIndex, int secondIndex) {
mSecondWheelPicker.setCurrentItem(secondIndex);
}

private void setDateItemIndexWithoutReLayout(int yearIndex, int monthIndex, int dayIndex) {
mYearWheelPicker.setCurrentItem(yearIndex);
mMonthWheelPicker.setCurrentItem(monthIndex);
mDayWheelPicker.setCurrentItem(dayIndex);
}

private void setTimeItemIndexWithoutReLayout(int hourIndex, int minuteIndex, int secondIndex) {
mHourWheelPicker.setCurrentItemWithoutReLayout(hourIndex);
mMinuteWheelPicker.setCurrentItemWithoutReLayout(minuteIndex);
mSecondWheelPicker.setCurrentItemWithoutReLayout(secondIndex);
}

public void setTextSize(int textSize) {
if (textSize < 0) {
return;
Expand Down Expand Up @@ -431,7 +439,7 @@ public void onWheelSelected(AbstractWheelPicker wheelPicker, int index, Object d

boolean changed = false;
if (mMode == MODE_PENDING) {
if (index == 0) {
if (mSelectedYear == mCurrYear) {
//current year
updateMinMonths(mCurrMonth);
} else {
Expand All @@ -444,7 +452,7 @@ public void onWheelSelected(AbstractWheelPicker wheelPicker, int index, Object d
int monthIndex = Math.max(0, mMonths.indexOf((mSelectedMonth + 1) + mMonthStr));
mMonthWheelPicker.setCurrentItemWithoutReLayout(monthIndex);
} else {
if (index == mYears.size() - 1) {
if (mSelectedYear == mCurrYear && mMode == MODE_BIRTHDAY) {
//current year
updateMaxMonths(mCurrMonth);
} else {
Expand All @@ -463,7 +471,7 @@ public void onWheelSelected(AbstractWheelPicker wheelPicker, int index, Object d
mSelectedMonth = month;
}
if (mMode == MODE_PENDING) {
if (index == 0 && mSelectedYear == mCurrYear) {
if (mSelectedYear == mCurrYear && mSelectedMonth == mCurrMonth) {
//current month
correctMinDays(mCurrDay);
} else {
Expand All @@ -473,7 +481,7 @@ public void onWheelSelected(AbstractWheelPicker wheelPicker, int index, Object d
int dayIndex = Math.max(0, mDays.indexOf(mSelectedDay + mDayStr));
mDayWheelPicker.setCurrentItemWithoutReLayout(dayIndex);
} else {
if (index == mMonths.size() - 1 && mSelectedYear == mCurrYear) {
if (mSelectedYear == mCurrYear && mSelectedMonth == mCurrMonth && mMode == MODE_BIRTHDAY) {
//current month
updateMaxDays(mCurrDay);
} else {
Expand All @@ -485,7 +493,7 @@ public void onWheelSelected(AbstractWheelPicker wheelPicker, int index, Object d
case TYPE_DAY:
mSelectedDay = getCurrentDate(data, mDayStr);
if (mMode == MODE_PENDING) {
if (index == 0 && mSelectedYear == mCurrYear && mSelectedMonth == mCurrMonth) {
if (mSelectedYear == mCurrYear && mSelectedMonth == mCurrMonth && mSelectedDay == mCurrDay) {
//current day
updateMinHour(mCurrHour);
} else {
Expand All @@ -495,7 +503,8 @@ public void onWheelSelected(AbstractWheelPicker wheelPicker, int index, Object d
int hourIndex = Math.max(0, mHours.indexOf(mSelectedHour + mHourStr));
mHourWheelPicker.setCurrentItemWithoutReLayout(hourIndex);
} else {
if (index == mMonths.size() - 1 && mSelectedYear == mCurrYear && mSelectedMonth == mCurrMonth) {
if (mSelectedYear == mCurrYear && mSelectedMonth == mCurrMonth && mSelectedDay == mCurrDay
&& mMode == MODE_BIRTHDAY) {
//current month
updateMaxHour(mCurrHour);
} else {
Expand All @@ -505,13 +514,55 @@ public void onWheelSelected(AbstractWheelPicker wheelPicker, int index, Object d
mHourPickerAdapter.setData(mHours);
break;
case TYPE_HOUR:
mSelectedHour = getCurrentDate(data, mDayStr);
mSelectedHour = getCurrentDate(data, mHourStr);
if (mMode == MODE_PENDING) {
if (mSelectedYear == mCurrYear && mSelectedMonth == mCurrMonth && mSelectedDay == mCurrDay
&& mSelectedHour == mCurrHour) {
//current hour
updateMinMinute(mCurrMinute);
} else {
updateMaxMinute(60);
}

int minuteIndex = Math.max(0, mMinutes.indexOf(mSelectedMinute + mMinuteStr));
mMinuteWheelPicker.setCurrentItemWithoutReLayout(minuteIndex);
} else {
if (mSelectedYear == mCurrYear && mSelectedMonth == mCurrMonth && mSelectedDay == mCurrDay
&& mSelectedHour == mCurrHour && mMode == MODE_BIRTHDAY) {
//current month
updateMaxMinute(mCurrMinute);
} else {
updateMaxMinute(60);
}
}
mMinutePickerAdapter.setData(mMinutes);
break;
case TYPE_MINUTE:
mSelectedMinute = getCurrentDate(data, mDayStr);
mSelectedMinute = getCurrentDate(data, mMinuteStr);
if (mMode == MODE_PENDING) {
if (mSelectedYear == mCurrYear && mSelectedMonth == mCurrMonth && mSelectedDay == mCurrDay
&& mSelectedHour == mCurrHour && mSelectedMinute == mCurrMinute) {
//current minute
updateMinSecond(mCurrSecond);
} else {
updateMaxSecond(60);
}

int secondIndex = Math.max(0, mSeconds.indexOf(mSelectedSecond + mSecondStr));
mSecondWheelPicker.setCurrentItemWithoutReLayout(secondIndex);
} else {
if (mSelectedYear == mCurrYear && mSelectedMonth == mCurrMonth && mSelectedDay == mCurrDay
&& mSelectedHour == mCurrHour && mSelectedMinute == mCurrMinute && mMode == MODE_BIRTHDAY) {
//current month
updateMaxSecond(mCurrSecond);
} else {
updateMaxSecond(60);
}
}
mSecondPickerAdapter.setData(mSeconds);
break;
case TYPE_SECOND:
mSelectedSecond = getCurrentDate(data, mDayStr);
mSelectedSecond = getCurrentDate(data, mSecondStr);
break;
default:
break;
Expand Down
Loading

0 comments on commit 1a7b715

Please sign in to comment.