Restore expanded alarm ViewHolder on rotate

This commit is contained in:
Phillip Hsu 2016-09-02 20:46:09 -07:00
parent adb40beed8
commit 446d7a5334
2 changed files with 28 additions and 2 deletions

View File

@ -38,8 +38,9 @@ public class AlarmsCursorAdapter extends BaseCursorAdapter<Alarm, BaseAlarmViewH
@Override
public int getItemViewType(int position) {
final long stableId = getItemId(position);
return stableId != RecyclerView.NO_ID && stableId == mExpandedId
// final long stableId = getItemId(position);
return /*stableId != RecyclerView.NO_ID && stableId == mExpandedId*/
position == mExpandedPosition
? VIEW_TYPE_EXPANDED : VIEW_TYPE_COLLAPSED;
}
@ -82,4 +83,8 @@ public class AlarmsCursorAdapter extends BaseCursorAdapter<Alarm, BaseAlarmViewH
mExpandedPosition = RecyclerView.NO_POSITION;
notifyItemChanged(position);
}
public int getExpandedPosition() {
return mExpandedPosition;
}
}

View File

@ -8,6 +8,7 @@ import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.content.Loader;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
@ -34,6 +35,8 @@ public class AlarmsFragment extends RecyclerViewFragment<
static final String TAG_TIME_PICKER = "time_picker";
private static final String KEY_EXPANDED_POSITION = "expanded_position";
// TODO: Delete these constants. We no longer use EditAlarmActivity.
// @Deprecated
// private static final int REQUEST_EDIT_ALARM = 0;
@ -50,6 +53,8 @@ public class AlarmsFragment extends RecyclerViewFragment<
private Handler mHandler = new Handler();
private View mSnackbarAnchor;
private int mExpandedPosition = RecyclerView.NO_POSITION;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
@ -74,6 +79,13 @@ public class AlarmsFragment extends RecyclerViewFragment<
// TODO Read arguments
}
if (savedInstanceState != null) {
// Restore the value of the last expanded position here.
// We cannot tell the adapter to expand this item until onLoadFinished()
// is called.
mExpandedPosition = savedInstanceState.getInt(KEY_EXPANDED_POSITION, RecyclerView.NO_POSITION);
}
// Will succeed because the activity is created at this point.
// See the Fragment lifecycle.
mSnackbarAnchor = getActivity().findViewById(R.id.main_content);
@ -100,6 +112,9 @@ public class AlarmsFragment extends RecyclerViewFragment<
super.onLoadFinished(loader, data);
// TODO: If this was a content change due to an update, verify that
// we scroll to the updated alarm if its sort order changes.
// Does nothing If there is no expanded position.
getAdapter().expand(mExpandedPosition);
}
@Override
@ -244,6 +259,12 @@ public class AlarmsFragment extends RecyclerViewFragment<
mAsyncUpdateHandler.asyncInsert(alarm);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(KEY_EXPANDED_POSITION, getAdapter().getExpandedPosition());
}
/////////////////////////////////////////////////////////////////////////////////////
// TODO: We won't need these anymore, since we won't handle the db
// update in onActivityResult() anymore.