Fix null pointer exception in year picker
Retain year picker position offset on orientation change
Bug: 8658299
Bug: 8581974
Change-Id: Ib21c5235be29f9143788b22fbd25d62538203265
diff --git a/src/com/android/datetimepicker/date/DatePickerDialog.java b/src/com/android/datetimepicker/date/DatePickerDialog.java
index 77bedbf..8954c35 100644
--- a/src/com/android/datetimepicker/date/DatePickerDialog.java
+++ b/src/com/android/datetimepicker/date/DatePickerDialog.java
@@ -67,6 +67,7 @@
private static final String KEY_YEAR_START = "year_start";
private static final String KEY_YEAR_END = "year_end";
private static final String KEY_CURRENT_VIEW = "current_view";
+ private static final String KEY_LIST_POSITION_OFFSET = "list_position_offset";
private static final int DEFAULT_START_YEAR = 1900;
private static final int DEFAULT_END_YEAR = 2100;
@@ -181,6 +182,7 @@
listPosition = mDayPickerView.getMostVisiblePosition();
} else if (mCurrentView == YEAR_VIEW) {
listPosition = mYearPickerView.getFirstVisiblePosition();
+ outState.putInt(KEY_LIST_POSITION_OFFSET, mYearPickerView.getFirstPositionOffset());
}
outState.putInt(KEY_LIST_POSITION, listPosition);
}
@@ -202,6 +204,7 @@
mYearView.setOnClickListener(this);
int listPosition = -1;
+ int listPositionOffset = 0;
int currentView = MONTH_AND_DAY_VIEW;
if (savedInstanceState != null) {
mWeekStart = savedInstanceState.getInt(KEY_WEEK_START);
@@ -209,6 +212,7 @@
mMaxYear = savedInstanceState.getInt(KEY_YEAR_END);
currentView = savedInstanceState.getInt(KEY_CURRENT_VIEW);
listPosition = savedInstanceState.getInt(KEY_LIST_POSITION);
+ listPositionOffset = savedInstanceState.getInt(KEY_LIST_POSITION_OFFSET);
}
final Activity activity = getActivity();
@@ -248,7 +252,7 @@
if (currentView == MONTH_AND_DAY_VIEW) {
mDayPickerView.postSetSelection(listPosition);
} else if (currentView == YEAR_VIEW) {
- mYearPickerView.postSetSelection(listPosition);
+ mYearPickerView.postSetSelectionFromTop(listPosition, listPositionOffset);
}
}
return view;
diff --git a/src/com/android/datetimepicker/date/YearPickerView.java b/src/com/android/datetimepicker/date/YearPickerView.java
index 465b759..411c574 100644
--- a/src/com/android/datetimepicker/date/YearPickerView.java
+++ b/src/com/android/datetimepicker/date/YearPickerView.java
@@ -79,15 +79,19 @@
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mController.tryVibrate();
TextViewWithCircularIndicator clickedView = (TextViewWithCircularIndicator) view;
- if (mSelectedView != clickedView) {
- mSelectedView.drawIndicator(false);
- mSelectedView.requestLayout();
- clickedView.drawIndicator(true);
- clickedView.requestLayout();
- mSelectedView = clickedView;
+ if (clickedView != null) {
+ if (clickedView != mSelectedView) {
+ if (mSelectedView != null) {
+ mSelectedView.drawIndicator(false);
+ mSelectedView.requestLayout();
+ }
+ clickedView.drawIndicator(true);
+ clickedView.requestLayout();
+ mSelectedView = clickedView;
+ }
+ mController.onYearSelected(getYearFromTextView(clickedView));
+ mAdapter.notifyDataSetChanged();
}
- mController.onYearSelected(getYearFromTextView(clickedView));
- mAdapter.notifyDataSetChanged();
}
private int getYearFromTextView(TextView view) {
@@ -115,31 +119,32 @@
}
}
- public void postSetSelection(final int position) {
+ public void postSetSelectionCentered(final int position) {
+ postSetSelectionFromTop(position, mViewSize / 2 - mChildSize / 2);
+ }
+
+ public void postSetSelectionFromTop(final int position, final int offset) {
post(new Runnable() {
@Override
public void run() {
- setSelection(position);
+ setSelectionFromTop(position, offset);
requestLayout();
}
});
}
- public void postSetSelectionFromTop(final int position) {
- post(new Runnable() {
-
- @Override
- public void run() {
- setSelectionFromTop(position, mViewSize / 2 - mChildSize / 2);
- requestLayout();
- }
- });
+ public int getFirstPositionOffset() {
+ final View firstChild = getChildAt(0);
+ if (firstChild == null) {
+ return 0;
+ }
+ return firstChild.getTop();
}
@Override
public void onDateChanged() {
mAdapter.notifyDataSetChanged();
- postSetSelectionFromTop(mController.getSelectedDay().year - mController.getMinYear());
+ postSetSelectionCentered(mController.getSelectedDay().year - mController.getMinYear());
}
}