@Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { mForecastAdapter.swapCursor(data); updateEmptyView(); if (data.getCount() == 0) { getActivity().supportStartPostponedEnterTransition(); } else { mRecyclerView .getViewTreeObserver() .addOnPreDrawListener( new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { // Since we know we're going to get items, we keep the listener around until // we see Children. if (mRecyclerView.getChildCount() > 0) { mRecyclerView.getViewTreeObserver().removeOnPreDrawListener(this); int position = mForecastAdapter.getSelectedItemPosition(); if (position == RecyclerView.NO_POSITION && -1 != mInitialSelectedDate) { Cursor data = mForecastAdapter.getCursor(); int count = data.getCount(); int dateColumn = data.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_DATE); for (int i = 0; i < count; i++) { data.moveToPosition(i); if (data.getLong(dateColumn) == mInitialSelectedDate) { position = i; break; } } } if (position == RecyclerView.NO_POSITION) position = 0; // If we don't need to restart the loader, and there's a desired position to // restore // to, do so now. mRecyclerView.smoothScrollToPosition(position); RecyclerView.ViewHolder vh = mRecyclerView.findViewHolderForAdapterPosition(position); if (null != vh && mAutoSelectView) { mForecastAdapter.selectView(vh); } if (mHoldForTransition) { getActivity().supportStartPostponedEnterTransition(); } return true; } return false; } }); } }
@Override public void onLoaderReset(Loader<Cursor> loader) { mForecastAdapter.swapCursor(null); }