@Override public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) { mForecastAdapter.swapCursor(cursor); if (mPosition != ListView.INVALID_POSITION) { mListView.setSelection(mPosition); } }
private void updateEmptyView() { if (mForecastAdapter.getItemCount() == 0) { TextView tv = (TextView) getView().findViewById(R.id.recyclerview_forecast_empty); if (null != tv) { // if cursor is empty, why? do we have an invalid location int message = R.string.empty_forecast_list; @SunshineSyncAdapter.LocationStatus int location = Utility.getLocationStatus(getActivity()); switch (location) { case SunshineSyncAdapter.LOCATION_STATUS_SERVER_DOWN: message = R.string.empty_forecast_list_server_down; break; case SunshineSyncAdapter.LOCATION_STATUS_SERVER_INVALID: message = R.string.empty_forecast_list_server_error; break; case SunshineSyncAdapter.LOCATION_STATUS_INVALID: message = R.string.empty_forecast_list_invalid_location; break; default: if (!Utility.isNetworkAvailable(getActivity())) { message = R.string.empty_forecast_list_no_network; } } tv.setText(message); } } }
private void openPreferredLocationInMap() { // Using the URI scheme for showing a location found on a map. This super-handy // intent can is detailed in the "Common Intents" page of Android's developer site: // http://developer.android.com/guide/components/intents-common.html#Maps if (null != mForecastAdapter) { Cursor c = mForecastAdapter.getCursor(); if (null != c) { c.moveToPosition(0); String posLat = c.getString(COL_COORD_LAT); String posLong = c.getString(COL_COORD_LONG); Uri geoLocation = Uri.parse("geo:" + posLat + "," + posLong); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(geoLocation); if (intent.resolveActivity(getActivity().getPackageManager()) != null) { startActivity(intent); } else { Log.d( LOG_TAG, "Couldn't call " + geoLocation.toString() + ", no receiving apps installed!"); } } } }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { weatheradapter = new ForecastAdapter(getActivity(), null, 0); View rootView = inflater.inflate(R.layout.fragment_main, container, false); listview = (ListView) rootView.findViewById(R.id.listview_weather); listview.setAdapter(weatheradapter); listview.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { Cursor cursor = (Cursor) adapterView.getItemAtPosition(position); if (cursor != null) { String locationSetting = Utility.getPreferredLocation(getActivity()); ((Callback) getActivity()) .onItemSelected( Weathercontract.WeatherEntry.buildWeatherLocationWithDate( locationSetting, cursor.getLong(COL_WEATHER_DATE))); } mPosition = position; } }); if (savedInstanceState != null && savedInstanceState.containsKey(SELECTED_KEY)) { mPosition = savedInstanceState.getInt(SELECTED_KEY); } weatheradapter.setUseTodayLayout(mUseTodayLayout); return (rootView); }
@Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { mForecastAdapter.swapCursor(data); if (mPosition != ListView.INVALID_POSITION) { mlistView.smoothScrollToPosition(mPosition); } updateEmptyView(); }
@Override public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) { weatheradapter.swapCursor(cursor); if (mPosition != ListView.INVALID_POSITION) { // If we don't need to restart the loader, and there's a desired position to restore // to, do so now. listview.smoothScrollToPosition(mPosition); } }
@Override public void onSaveInstanceState(Bundle outState) { // When tablets rotate, the currently selected list item needs to be saved. // When no item is selected, mPosition will be set to RecyclerView.NO_POSITION, // so check for that before storing. if (mPosition != RecyclerView.NO_POSITION) { outState.putInt(SELECTED_KEY, mPosition); } mForecastAdapter.onSaveInstanceState(outState); super.onSaveInstanceState(outState); }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // The CursorAdapter will take data from our cursor and populate the ListView. mForecastAdapter = new ForecastAdapter(getActivity(), null, 0); mForecastAdapter.setmUseTodayLayout(mUseTodayLayout); View rootView = inflater.inflate(R.layout.fragment_main, container, false); // Get a reference to the ListView, and attach this adapter to it. mListView = (ListView) rootView.findViewById(R.id.listview_forecast); mListView.setAdapter(mForecastAdapter); // We'll call our MainActivity mListView.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { // CursorAdapter returns a cursor at the correct position for getItem(), or null // if it cannot seek to that position. Cursor cursor = (Cursor) adapterView.getItemAtPosition(position); if (cursor != null) { String locationSetting = Utility.getPreferredLocation(getActivity()); ((Callback) getActivity()) .onItemSelected( WeatherContract.WeatherEntry.buildWeatherLocationWithDate( locationSetting, cursor.getLong(COL_WEATHER_DATE))); mPosition = position; } } }); if (savedInstanceState != null && savedInstanceState.containsKey(SELECTED_KEY)) { mPosition = savedInstanceState.getInt(SELECTED_KEY); } mForecastAdapter.setmUseTodayLayout(mUseTodayLayout); return rootView; }
@Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { mForecastAdapter.swapCursor(data); new SendFirstItemData().execute(data); if (mPosition != RecyclerView.NO_POSITION) { // If we don't need to restart the loader, and there's a desired position to restore // to, do so now. mRecyclerView.smoothScrollToPosition(mPosition); } updateEmptyView(); // if (data.getCount() > 0) { 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 itemPosition = mForecastAdapter.getSelectedItemPosition(); if (RecyclerView.NO_POSITION == itemPosition) itemPosition = 0; RecyclerView.ViewHolder vh = mRecyclerView.findViewHolderForAdapterPosition(itemPosition); if (null != vh && mAutoSelectView) { mForecastAdapter.selectView(vh); } if (mHoldForTransition) { getActivity().supportStartPostponedEnterTransition(); } return true; } return false; } }); } }
@Nullable @Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); mForecastAdapter = new ForecastAdapter(getActivity(), null, 0); mForecastAdapter.setUseTodayLayout(mUseTodayLayout); mlistView = (ListView) rootView.findViewById(R.id.listview_forecast); View emptyView = rootView.findViewById(R.id.forecast_fragment_empty_textview); mlistView.setEmptyView(emptyView); mlistView.setAdapter(mForecastAdapter); mlistView.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // CursorAdapter returns a cursor at the correct position for getItem(), or null Cursor cursor = (Cursor) parent.getItemAtPosition(position); if (cursor != null) { String locationSetting = Utility.getPreferredLocation(getActivity()); ((Callback) getActivity()) .onItemSelected( WeatherContract.WeatherEntry.buildWeatherLocationWithDate( locationSetting, cursor.getLong(COL_WEATHER_DATE))); } mPosition = position; } }); if (savedInstanceState != null && savedInstanceState.containsKey(SELECTED_KEY)) { mPosition = savedInstanceState.getInt(SELECTED_KEY); } return rootView; }
@Override public void onLoaderReset(Loader<Cursor> cursorLoader) { mForecastAdapter.swapCursor(null); }
public void setUseTodayLayout(boolean useTodayLayout) { mUseTodayLayout = useTodayLayout; if (mForecastAdapter != null) { mForecastAdapter.setmUseTodayLayout(mUseTodayLayout); } }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { String locationSetting = Utility.getPreferredLocation(getActivity()); // Sort order: Ascending, by date. // String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + " ASC"; // Uri weatherForLocationUri = // WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate( // locationSetting, System.currentTimeMillis()); // Cursor cur = getActivity().getContentResolver().query(weatherForLocationUri, // null, null, null, sortOrder); // The ForecastAdapter will take data from a source and // use it to populate the ListView it's attached to. // However, we cannot use FLAG_AUTO_REQUERY since it is deprecated, so we will end // up with an empty list the first time we run. // // The CursorAdapter will take data from our cursor and populate the ListView. // mForecastAdapter = new ForecastAdapter(getActivity(), null, 0); //// The ForecastAdapter will take data from a source and // // use it to populate the RecyclerView it's attached to. // mForecastAdapter = new ForecastAdapter(getActivity()); rootView = inflater.inflate(R.layout.fragment_main, container, false); // mListView = (ListView) rootView.findViewById(R.id.listview_forecast); // View emptyView = rootView.findViewById(R.id.listview_forecast_empty); // mListView.setEmptyView(emptyView); // mListView.setAdapter(mForecastAdapter); // Get a reference to the RecyclerView, and attach this adapter to it. mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview_forecast); // Set the layout manager mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); View emptyView = rootView.findViewById(R.id.recyclerview_forecast_empty); // use this setting to improve performance if you know that changes // in content do not change the layout size of the RecyclerView mRecyclerView.setHasFixedSize(true); // The ForecastAdapter will take data from a source and // use it to populate the RecyclerView it's attached to. mForecastAdapter = new ForecastAdapter( getActivity(), new ForecastAdapter.ForecastAdapterOnClickHandler() { @Override public void onClick(Long date, ForecastAdapter.ForecastAdapterViewHolder vh) { String locationSetting = Utility.getPreferredLocation(getActivity()); ((Callback) getActivity()) .onItemSelected( WeatherContract.WeatherEntry.buildWeatherLocationWithDate( locationSetting, date), vh); // I could have sent the position back directly instead of the ViewHolder mPosition = vh.getAdapterPosition(); } }, emptyView, mChoiceMode); // specify an adapter (see also next example) mRecyclerView.setAdapter(mForecastAdapter); final View parallaxView = rootView.findViewById(R.id.parallax_bar); if (null != parallaxView) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { mRecyclerView.addOnScrollListener( new RecyclerView.OnScrollListener() { @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); int max = parallaxView.getHeight(); if (dy > 0) { parallaxView.setTranslationY( Math.max(-max, parallaxView.getTranslationY() - dy / 2)); } else { parallaxView.setTranslationY( Math.min(0, parallaxView.getTranslationY() - dy / 2)); } } }); } } final AppBarLayout appbarView = (AppBarLayout) rootView.findViewById(R.id.appbar); if (null != appbarView) { ViewCompat.setElevation(appbarView, 0); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mRecyclerView.addOnScrollListener( new RecyclerView.OnScrollListener() { @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { if (0 == mRecyclerView.computeVerticalScrollOffset()) { appbarView.setElevation(0); } else { appbarView.setElevation(appbarView.getTargetElevation()); } } }); } } // mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { // // @Override // public void onItemClick(AdapterView adapterView, View view, int position, long l) // { // // CursorAdapter returns a cursor at the correct position for getItem(), or // null // // if it cannot seek to that position. // Cursor cursor = (Cursor) adapterView.getItemAtPosition(position); // if (cursor != null) { // String locationSetting = Utility.getPreferredLocation(getActivity()); // // ((Callback) getActivity()) // // .onItemSelected(WeatherContract.WeatherEntry.buildWeatherLocationWithDate( // locationSetting, cursor.getLong(COL_WEATHER_DATE) // )); // // mPosition = position; // } // } // }); // If there's instance state, mine it for useful information. // The end-goal here is that the user never knows that turning their device sideways // does crazy lifecycle related things. It should feel like some stuff stretched out, // or magically appeared to take advantage of room, but data or place in the app was never // actually *lost*. if (savedInstanceState != null) { if (savedInstanceState.containsKey(SELECTED_KEY)) { // The Recycler View probably hasn't even been populated yet. Actually perform the // swapout in onLoadFinished. mPosition = savedInstanceState.getInt(SELECTED_KEY); } mForecastAdapter.onRestoreInstanceState(savedInstanceState); } mForecastAdapter.setUseTodayLayout(mUseTodayLayout); return rootView; }
public void setUseTodayLayout(boolean useTodayLayout) { mUseTodayLayout = useTodayLayout; if (weatheradapter != null) { weatheradapter.setUseTodayLayout(mUseTodayLayout); } }
@Override public void onLoaderReset(Loader<Cursor> cursorLoader) { weatheradapter.swapCursor(null); }